注意: 如果填写地址 是指定端口 IP地址与端口之间需要用逗号隔开。

 

private void button2_Click(object sender, EventArgs e)

{
string strdata = textBox1.Text;
string strsa = textBox2.Text;
string strpwd = textBox3.Text;

SqlConnectionStringBuilder sqlb01 = new SqlConnectionStringBuilder();

sqlb01.DataSource = strdata;

sqlb01.InitialCatalog = "random";
sqlb01.UserID = strsa;
sqlb01.Password = strpwd;
sqlb01.IntegratedSecurity = false;
sqlb01.Pooling = true;
SqlConnection conn01 = new SqlConnection();
conn01.ConnectionString = sqlb01.ConnectionString;
MessageBox.Show(sqlb01.ConnectionString);

try
{
conn01.Open();
if (conn01.State.ToString() == "Open")
{
MessageBox.Show("数据库链接成功!");


conn01.Close();
}

}

catch(SqlException sqlex)
{
MessageBox.Show("数据库配置错误,请重新填写//n");
MessageBox.Show(sqlex.ToString());

}



}

posted on 2024-04-20 19:21  Lionever  阅读(3)  评论(0编辑  收藏  举报