在我的 C# MVC4 应用程序中,我有以下代码:
string[] parameters = new string[items.Count];
SqlCommand SecondQuery = new SqlCommand();
for (int i = 0; i < items.Count; i++)
{
parameters[i] = string.Format(items[i].Id);
SecondQuery.Parameters.AddWithValue(parameters[i], items[i]);
}
SecondQuery.CommandText = string.Format("SELECT * from S_analysis WHERE heat_no IN ({0})", string.Join(", ", parameters));
SecondQuery.Connection = new SqlConnection(strSQLconnection);
using (SqlConnection conn = new SqlConnection(strSQLconnection))
{
SecondQuery.CommandTimeout = 50000;
conn.Open();
SecondQuery.Connection = new SqlConnection(strSQLconnection);
SecondQuery.Connection.Open();
using (var reader7 = SecondQuery.ExecuteReader())
{
int fieldCount = reader7.FieldCount;
while (reader7.Read())
{
for (int i = 0; i < fieldCount; i++)
{
finalresults.Add(reader7[i].ToString());
}
}
}
}
我的应用程序在线崩溃:using (var reader7 = SecondQuery.ExecuteReader())
出现此错误:No mapping exists from object type <>f__AnonymousType42[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] to a known managed provider native type.
我假设这是因为当我查看我的 SecondQuery.CommandText 时,我的查询看起来像:SecondQuery.CommandText = "SELECT * from S_analysis WHERE heat_no IN (B5P5649, B5P5647, B5P5656, A5P0761, A5P0762)"
我相信代码的 IN 部分中的每个参数都应该在每一侧都有单引号。如何修改我的代码以添加此代码,或者这是导致我的错误的原因?