?? databasesetting.cs
字號:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using DataBaseOperation;
using Library.Class;
namespace Library
{
/// <summary>
/// DataBaseSetting 的摘要說明。
/// </summary>
public class DataBaseSetting : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private DataBaseOperation.DBConfig db = new DBConfig();
public DataBaseSetting()
{
//
// Windows 窗體設計器支持所必需的
//
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.groupBox1 = new System.Windows.Forms.GroupBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.btnOK = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.textBox4);
this.groupBox1.Controls.Add(this.textBox3);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.label4);
this.groupBox1.Controls.Add(this.textBox2);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(352, 176);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "數據庫連接";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(96, 60);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(224, 21);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(96, 24);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(224, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(16, 62);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 17);
this.label2.TabIndex = 1;
this.label2.Text = "數據庫名稱:";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(16, 26);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 17);
this.label1.TabIndex = 0;
this.label1.Text = "服務器地址:";
//
// btnOK
//
this.btnOK.Location = new System.Drawing.Point(192, 208);
this.btnOK.Name = "btnOK";
this.btnOK.TabIndex = 5;
this.btnOK.Text = "確定";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// button1
//
this.button1.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.button1.Location = new System.Drawing.Point(280, 208);
this.button1.Name = "button1";
this.button1.TabIndex = 6;
this.button1.Text = "關閉";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(96, 132);
this.textBox4.Name = "textBox4";
this.textBox4.PasswordChar = '*';
this.textBox4.Size = new System.Drawing.Size(224, 21);
this.textBox4.TabIndex = 8;
this.textBox4.Text = "";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(96, 96);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(224, 21);
this.textBox3.TabIndex = 7;
this.textBox3.Text = "";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(24, 98);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(42, 17);
this.label3.TabIndex = 10;
this.label3.Text = "用戶名";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(24, 134);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(42, 17);
this.label4.TabIndex = 9;
this.label4.Text = "密 碼";
//
// DataBaseSetting
//
this.AcceptButton = this.btnOK;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.CancelButton = this.button1;
this.ClientSize = new System.Drawing.Size(370, 247);
this.ControlBox = false;
this.Controls.Add(this.button1);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.groupBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "DataBaseSetting";
this.ShowInTaskbar = false;
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "數據庫設定";
this.Load += new System.EventHandler(this.DataBaseSetting_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 關閉
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
/// <summary>
/// 初始化界面
/// </summary>
private void initializeScreen()
{
textBox1.Text = db.DbSource;
textBox2.Text = db.DbName;
textBox3.Text = db.UID;
textBox4.Text = db.PWD;
textBox1.Focus();
}
/// <summary>
/// 自動生成鏈接字符串
/// </summary>
private void makeStrConn()
{
string ConnectionString = "server=" + db.DbSource + ";database=" + db.DbName + ";uid =" + db.UID + ";pwd =" + db.PWD;
//數據庫聯接字符串
Global.conn.ConnectionString = ConnectionString;
}
/// <summary>
/// 構建數據庫聯接配置文件
/// </summary>
private bool CreateDbSourceXML()
{
db.UID = textBox3.Text ;
db.PWD = textBox4.Text;
db.DbName = textBox2.Text;
db.DbSource = textBox1.Text;
db.TimeOut = "15";
db.Save();
//測試聯接
return testConnection();
}
private bool testConnection()
{
//測試聯接
try
{
//創建聯接字符串
makeStrConn();
//打開數據庫聯接
Global.conn.Open();
return true;
}
catch(Exception ex)
{
Global.conn.Close();
MessageBox.Show("數據庫聯接失敗!\n錯誤信息如下:\n\t" + ex.ToString(),"錯誤",MessageBoxButtons.OK,MessageBoxIcon.Error);
return false;
}
}
/// <summary>
/// 數據庫聯接
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOK_Click(object sender, System.EventArgs e)
{
if(CreateDbSourceXML())
{
this.Close();
}
else
{
initializeScreen();
}
}
private void DataBaseSetting_Load(object sender, System.EventArgs e)
{
initializeScreen();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -