0

在我的 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 部分中的每个参数都应该在每一侧都有单引号。如何修改我的代码以添加此代码,或者这是导致我的错误的原因?

4

1 回答 1

3

你说得对,需要为 SQL Server 引用字符串值。

string.Join(", ", parameters)

有很多选择,但如果这是唯一的问题,这将起作用

"'"+string.Join(",", parameters).Replace(",", "','")+"'"

但是,您的代码中的错误更为广泛。您试图参数化每个项目 - 这不起作用,因为您在 CommandText 中没有指定占位符。您所拥有的只是静态IN列表。您还拥有比所需更多的 SqlConnection 实例。

        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));
        using (SqlConnection conn = new SqlConnection(strSQLconnection))
        {
            SecondQuery.CommandTimeout = 50000;
            conn.Open();
            SecondQuery.Connection = conn;

            using (var reader7 = SecondQuery.ExecuteReader())
            {
                int fieldCount = reader7.FieldCount;

                while (reader7.Read())
                {
                    for (int i = 0; i < fieldCount; i++)
                    {
                        finalresults.Add(reader7[i].ToString());
                    }
                }
            }
        }
于 2013-04-26T00:21:31.367 回答