已有打开的与此命令相关联的 DataReader,必须首先将它关闭。
publicclassDBHelper{privatestaticSqlConnectionconnection;publicstaticSqlConnectionCon...
public class DBHelper
{
private static SqlConnection connection;
public static SqlConnection Connection
{
get
{
string connectionString = ConfigurationManager.ConnectionStrings["JcodeConnectionStrings"].ConnectionString;
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
//取所有记录
public static DataView GetTableData(string sqlString)
{
SqlDataAdapter da = new SqlDataAdapter(sqlString,Connection);
DataSet ds = new DataSet();
da.Fill(ds, "Jcode");
return ds.Tables["Jcode"].DefaultView;
}
/// <summary>
/// 实例化sqlCommand对象返回sqlDataReader
/// </summary>
/// <param name="safeSql"></param>
/// <returns></returns>
public static SqlDataReader GetReader(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataReader reader = cmd.ex.ExecuteReader();
return reader;
}
public static SqlDataReader GetReader(string sql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
public static int ExcuteCommand(string sql)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
int result=cmd.ExecuteNonQuery();
return result;
}
**************************
行 54: {
行 55: SqlCommand cmd = new SqlCommand(safeSql, Connection);
行 56: SqlDataReader reader = cmd.ExecuteReader();
行 57: return reader;
行 58: }
InvalidOperationException: 已有打开的与此命令相关联的 DataReader,必须首先将它关闭 展开
{
private static SqlConnection connection;
public static SqlConnection Connection
{
get
{
string connectionString = ConfigurationManager.ConnectionStrings["JcodeConnectionStrings"].ConnectionString;
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
//取所有记录
public static DataView GetTableData(string sqlString)
{
SqlDataAdapter da = new SqlDataAdapter(sqlString,Connection);
DataSet ds = new DataSet();
da.Fill(ds, "Jcode");
return ds.Tables["Jcode"].DefaultView;
}
/// <summary>
/// 实例化sqlCommand对象返回sqlDataReader
/// </summary>
/// <param name="safeSql"></param>
/// <returns></returns>
public static SqlDataReader GetReader(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataReader reader = cmd.ex.ExecuteReader();
return reader;
}
public static SqlDataReader GetReader(string sql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
public static int ExcuteCommand(string sql)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
int result=cmd.ExecuteNonQuery();
return result;
}
**************************
行 54: {
行 55: SqlCommand cmd = new SqlCommand(safeSql, Connection);
行 56: SqlDataReader reader = cmd.ExecuteReader();
行 57: return reader;
行 58: }
InvalidOperationException: 已有打开的与此命令相关联的 DataReader,必须首先将它关闭 展开
14个回答
展开全部
每次用完只读向前游标DataReader 都要关闭 reader.Close();就OK了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用完OleDr.Read()之后记得将其关闭,语句为OleDr.Close()
在你的每一个
while
(OleDr.Read())
循环结束后都加上这句OleDr.Close()
第二个方法里:
OleDbDataReader
OleDr
=
OleCmd.ExecuteReader();
OleCmd.ExecuteNonQuery();
//错在这儿
提示说
已有打开的与此命令相关联的
DataReader,必须首先将它关闭。
OleDbDataReader
OleDr
=
OleCmd.ExecuteReader();这一句有什么用,有下面这一句OleCmd.ExecuteNonQuery();
把那个OleDbDataReader
OleDr
=
OleCmd.ExecuteReader();这句删了,应该是它与下面那句冲突了
在你的每一个
while
(OleDr.Read())
循环结束后都加上这句OleDr.Close()
第二个方法里:
OleDbDataReader
OleDr
=
OleCmd.ExecuteReader();
OleCmd.ExecuteNonQuery();
//错在这儿
提示说
已有打开的与此命令相关联的
DataReader,必须首先将它关闭。
OleDbDataReader
OleDr
=
OleCmd.ExecuteReader();这一句有什么用,有下面这一句OleCmd.ExecuteNonQuery();
把那个OleDbDataReader
OleDr
=
OleCmd.ExecuteReader();这句删了,应该是它与下面那句冲突了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Connection.Close();
然后再执行后面的:
行 55: SqlCommand cmd = new SqlCommand(safeSql, Connection);
行 56: SqlDataReader reader = cmd.ExecuteReader();
行 57: return reader;
然后再执行后面的:
行 55: SqlCommand cmd = new SqlCommand(safeSql, Connection);
行 56: SqlDataReader reader = cmd.ExecuteReader();
行 57: return reader;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SqlConnection
cn
=
new
SqlConnection();
cn.ConnectionString
=
"data
source=.;initial
catalog=oa;integrated
security=sspi";
cn.Open();
SqlCommand
cmd
=
new
SqlCommand("select
是否已阅
from
Msg
where
收信人='"
+
b
+
"'
and
是否已阅
='否'",cn
);
using(SqlDataReader
dr
=
cmd.ExecuteReader())
{
try{
dr.Read();
Label1.Text
=
dr.GetString(0).ToString();
dr.Close();
Label6.Text
=
"您有新短信,请注意查收!";
}catch{}
}
con.Close();
//光给代码不知道你看得懂不....也没什么好说的,楼上几位把该说的都说完了....
cn
=
new
SqlConnection();
cn.ConnectionString
=
"data
source=.;initial
catalog=oa;integrated
security=sspi";
cn.Open();
SqlCommand
cmd
=
new
SqlCommand("select
是否已阅
from
Msg
where
收信人='"
+
b
+
"'
and
是否已阅
='否'",cn
);
using(SqlDataReader
dr
=
cmd.ExecuteReader())
{
try{
dr.Read();
Label1.Text
=
dr.GetString(0).ToString();
dr.Close();
Label6.Text
=
"您有新短信,请注意查收!";
}catch{}
}
con.Close();
//光给代码不知道你看得懂不....也没什么好说的,楼上几位把该说的都说完了....
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SqlDataReader
reader
=
GetDataReader("select
*
from
tabcity");
一定要关闭
reader.Close();
reader
=
GetDataReader("select
*
from
tabcity");
一定要关闭
reader.Close();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询