列名无效 [英] invalid column name

查看:156
本文介绍了列名无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用(SqlConnection connection =  new  SqlConnection(connectionString)) 
{


string str = < span class =code-string> SELECT Department,Name FROM View_3 Where CompanyName = + Class1.Text1;



dba.setDataReader(str);


public SqlDataReader setDataReader( string sSQL)
{
SqlCommand cmd = new SqlCommand(sSQL,conn,transaction);
cmd.CommandTimeout = 300 ;
SqlDataReader rtnReader;
rtnReader = cmd.ExecuteReader();
return rtnReader;
}

解决方案

在不知道你的表格结构的情况下,很难确定精确的列,但是至少有一个查询中的列名与表中的列名不匹配:

-Department

-Name

-CompanyName



此外,您永远不应该将值直接连接到SQL语句。这将使您对SQL注入,转换问题等持开放态度。而是使用 SqlParameter [ ^

using (SqlConnection connection = new SqlConnection(connectionString))
        {


            string str = "SELECT Department,Name FROM View_3 Where CompanyName=" + Class1.Text1;



            dba.setDataReader(str);


   public SqlDataReader setDataReader(string sSQL)
        {
            SqlCommand cmd = new SqlCommand(sSQL, conn, transaction);
            cmd.CommandTimeout = 300;
            SqlDataReader rtnReader;
            rtnReader = cmd.ExecuteReader();
            return rtnReader;
        }

解决方案

Without knowing your table structure, it's hard to pinpoint the exact column but either way at least one of the column names in your query does not match column names in your table:
-Department
-Name
-CompanyName

Also, you should never ever concatenate values directly to the SQL statement. This would leave you wide open to SQL injections, conversion problems and so on. Instead use
SqlParameter[^]


这篇关于列名无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆