C# 中为何 显示 ExecuteReader 要求已打开且可用的连接。连接的当前状态为已关闭。 5
SqlConnectionconn=null;SqlCommandcomm=null;stringstrConnection;//boolflag=false;strin...
SqlConnection conn = null;
SqlCommand comm = null;
string strConnection;
// bool flag = false;
string userId = TextBox1.Text;
string password = TextBox2.Text;
try
{
strConnection = "server=(local);database=HRManager;integrated security=SSPI";
conn.Open();
conn = new SqlConnection();
conn.ConnectionString = strConnection;
// conn.Open();
}
catch
{
Label2.Text = "连接数据库失败!";
}
string sql = "select * from [user] where userId='" + TextBox1.Text.Trim() + "' and password='" + TextBox2.Text.Trim() + "'";
comm = new SqlCommand(sql, conn);
// comm.CommandText = sql;
SqlDataReader reader = comm.ExecuteReader();
if (reader.HasRows == true)
{
Response.Redirect("main.aspx");
}
else
{
// Label3 = "用户不存在!";
} 展开
SqlCommand comm = null;
string strConnection;
// bool flag = false;
string userId = TextBox1.Text;
string password = TextBox2.Text;
try
{
strConnection = "server=(local);database=HRManager;integrated security=SSPI";
conn.Open();
conn = new SqlConnection();
conn.ConnectionString = strConnection;
// conn.Open();
}
catch
{
Label2.Text = "连接数据库失败!";
}
string sql = "select * from [user] where userId='" + TextBox1.Text.Trim() + "' and password='" + TextBox2.Text.Trim() + "'";
comm = new SqlCommand(sql, conn);
// comm.CommandText = sql;
SqlDataReader reader = comm.ExecuteReader();
if (reader.HasRows == true)
{
Response.Redirect("main.aspx");
}
else
{
// Label3 = "用户不存在!";
} 展开
2个回答
展开全部
try
{
strConnection = "server=(local);database=HRManager;integrated security=SSPI";
conn.Open();
conn = new SqlConnection();
conn.ConnectionString = strConnection;
// conn.Open();
}
你把第一个conn.open()去掉,因为这个时候你还没有初始化conn呢~~
{
strConnection = "server=(local);database=HRManager;integrated security=SSPI";
conn.Open();
conn = new SqlConnection();
conn.ConnectionString = strConnection;
// conn.Open();
}
你把第一个conn.open()去掉,因为这个时候你还没有初始化conn呢~~
追问
我原本是用的下面那个 conn.Open();
,可是有人说要放上面 ,可是不管放哪边都是一样 都显示的同样的问题
展开全部
conn.Open();
conn = new SqlConnection();
conn.ConnectionString = strConnection;
顺序有问题
conn.Open();放下边试试
conn = new SqlConnection();
conn.ConnectionString = strConnection;
顺序有问题
conn.Open();放下边试试
追问
放下面了的 ,可是他还是这句有问题 “SqlDataReader reader = comm.ExecuteReader();”
ExecuteReader 要求已打开且可用的连接。连接的当前状态为已关闭。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询