📄 form1.cs
字号:
this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// dataGrid1
//
this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 88);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(464, 232);
this.dataGrid1.TabIndex = 6;
//
// btnQuery
//
this.btnQuery.Location = new System.Drawing.Point(280, 56);
this.btnQuery.Name = "btnQuery";
this.btnQuery.Size = new System.Drawing.Size(56, 24);
this.btnQuery.TabIndex = 5;
this.btnQuery.Text = "Query";
this.btnQuery.Click += new System.EventHandler(this.btnQuery_Click);
//
// txtCondition
//
this.txtCondition.Location = new System.Drawing.Point(88, 56);
this.txtCondition.Name = "txtCondition";
this.txtCondition.Size = new System.Drawing.Size(176, 21);
this.txtCondition.TabIndex = 4;
this.txtCondition.Text = "";
//
// label6
//
this.label6.Location = new System.Drawing.Point(8, 56);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(72, 23);
this.label6.TabIndex = 3;
this.label6.Text = "Where";
this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label5
//
this.label5.Location = new System.Drawing.Point(8, 32);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(72, 23);
this.label5.TabIndex = 1;
this.label5.Text = "TableName";
this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// btnStructure
//
this.btnStructure.Location = new System.Drawing.Point(352, 56);
this.btnStructure.Name = "btnStructure";
this.btnStructure.TabIndex = 10;
this.btnStructure.Text = "Structure";
this.btnStructure.Click += new System.EventHandler(this.btnStructure_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(472, 342);
this.Controls.Add(this.tabControl1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.tabControl1.ResumeLayout(false);
this.tabPgLogin.ResumeLayout(false);
this.tabPgSelect.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void tabPgLogin_Click(object sender, System.EventArgs e)
{
}
private void Form1_Load(object sender, System.EventArgs e)
{
setbtnEnable(m_isconnected);
#region 获取SqlServers列表,并载入到ComBox中
try
{
SqlServerList sqlserlist = new SqlServerList();
ServerEnumerator serenum = (ServerEnumerator)sqlserlist.GetEnumerator();
this.cmbServer.Items.Add("(local)");
while(serenum.MoveNext())
{
this.cmbServer.Items.Add(((SqlServer)serenum.Current).Name);
}
this.cmbServer.SelectedIndex = 0;
}
catch
{
this.cmbServer.Items.Add("(local)");
}
#endregion
}
private void button1_Click(object sender, System.EventArgs e)
{
//获取连接字符串
connstr = "Data Source=" + this.cmbServer.Text + ";Initial Catalog=master;User Id=" + this.txtUser.Text + ";Password=" + this.txtPassword.Text + ";Connection Timeout=15;Connection Lifetime=60";
mycmd.ServerName = this.cmbServer.Text;
mycmd.DataBaseName = "Master";
mycmd.UserName=txtUser.Text;
mycmd.Password= txtPassword.Text;
#region 测试连接
try
{
this.label4.Text = "Connecting....";
Thread.Sleep(200);
this.button1.Enabled = false;
sqlconn = new SqlConnection(connstr);
sqlconn.Open();
SqlCommand sqlcmd = new SqlCommand("SELECT @@VERSION",sqlconn);
string sqlversion = sqlcmd.ExecuteScalar().ToString();
sqlversion = sqlversion.Replace("8.00.194","RTM");
sqlversion = sqlversion.Replace("8.00.384","SP1");
sqlversion = sqlversion.Replace("8.00.534","SP2");
sqlversion = sqlversion.Replace("8.00.760","SP3");
sqlversion = sqlversion.Replace("8.00.2039","SP4");
this.label4.Text= "The server "+this.cmbServer.Text+"'s version:\r\n";
this.label4.Text += sqlversion;
m_isconnected=true;
initdatabase();
setbtnEnable(m_isconnected);
}
catch
{
this.button1.Enabled = true;
this.label4.Text = "Connected "+ this.cmbServer.Text+" failed! \r Please check the username and password!";
m_isconnected=false;
setbtnEnable(m_isconnected);
}
#endregion
}
private void initdatabase()
{
ArrayList databases = mycmd.getDataBases();
cmbDatabase.Items.Clear();
for(int i=0 ;i<databases.Count;i++)
{
cmbDatabase.Items.Add(databases[i].ToString());
}
}
private void setbtnEnable(bool b)
{
button1.Enabled=!b;
btnQuery.Enabled=b;
btnStructure.Enabled =b;
}
private void cmbServer_TextChanged(object sender, System.EventArgs e)
{
m_isconnected=false;
setbtnEnable(m_isconnected);
}
private void btnQuery_Click(object sender, System.EventArgs e)
{
//获取连接字符串
string sqlcmd="";
if(this.cmbTableName.Text!=""&&this.cmbDatabase.Text!="")
{
connstr = connstr.Replace("master",this.cmbDatabase.Text);
if(this.txtCondition.Text==null||this.txtCondition.Text=="")
{
sqlcmd="select * from "+this.cmbTableName.Text;
}
else
{
sqlcmd="select * from "+this.cmbTableName.Text+" where "+this.txtCondition.Text;
}
try
{
sqlconn=new SqlConnection(connstr);
da = new SqlDataAdapter(sqlcmd,sqlconn);
ds.Tables.Clear();
da.Fill(ds,cmbTableName.Text);
dataGrid1.DataSource=ds.Tables[0];
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("DataBase and TableName can not be NULL!");
}
}
private void cmbDatabase_SelectedIndexChanged(object sender, System.EventArgs e)
{
mycmd.DataBaseName = this.cmbDatabase.Text;
cmbTableName.Items.Clear();
ArrayList tables = mycmd.getTables();
for(int i=0;i<tables.Count;i++)
{
cmbTableName.Items.Add(tables[i].ToString());
}
}
private void btnStructure_Click(object sender, System.EventArgs e)
{
if(cmbDatabase.Text!=""&cmbTableName.Text!="")
{
mycmd.DataBaseName = cmbDatabase.Text;
try
{
dataGrid1.DataSource=mycmd.getTableAttribute(cmbTableName.Text);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("DataBase and TableName can not be NULL!");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -