我正在尝试运行此代码
public Exception SetData(string Data , long NoOfColumnsAllowed)
{
try
{
con = new SqlCeConnection(conectionstring);
con.Open();
transaction = con.BeginTransaction();
com = new SqlCeCommand();
com.Transaction = transaction;
com.CommandText = "Select count(*) from [Copy]";
com.Connection = con;
sdr = com.ExecuteReader();
while (sdr.Read())
{
noOfColumns = sdr.GetInt32(0);
}
if (noOfColumns > NoOfColumnsAllowed)
{
long NoOfColumsToBeDeleted = noOfColumns - NoOfColumnsAllowed;
com.CommandText = "delete from [Copy] where Sno<=@sno";
com.Parameters.AddWithValue("@sno", NoOfColumsToBeDeleted);
com.ExecuteNonQuery();
}
com.CommandText = "Insert into [Copy] (Data) values (@data)";
com.Parameters.AddWithValue("@data", Data);
com.ExecuteNonQuery();
transaction.Commit();
con.Close();
return null;
}
catch (Exception ex)
{
try
{
transaction.Rollback();
}
catch (Exception)
{
}
con.Close();
return ex;
}
}
发生异常 -
system.invalidoperationexception:如果在此事务的范围内有任何打开的游标,则无法提交该事务。确保在提交更改之前明确关闭所有数据读取器/结果集。
我是事务新手,无法找到有关打开游标的任何有价值的解决方案。代码有问题吗,或者我必须明确关闭数据阅读器,如果是,请告诉我怎么做?