⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 databasesetting.cs

📁 图书馆信息管理系统 Library Information Management System
💻 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 + -