从C#中选择SQL Server数据库中的特定记录

时间:2012-01-02 16:58:34

标签: c# .net sql sql-server windows

我目前正在尝试使用符合以下条件的C#从SQL Server数据库中获取一些行:

  • 来自RamResults数据库
  • Results
  • 其中Date列等于当前日期

到目前为止,我有以下内容:

// Open the same connection with the same connection string.
using (SqlCeConnection con = new SqlCeConnection(DatabaseControl.conString))
{
   con.Open();
   // Read specific values in the table.
   using (SqlCeCommand com = new SqlCeCommand("SELECT Result FROM RamResults WHERE Date == @Form1.date", con))
   {
      SqlCeDataReader reader = com.ExecuteReader();
      while (reader.Read())
      {
         int resultsoutput = reader.GetInt32(0);
         MessageBox.Show(resultsoutput.ToString());
      }
   }
}

使用SELECT Result FROM RamResults WHERE Date == Form1.date会抛出错误:

  

解析查询时出错。 [令牌行号= 1,令牌   line offset = 43,Token in error = =]

虽然如果我拿出WHERE语句,例如

SELECT Result FROM RamResults

它完美无缺

9 个答案:

答案 0 :(得分:6)

描述

2件事

  1. 使用=代替==,因为这是T-SQL中的正确等号运算符。 您的查询应该是这样的

    SELECT Result FROM RamResults WHERE Date = @Date

  2. 您忘记传入参数。

  3. 示例

    // Open the same connection with the same connection string.
    using (SqlCeConnection con = new SqlCeConnection(DatabaseControl.conString))
    {
        con.Open();
        // Read specific values in the table.
        using (SqlCeCommand com = new SqlCeCommand("SELECT Result FROM RamResults WHERE Date = @Date", con))
        {
            com.Parameters.AddWithValue("@Date", Form1.date);
            SqlCeDataReader reader = com.ExecuteReader();
            while (reader.Read())
            {
                int resultsoutput = reader.GetInt32(0);
                MessageBox.Show(resultsoutput.ToString());
            }
        }
    }
    

答案 1 :(得分:5)

尝试参数化查询并将==替换为=子句中的WHERE

// ...
using (SqlCeCommand com = 
    new SqlCeCommand("SELECT Result FROM RamResults WHERE Date = @date", con))
{
    com.Parameters.Add(new SqlParameter("date", Form1.date));
    // ...
}
// ...

答案 2 :(得分:2)

试试这个:

using (SqlCeCommand com = new SqlCeCommand("SELECT Result FROM RamResults WHERE Date = @date", con))
    {
        com.Parameters.AddWithValue("date",Form1.date);
        SqlCeDataReader reader = com.ExecuteReader();
        while (reader.Read())

始终使用SQL参数而不是字符串连接。

答案 3 :(得分:1)

不是==,请在SQL中使用=等式:

WHERE Date = @Form1.date

答案 4 :(得分:1)

运算符“==”是SQL的无效语法。在where子句中使用单个等号“=”。

答案 5 :(得分:1)

尝试

var query = "SELECT Result FROM RamResults WHERE Date = " + Form1.date;
using (SqlCeCommand com = new SqlCeCommand(query, con))

我建议使用此example on MSDN

中的参数

答案 6 :(得分:1)

使用以下内容替换SQL查询:

SELECT Result 
FROM RamResults 
WHERE Date like DATEADD(day, DATEDIFF(day, 0,  getdate()), 0)

希望这有效。

答案 7 :(得分:0)

这很令人困惑,许多人犯了这些错误。虽然C#使用==进行相等操作(确定我们也有Equal()),但SQL只使用=。

除此之外,你也忘了在这里传递参数

答案 8 :(得分:0)

我在你的代码中发现了两个产生问题的东西

1)为参数赋值 2)==而不是=(只是为了使它适用于SQL)

所以代码应该是这样的:

using (SqlCeConnection con = new SqlCeConnection(DatabaseControl.conString))
    {
    con.Open();
    using (SqlCeCommand com = new SqlCeCommand("SELECT Result FROM RamResults WHERE Date = @Date", con))
    {
        com.Parameters.AddWithValue("@Date", Form1.date);
        SqlCeDataReader reader = com.ExecuteReader();
        while (reader.Read())
        {
            int resultsoutput = reader.GetInt32(0);
            MessageBox.Show(resultsoutput.ToString());
        }
    }
}