如果manage_score返回true,则调用函数Update。 在Update中的这一行(System.Data.SqlClient.SqlDataReader oReader3 = cmd2.ExecuteReader()。我收到此错误(已经有一个与此命令关联的打开的DataReader必须先关闭。)我删除了一些行代码阅读时间不长。 Manage_score:如果尚未插入新条目,则插入新条目。 更新:如果条目已经存在,则调用它,并在更新之前进行一些计算。
谢谢
public static bool manage_score(String City, String Street, String No, int Clean, int Fun, int Study, bool recommend)
{
System.Data.SqlClient.SqlConnection conn=null;
System.Data.SqlClient.SqlCommand cmd = null; ;
System.Data.SqlClient.SqlDataReader rdr=null;
bool needUpdate=false;
try
{
using( conn = new System.Data.SqlClient.SqlConnection(dbConn))
{
using(cmd = new System.Data.SqlClient.SqlCommand(
"Insert Into c_Show (Street,City,No,Count,Clean,Fun,Study,CleanScore,FunScore,StudyScore,Like_,Dislike) " +
"VALUES (@Street,@City,@No,'1',@Clean,@Fun,@Study,@Clean,@Fun,@Study,@Like,@DisLike)", conn))
{
cmd.Parameters.Add("@No", System.Data.SqlDbType.Int).Value = No;
cmd.Parameters.Add("@City", System.Data.SqlDbType.Char).Value = City;
cmd.Connection.Open();
using (rdr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)) { }
}
}
}
catch (System.Data.SqlClient.SqlException ex)
{
if (ex.Number == 2627)// duplicate key
{
needUpdate=true;
}
}
catch (System.Exception ex) { }
return needUpdate;
}
static public void update(String City, String Street, String No, int Clean, int Fun, int Study, bool recommend)
{
using (System.Data.SqlClient.SqlConnection conn2 = new System.Data.SqlClient.SqlConnection(dbConn))
{
String resCount = null;
String resClean = null;
String resFun = null;
String resStudy = null;
String resLike = null;
String resDislike = null;
using (System.Data.SqlClient.SqlCommand cmd2 = new System.Data.SqlClient.SqlCommand(
"SELECT Street,City,No,Count,CleanScore,FunScore,StudyScore,Like_,Dislike FROM c_Show " +
"Where City=@City and Street=@Street and No=@No ", conn2))
{
cmd2.Parameters.Add("@No", System.Data.SqlDbType.Int).Value = No;
cmd2.Parameters.Add("@City", System.Data.SqlDbType.Char).Value = City;
cmd2.Parameters.Add("@Street", System.Data.SqlDbType.NVarChar).Value = Street;
cmd2.Connection.Open();
cmd2.ExecuteReader();
using (System.Data.SqlClient.SqlDataReader oReader3 = cmd2.ExecuteReader())
{}
}
}
}
答案 0 :(得分:0)
根据您的代码,您错误地调用了cmd2.ExecuteReader()
两次。
cmd2.ExecuteReader(); // once. You probably want to remove this one?
using (System.Data.SqlClient.SqlDataReader oReader3 = cmd2.ExecuteReader()) // twice
{
我认为第一次通话是个错误。删除它,你应该没事。