我目前正在尝试使用 C# 从 SQL Server 数据库中获取一些符合以下条件的行:
- 来自
RamResults
数据库 - 在
结果
表中 - 其中
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
会引发错误:
There was an error parsing the query. [ Token line number = 1,Token line offset = 43,Token in error = = ]
虽然如果我取出 WHERE 语句,例如
SELECT Result FROM RamResults
完美运行
最佳答案
描述
2 件事
使用
=
而不是==
因为这是T-SQL
中的正确等号运算符。 你的查询应该是这样的从 RamResults 中选择结果,其中 Date = @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 = @Date", con))
{
com.Parameters.AddWithValue("@Date", Form1.date);
SqlCeDataReader reader = com.ExecuteReader();
while (reader.Read())
{
int resultsoutput = reader.GetInt32(0);
MessageBox.Show(resultsoutput.ToString());
}
}
}
关于c# - 从 C# 中选择 SQL Server 数据库中的特定记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8703352/