【问题标题】:SQL Server select query execution from c#SQL Server 从 C# 中选择查询执行
【发布时间】:2012-12-16 17:54:43
【问题描述】:
string user = "1234";
string strSQL = string.Format("Select * From User where UserId = '{0}'",user);
SqlCommand myCommand = new SqlCommand(strSQL, cnn);
reader = myCommand.ExecuteReader();

我的User 表由UserIdPassword 列组成。 UserId 列类型是 nchar,所以我使用了单引号。我收到一个错误提示

关键字“用户”附近的语法不正确

(我猜这里引用了表名User)。

我有正确的连接字符串和其他与数据库环境相关的东西,因为我已经检查了数据库连接状态并且它是打开的(在程序执行期间)。

语法有什么错误?我无法从我的表中检索行。

【问题讨论】:

  • 在你的 SQL 命令行上打一个断点并获取 strSQL 的值,然后复制字符串值并将其粘贴到 SSMS 中。执行并检查查询。如果没有表格的设计,就很难诊断出语法错误是什么。

标签: c# sql-server select


【解决方案1】:

你应该用括号[]包裹user

string strSQL = string.Format("Select * From [User] where UserId = '{0}'",user);

上面的查询容易受到SQL Injection 的攻击。应该对其进行参数化以避免这种情况。下面是一个例子:

string user = "1234";
string strSQL = "Select * From [User] where UserId = @userID";
SqlCommand myCommand = new SqlCommand(strSQL, cnn);
myCommand.AddWithValue("@userID", user);
reader = myCommand.ExecuteReader();

使用下面的

  • Try-Catch 阻止正确捕获错误
  • using 正确处理对象的声明

sn-p:

string user = "1234";
string strSQL = "Select * From [User] where UserId = @userID";
using (SqlConnection cnn = new SqlConnection("connection string here"))
{
    using (SqlCommand myCommand = new SqlCommand(strSQL, cnn))
    {
        myCommand.Parameters.AddWithValue("@userID", user);
        using (SqlDataReader reader = myCommand.ExecuteReader())
        {
            while (reader.Read())
            {
                Console.WriteLine(reader["columnName"].ToString());
            }
        }
    }
}

【讨论】:

  • 我认为您的代码中有一些错误。 “myCimmand”拼写错误,看起来你应该使用括号时使用了括号,也许?我不确定你在第 4 行做了什么?
【解决方案2】:

User 是一个关键字。在它周围使用方括号以避免错误。 Select * from [User]

string strSQL = string.Format("Select * From [User] where UserId = '{0}'",user);

此外,您应该始终使用如下参数化查询来防止 SQL 注入攻击:

string strSQL = string.Format("Select * From [User] where UserId = @UserId");

【讨论】:

    【解决方案3】:

    [] 包裹。它是一个关键字。阅读来自 MSDN 的 Reserved Keywords 文章。

    string strSQL = string.Format("Select * From [User] where UserId = '{0}'",user);
    

    但更重要的是,您的查询对SQL Injection 攻击开放。您应该始终使用参数化查询。

    string strSQL = "Select * From [User] where UserId = @userID";
    SqlCommand myCommand = new SqlCommand(strSQL, cnn);
    myCommand.Parameters.AddWithValue("@userID", user);
    

    【讨论】:

      【解决方案4】:

      你真的应该为此使用参数:

      string user = "1234";
      
      using (SqlCommand command = new SqlCommand("select * from [User] where UserId = @userid", cnn))
      {
          command.Parameters.AddWithValue("@userid", user);
      
          using (SqlDataReader reader = myCommand.ExecuteReader())
          {
              // iterate your results here
          }
      }
      

      被其他发帖人发现了,我从来没有用你的表名找到保留词。我已经修改了我的答案 - 但不能因为错过了显而易见的事情而受到赞扬!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-14
        • 2020-07-31
        • 2015-10-28
        • 1970-01-01
        相关资源
        最近更新 更多