Stu*_*art 2 c# asp.net connection sqlcommand sqldatareader
我有以下代码.
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
connection.Open();
SqlCommand select = new SqlCommand("SELECT RTRIM(LTRIM(PART_NO)) AS PART_NO, record FROM [RMAData].[dbo].[IMPORTING_ORDER_EDI] WHERE sessionID = '" + Session.SessionID + "'", connection);
SqlDataReader reader = select.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
if (!currentPart.IsActive)
{
// this part is not active, set the active flag in sql to 0
SqlCommand update = new SqlCommand("UPDATE [RMAData].[dbo].[IMPORTING_ORDER_EDI] SET valid = 0, active = 0 WHERE record = " + reader["record"].ToString() + ";", connection);
update.ExecuteNonQuery();
}
else
{
///blah
}
}
reader.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
但这会导致以下异常......
System.InvalidOperationException:已经有一个与此命令关联的打开DataReader,必须先关闭它.
我需要读取返回的每一行,对数据进行一些验证并在必要时进行更新,然后继续下一条记录.如果我不能使用SqlCommandwhile循环,我怎样才能实现这一目标reader.Read()?