📄 dbcon.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.Win32;
namespace 通讯录
{
/// <summary>
/// dbcon 的摘要说明。
/// </summary>
public class dbcon : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox txtserver;
private System.Windows.Forms.TextBox txtdb;
private System.Windows.Forms.TextBox txtuser;
private System.Windows.Forms.TextBox txtpwd;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button btnexit;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public dbcon()
{
//
// 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.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.txtserver = new System.Windows.Forms.TextBox();
this.txtdb = new System.Windows.Forms.TextBox();
this.txtuser = new System.Windows.Forms.TextBox();
this.txtpwd = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.btnexit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(40, 35);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 23);
this.label1.TabIndex = 0;
this.label1.Text = "服务器名:";
//
// label2
//
this.label2.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label2.Location = new System.Drawing.Point(40, 83);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 23);
this.label2.TabIndex = 1;
this.label2.Text = "数据库名:";
//
// label3
//
this.label3.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label3.Location = new System.Drawing.Point(40, 123);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(80, 23);
this.label3.TabIndex = 2;
this.label3.Text = "登录名:";
//
// label4
//
this.label4.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label4.Location = new System.Drawing.Point(40, 163);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(80, 23);
this.label4.TabIndex = 3;
this.label4.Text = "密码:";
//
// txtserver
//
this.txtserver.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.txtserver.Location = new System.Drawing.Point(144, 32);
this.txtserver.Name = "txtserver";
this.txtserver.Size = new System.Drawing.Size(152, 23);
this.txtserver.TabIndex = 4;
this.txtserver.Text = "";
//
// txtdb
//
this.txtdb.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.txtdb.Location = new System.Drawing.Point(144, 72);
this.txtdb.Name = "txtdb";
this.txtdb.Size = new System.Drawing.Size(152, 23);
this.txtdb.TabIndex = 5;
this.txtdb.Text = "";
//
// txtuser
//
this.txtuser.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.txtuser.Location = new System.Drawing.Point(144, 116);
this.txtuser.Name = "txtuser";
this.txtuser.Size = new System.Drawing.Size(152, 23);
this.txtuser.TabIndex = 6;
this.txtuser.Text = "";
//
// txtpwd
//
this.txtpwd.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.txtpwd.Location = new System.Drawing.Point(144, 160);
this.txtpwd.Name = "txtpwd";
this.txtpwd.Size = new System.Drawing.Size(152, 23);
this.txtpwd.TabIndex = 7;
this.txtpwd.Text = "";
//
// button1
//
this.button1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button1.Location = new System.Drawing.Point(48, 229);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 32);
this.button1.TabIndex = 8;
this.button1.Text = "确定";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// btnexit
//
this.btnexit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnexit.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnexit.Location = new System.Drawing.Point(184, 230);
this.btnexit.Name = "btnexit";
this.btnexit.Size = new System.Drawing.Size(88, 32);
this.btnexit.TabIndex = 9;
this.btnexit.Text = "退出";
this.btnexit.Click += new System.EventHandler(this.button2_Click);
//
// dbcon
//
this.AcceptButton = this.button1;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.CancelButton = this.btnexit;
this.ClientSize = new System.Drawing.Size(328, 285);
this.Controls.Add(this.btnexit);
this.Controls.Add(this.button1);
this.Controls.Add(this.txtpwd);
this.Controls.Add(this.txtuser);
this.Controls.Add(this.txtdb);
this.Controls.Add(this.txtserver);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "dbcon";
this.Text = " 连接数据库服务器";
this.Load += new System.EventHandler(this.dbcon_Load);
this.ResumeLayout(false);
}
#endregion
#region "注册数据库"
private void button1_Click(object sender, System.EventArgs e)
{
string server=txtserver.Text;
string database=txtdb.Text;
string user=txtuser.Text;
string pwd=txtpwd.Text;
try
{
RegistryKey mkey= Registry.CurrentUser;
RegistryKey sub1=mkey.OpenSubKey("software",true);
RegistryKey sub2=sub1.CreateSubKey("csharp");
RegistryKey sub3=sub2.CreateSubKey("dbconnection");
RegistryKey sub4=sub3.CreateSubKey("db");
sub4.SetValue("server",server);
sub4.SetValue("database",database);
sub4.SetValue("userid",user);
sub4.SetValue("passwd",pwd);
//测试数据库连接是否成功
Classdb test=new Classdb();
bool tf;
tf=test.con(server,database,user,pwd);
if (tf) //如果成功
{
MessageBox.Show("注册成功!");
this.Close();
}
else
{
MessageBox.Show("注册失败,请确认输入的信息全部正确!");
}
}
catch (Exception ex)
{
MessageBox.Show("注册失败!" + "\n" + ex.ToString());
}
}
#endregion
#region "窗体启动时显示已注册服务器信息"
private void dbcon_Load(object sender, System.EventArgs e)
{
try
//如果数据库已经连接,则在文本框中显示已连接的数据信息
{
RegistryKey su1=Registry.CurrentUser;
RegistryKey su2=su1.OpenSubKey("software");
RegistryKey su3=su2.OpenSubKey("csharp");
RegistryKey su4=su3.OpenSubKey("dbconnection");
RegistryKey su5=su4.OpenSubKey("db");
string server=su5.GetValue("server").ToString();
string database=su5.GetValue("database").ToString();
string user=su5.GetValue("userid").ToString();
string pwd=su5.GetValue("passwd").ToString();
txtserver.Text=server;
txtdb.Text=database;
txtuser.Text=user;
txtpwd.Text=pwd;
}
catch
{
//如果尚未连接数据库连接错误,则清空文本框;
txtserver.Text="";
txtdb.Text="";
txtuser.Text="";
txtpwd.Text="";
}
}
#endregion
#region "关闭窗体"
private void button2_Click(object sender, System.EventArgs e)
{
this.Close(); //关闭
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -