📄 frmsetdbinfo.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.Win32;
using System.IO;
namespace GlobleUtility
{
/// <summary>
/// 类名:CfrmSetDBInfo
/// 功能:设置数据库参数
/// </summary>
public class CfrmSetDBInfo : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button btnCancel;
public System.Windows.Forms.TextBox edtDBComputer;
public System.Windows.Forms.TextBox edtDBName;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public CfrmSetDBInfo()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.edtDBComputer = new System.Windows.Forms.TextBox();
this.edtDBName = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.btnOK = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 21);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 16);
this.label1.TabIndex = 3;
this.label1.Text = "服务器 IP";
//
// edtDBComputer
//
this.edtDBComputer.AcceptsReturn = true;
this.edtDBComputer.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.edtDBComputer.Location = new System.Drawing.Point(88, 16);
this.edtDBComputer.MaxLength = 23;
this.edtDBComputer.Name = "edtDBComputer";
this.edtDBComputer.Size = new System.Drawing.Size(136, 21);
this.edtDBComputer.TabIndex = 4;
this.edtDBComputer.Text = "";
this.edtDBComputer.WordWrap = false;
this.edtDBComputer.TextChanged += new System.EventHandler(this.edtDBComputer_TextChanged);
//
// edtDBName
//
this.edtDBName.AcceptsReturn = true;
this.edtDBName.Location = new System.Drawing.Point(88, 48);
this.edtDBName.MaxLength = 64;
this.edtDBName.Name = "edtDBName";
this.edtDBName.Size = new System.Drawing.Size(136, 21);
this.edtDBName.TabIndex = 6;
this.edtDBName.Text = "";
this.edtDBName.TextChanged += new System.EventHandler(this.edtDBName_TextChanged);
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 53);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 16);
this.label2.TabIndex = 5;
this.label2.Text = "数据库名称";
//
// btnOK
//
this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnOK.Enabled = false;
this.btnOK.Location = new System.Drawing.Point(16, 88);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(88, 24);
this.btnOK.TabIndex = 7;
this.btnOK.Text = "确定(&O)";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// btnCancel
//
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(136, 88);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(88, 24);
this.btnCancel.TabIndex = 8;
this.btnCancel.Text = "取消(&C)";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// CfrmSetDBInfo
//
this.AcceptButton = this.btnOK;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(242, 127);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.edtDBName);
this.Controls.Add(this.edtDBComputer);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "CfrmSetDBInfo";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "设置数据库参数";
this.Load += new System.EventHandler(this.CfrmSetDBInfo_Load);
this.ResumeLayout(false);
}
#endregion
private void btnCancel_Click(object sender, System.EventArgs e)
{
Close();
}
private void btnOK_Click(object sender, System.EventArgs e)
{
Register Reg = new Register();
Reg.DBComputer = edtDBComputer.Text.Trim();
Reg.DBSource = edtDBName.Text.Trim();
///////---设置数据库服务器IP到配置文件---//////////
//初始化字符串
string sFileName = null;
string sFileContent = null;
string sIP = null;
sIP = this.edtDBComputer.Text.Trim();
if("" == sIP)
{
MessageBox.Show(this,"您没有输入有效的IP地址!","错误提示");
return;
}
sFileContent = "LIBCARD =\n"
+" (DESCRIPTION =\n"
+" (ADDRESS_LIST =\n"
+" (ADDRESS = (PROTOCOL = TCP)(HOST = "+sIP.Trim()+")(PORT = 1521))\n"
+" )\n"
+" (CONNECT_DATA = (SID = lib)(SERVER = DEDICATED))\n"
+" )";
//从注册表读取配置文件的位置
RegistryKey rk = Registry.LocalMachine;
rk = rk.CreateSubKey("Software\\BJTU_AILab\\LibCard");
if(null == rk)
{
MessageBox.Show(this,"找不到配置文件,设置数据库服务器IP地址失败!","错误提示");
return;
}
sFileName = (string)rk.GetValue("DBConfigDir", "");
if("" == sFileName)
{
MessageBox.Show(this,"找不到配置文件,设置数据库服务器IP地址失败!","错误提示");
return;
}
//找到配置文件后,更新配置文件
try
{
StreamWriter swFileWriter = new StreamWriter(sFileName,false);
swFileWriter.Write(sFileContent);
swFileWriter.Close();
}
catch
{
MessageBox.Show(this,"设置数据库服务器IP地址失败!","错误提示");
return;
}
}
private void edtDBComputer_TextChanged(object sender, System.EventArgs e)
{
if(ValidateParam())
btnOK.Enabled = true;
else
btnOK.Enabled = false;
}
private void edtDBName_TextChanged(object sender, System.EventArgs e)
{
if(ValidateParam())
btnOK.Enabled = true;
else
btnOK.Enabled = false;
}
private bool ValidateParam()
{
//检查数据的合法性。注意,这里只是简单地检查是否有字符输入。
//由于数据库机器名可以写成类似IP的形式,故无法判断到底是IP还是机器名。
if(edtDBComputer.Text.Trim().Length == 0)
return false;
if(edtDBName.Text.Trim().Length == 0)
return false;
return true;
}
private void CfrmSetDBInfo_Load(object sender, System.EventArgs e)
{
this.edtDBName.Enabled = false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -