日期:2014-05-17 浏览次数:21284 次
private static SqlConnection mySqlConnection;
private static string ConnectionString="";
private string ConnectionStringFunction()//根据文本框内的参数值,返回连接字符串
{
return "server=" + this.textBox1.Text + ";database=" + this.textBox2.Text + ";uid=" + this.textBox3.Text + ";pwd=" + this.textBox4.Text + ";Asynchronous Processing=True;Connection Timeout=1;";
}
private void checkLinkBtn_Click(object sender, EventArgs e)//测试数据库是否连接成功按钮
{
ConnectionString = ConnectionStringFunction();
mySqlConnection = new SqlConnection(ConnectionString);
try
{
mySqlConnection.Open();
}
catch
{
//TEST
messageBox.show("失败");
}
finally
{
if (mySqlConnection.State == ConnectionState.Closed || mySqlConnection.State == ConnectionState.Broken)
{
this.label5.Text = "连接失败";
}
else
{
this.label5.Text = "连接成功";
}
mySqlConnection.Close();
}
}