私の 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 部分内の各パラメーターには、両側に一重引用符が必要だと思います。コードを変更してこれを追加するにはどうすればよいですか、またはこれがエラーの原因ですか?