System.Data.SqlClient.SqlException(0x80131904):列名无效

时间:2016-01-17 13:45:35

标签: sql asp.net asp.net-mvc ado.net

我在尝试执行附加代码时遇到此错误。请帮忙。我成功地在控制器方法中获取值作为列表数组但是当我尝试从sql数据库中读取数据时,我收到错误。

控制器方法:

[HttpPost]
        public JsonResult searchdata(List<string> firstField, List<string> secondField, List<string> thirdField)
        {
            List<string> lt = new List<string>();
            Console.WriteLine(firstField);
            Console.WriteLine(secondField);
            string dogCsv = string.Join(",", firstField.ToArray());
            Console.WriteLine(dogCsv);
            try
            {
                using (SqlConnection connection = new SqlConnection("data source=.; database=Srivatsava; integrated security=SSPI"))
                {
                    connection.Open();
                    using (SqlCommand command = connection.CreateCommand())
                    {
                        command.CommandTimeout = 0;
                        command.CommandText = @"select accntname,BU,salesop,isdormant from fourth_page as fg
                                  INNER JOIN linked as ld on ld.productid=fg.productid 
                                   INNER JOIN isdormant as it on it.productid=ld.productid 
                                   where fg.accountname in (" + dogCsv + ")";
                        //command.Parameters.AddWithValue("firstField", dogCsv);
                        using (SqlDataReader sdr = command.ExecuteReader())
                        {
                            while (sdr.Read())
                            {

                                lt.Add(sdr["accntname"].ToString());
                            }
                        }
                    }
                    connection.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            Console.WriteLine(lt);
            return Json(firstField);

        }

Execption: -

{System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'account2'.
Invalid column name 'account1'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader()
   at MvcApplication32.Controllers.TodoListController.searchdata(List`1 firstField, List`1 secondField, List`1 thirdField) in c:\Users\ADMIN\Documents\Visual Studio 2012\Projects\MvcApplication32\MvcApplication32\Controllers\TodoListController.cs:line 597
ClientConnectionId:a0674fe4-e0d5-499f-be61-66e1b27768fa}

1 个答案:

答案 0 :(得分:1)

由于您使用的是内联SQL,因此dogCSV字符串中逗号分隔的每个值都需要围绕它的单引号。例如,如果dogCSV有"account2,someotheraccount",则无法使用"'account2','someotheraccount'"

如果你不这样那肯定是问题。

编辑:像这样建立dogCSV:

string dogCsv = string.Join(",", firstField.Select(i => "'" + i + "'"));

现在它的构建方式不会引用它。