关于datareader
以下是登录的函数:string sql = "select * from user";
SqlDataReader reader= DBcontext.DbQueyr(sql); //调用了DBquery
reader.Read();
if (reader.HasRows)
{
int id=reader.GetInt32(0);
Session.Add("user_id",id);
Session.Add("username", username);
Response.Write("<script> alert('登录成功!')</script>");
}
else
{
Response.Redirect("22.aspx");
}
DBcontext.conn.Close();
DBcontext.sqlread.Close();
reader.Close();
其中数据库连接中DBcontext.DbQueyr(sql);DBcontext.adapterDbQueyr方法如下:
public static SqlDataReader DbQueyr(string sql)
{
using (sqlcom = new SqlCommand(sql, conn))
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
if (sqlread != null)
{
sqlread.Close();
}
sqlread = sqlcom.ExecuteReader();
return sqlread;
}
}
public static SqlDataAdapter adapterDbQueyr(string sql)
{
using (sqlcom = new SqlCommand(sql, conn))
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
adapter = new SqlDataAdapter(sqlcom);
return adapter;
}
}
再次调用adapterDbQueyr
SqlDataAdapter sda = DBcontext.adapterDbQueyr(sql);
DataSet ds = new DataSet();
sda.Fill(ds, "communicate");
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["communicate"].DefaultView;
datalist.DataSource = pds;
datalist.DataBind();
这是报错为:已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭。
求助高手们如何解决 ,谢谢大家了 --------------------编程问答-------------------- 高手们 来帮帮忙啊 啊 --------------------编程问答-------------------- DBcontext.conn.Close();
DBcontext.sqlread.Close();
reader.Close();
你这个datareader在使用完后就该关闭,在connection的关闭之前。
改成:
reader.Close();
DBcontext.conn.Close();
DBcontext.sqlread.Close();
--------------------编程问答-------------------- 同意楼上说法,也可以在创建Connection的时候有一个参数可以设置一下 --------------------编程问答--------------------
public static SqlDataReader DbQueyr(string sql)
{
using (sqlcom = new SqlCommand(sql, conn))
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
if (sqlread != null)
{
sqlread.Close();
}
sqlread = sqlcom.ExecuteReader();
conn.Close();//用完关闭掉
return sqlread;
}
}
DBcontext.conn.Close();
DBcontext.sqlread.Close();
reader.Close();
改成
reader.Close();
DBcontext.sqlread.Close();
DBcontext.conn.Close();
--------------------编程问答-------------------- 什么参数?? --------------------编程问答-------------------- conn.Close();//用完关闭掉
错了不要这个。 --------------------编程问答--------------------
reader.Close();
DBcontext.conn.Close();
DBcontext.sqlread.Close();
使用一个 关闭一个
--------------------编程问答-------------------- 和关闭顺序有关系么??试了一下 好像不行 --------------------编程问答-------------------- 看一下官网的例子
private static void ReadOrderData(string connectionString)
{
string queryString =
"SELECT OrderID, CustomerID FROM dbo.Orders;";
using (SqlConnection connection =
new SqlConnection(connectionString))
{
SqlCommand command =
new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
// Call Read before accessing data.
while (reader.Read())
{
Console.WriteLine(String.Format("{0}, {1}",
reader[0], reader[1]));
}
// Call Close when done reading.
reader.Close();
}
}
http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqldatareader%28VS.80%29.aspx
补充:.NET技术 , ASP.NET