根据平常的思维你应该在之前打开,然后判断它的状态。
测试代码如下:
1 连接数据库try{
String commandText="select count(*) from users";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=(local);Initial
Catalog=classmate;User Id=sa;Password=sa";
SqlCommand cmd = new SqlCommand(commandText,conn);
conn.Open();
string count = cmd.ExecuteScalar().ToString();
if (conn.State == ConnectionState.Open){
Response.Write(count);
Response.Write("数据库连接成功!");
Response.Write("