没有装微软MySQL Server装了Navicat建立数据库连接都没有问题,在VS中想用C#连接数据库,结果连接不了
privatevoidbutton4_Click(objectsender,EventArgse){stringstrConnection="Server=localho...
private void button4_Click(object sender, EventArgs e)
{
string strConnection ="Server=localhost;";
strConnection += "initial catalog=asd;";
strConnection += "user id=root;";
strConnection += "password=121016;";
strConnection += "Connect Timeout=5";
bool CanConnectDB = false;
using (SqlConnection objConnection = new SqlConnection(strConnection)) {
try
{
objConnection.Open();
CanConnectDB = true;
objConnection.Close();
}
catch { }
if (CanConnectDB) MessageBox.Show("数据库连接成功");
else MessageBox.Show("数据库连接失败!");
}
}
}
} 展开
{
string strConnection ="Server=localhost;";
strConnection += "initial catalog=asd;";
strConnection += "user id=root;";
strConnection += "password=121016;";
strConnection += "Connect Timeout=5";
bool CanConnectDB = false;
using (SqlConnection objConnection = new SqlConnection(strConnection)) {
try
{
objConnection.Open();
CanConnectDB = true;
objConnection.Close();
}
catch { }
if (CanConnectDB) MessageBox.Show("数据库连接成功");
else MessageBox.Show("数据库连接失败!");
}
}
}
} 展开
1个回答
2015-07-13 · 知道合伙人软件行家
关注
展开全部
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询