public string str;
private void btnConnect_Click(object sender, EventArgs e) //按钮单击事件
{
try //try....catch捕获异常
{
str = @"server=" + txtA.Text + ";" + "database=" + txtB.Text + ";" + "uid=" + txtC.Text + ";" + "pwd=" + txtD.Text; //获取四个文本框中的
SqlConnection cnn = new SqlConnection(); //实例化SqlConnection
{
MessageBox.Show("数据库已经打开", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show("数据库没有打开", "提示");
}
}
private void btnConnect_Click(object sender, EventArgs e) //按钮单击事件
{
try //try....catch捕获异常
{
str = @"server=" + txtA.Text + ";" + "database=" + txtB.Text + ";" + "uid=" + txtC.Text + ";" + "pwd=" + txtD.Text; //获取四个文本框中的
SqlConnection cnn = new SqlConnection(); //实例化SqlConnection
cnn.ConnectionString = str; //连接数据库
cnn.Open(); //打开数据库
if (cnn.State == ConnectionState.Open) //如果数据库的状态是打开的{
MessageBox.Show("数据库已经打开", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show("数据库没有打开", "提示");
}
}