📄 config.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace AutoCreateCode
{
/// <summary>
/// config 的摘要说明。
/// </summary>
public class config : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnConfim;
private System.Windows.Forms.Button btnTest;
private System.Windows.Forms.TextBox txtServerName;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox txtDBName;
private System.Windows.Forms.TextBox txtUid;
private System.Windows.Forms.TextBox txtPwd;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public config()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
LoadData();
//
// 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.txtServerName = new System.Windows.Forms.TextBox();
this.btnConfim = new System.Windows.Forms.Button();
this.btnTest = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.txtDBName = new System.Windows.Forms.TextBox();
this.txtUid = new System.Windows.Forms.TextBox();
this.txtPwd = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("隶书", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 23);
this.label1.TabIndex = 0;
this.label1.Text = "服务器:";
//
// txtServerName
//
this.txtServerName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtServerName.Location = new System.Drawing.Point(88, 16);
this.txtServerName.Name = "txtServerName";
this.txtServerName.Size = new System.Drawing.Size(88, 21);
this.txtServerName.TabIndex = 1;
this.txtServerName.Text = "";
//
// btnConfim
//
this.btnConfim.Location = new System.Drawing.Point(200, 64);
this.btnConfim.Name = "btnConfim";
this.btnConfim.TabIndex = 2;
this.btnConfim.Text = "确 定";
this.btnConfim.Click += new System.EventHandler(this.btnConfim_Click);
//
// btnTest
//
this.btnTest.Location = new System.Drawing.Point(200, 32);
this.btnTest.Name = "btnTest";
this.btnTest.TabIndex = 4;
this.btnTest.Text = "测 试";
this.btnTest.Click += new System.EventHandler(this.btnTest_Click);
//
// label2
//
this.label2.Font = new System.Drawing.Font("隶书", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label2.Location = new System.Drawing.Point(16, 40);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 23);
this.label2.TabIndex = 5;
this.label2.Text = "数据库:";
//
// label3
//
this.label3.Font = new System.Drawing.Font("隶书", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label3.Location = new System.Drawing.Point(16, 64);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(64, 23);
this.label3.TabIndex = 6;
this.label3.Text = "用户名:";
//
// label4
//
this.label4.Font = new System.Drawing.Font("隶书", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label4.Location = new System.Drawing.Point(16, 88);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(56, 23);
this.label4.TabIndex = 7;
this.label4.Text = "密 码:";
//
// txtDBName
//
this.txtDBName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtDBName.Location = new System.Drawing.Point(88, 40);
this.txtDBName.Name = "txtDBName";
this.txtDBName.Size = new System.Drawing.Size(88, 21);
this.txtDBName.TabIndex = 8;
this.txtDBName.Text = "";
//
// txtUid
//
this.txtUid.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtUid.Location = new System.Drawing.Point(88, 64);
this.txtUid.Name = "txtUid";
this.txtUid.Size = new System.Drawing.Size(88, 21);
this.txtUid.TabIndex = 9;
this.txtUid.Text = "";
//
// txtPwd
//
this.txtPwd.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtPwd.Location = new System.Drawing.Point(88, 88);
this.txtPwd.Name = "txtPwd";
this.txtPwd.PasswordChar = '*';
this.txtPwd.Size = new System.Drawing.Size(88, 21);
this.txtPwd.TabIndex = 10;
this.txtPwd.Text = "";
//
// config
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(296, 118);
this.Controls.Add(this.txtPwd);
this.Controls.Add(this.txtUid);
this.Controls.Add(this.txtDBName);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.btnTest);
this.Controls.Add(this.btnConfim);
this.Controls.Add(this.txtServerName);
this.Controls.Add(this.label1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "config";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "设置";
this.ResumeLayout(false);
}
#endregion
private void btnConfim_Click(object sender, System.EventArgs e)
{
string filePath = Application.StartupPath+@"\config.ini";
INIClass objAccessIni = new INIClass(filePath);
if( this.txtServerName.Text != "" && this.txtServerName.Text != "" && this.txtUid.Text != "" && this.txtPwd.Text != "" )
{
try
{
string sqlconn = "server="+txtServerName.Text.Trim()
+";database="+txtDBName.Text.Trim()
+";uid="+txtUid.Text.Trim()
+";pwd="+txtPwd.Text.Trim()+";";
objAccessIni.IniWriteValue("strconn","sqlconnstring",sqlconn);
}
catch
{
MessageBox.Show("保存数据库连接字符串报错!");
}
this.Close();
}
}
private void btnCanel_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void btnTest_Click(object sender, System.EventArgs e)
{
if( this.txtServerName.Text != "" && this.txtServerName.Text != "" && this.txtUid.Text != "" && this.txtPwd.Text != "" )
{
bool bResult;
string sqlconn = "server="+txtServerName.Text.Trim()
+";database="+txtDBName.Text.Trim()
+";uid="+txtUid.Text.Trim()
+";pwd="+txtPwd.Text.Trim();
bResult = DBConn.TestConn(sqlconn);
if( bResult )
{
MessageBox.Show("数据库连接成功!");
}
else
{
MessageBox.Show("数据连接失败!");
}
}
else
{
MessageBox.Show("请输入数据库连接字符串!");
}
}
private void LoadData()
{
string filePath = Application.StartupPath+@"\config.ini";
INIClass objAccessIni = new INIClass(filePath);
try
{
string sqlconn = objAccessIni.IniReadValue("strconn","sqlconnstring");
if( sqlconn != "" )
{
string[] temp = sqlconn.Split(';');
this.txtServerName.Text = GetFistStr(temp[0]);
this.txtDBName.Text = GetFistStr(temp[1]);
this.txtUid.Text = GetFistStr(temp[2]);
this.txtPwd.Text = GetFistStr(temp[3]);
}
}
catch
{
}
}
private string GetFistStr(string oldStr)
{
string[] tempstr = oldStr.Split('=');
return tempstr[1];
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -