【发布时间】:2017-07-08 23:42:48
【问题描述】:
我通过单击行从用户那里获取行号。然后我将该数字(由用户获取)存储在一个整数中。他们我正在应用动态查询从数据库中获取相关数据。但它不起作用我的查询如下 “从 ID = x 的 project1 中选择标题” 其中 ID 和 titlw 是我的列的名称,project1 是表的名称。
这是我的 C# 代码
private void letsee()
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("select Title from project1 where ID = x", conn))
{
// create a SQL parameter to add them to the command - this will limit the results to the single user
SqlParameter p = new SqlParameter("Title", System.Data.SqlDbType.Text);
p.Value = 1;
cmd.Parameters.Add(p);
// as we are only selecting one column and one row we can use ExecuteScalar
string connType = cmd.ExecuteReader().ToString();
//to display on the frontend
NEW.InnerText = connType;
}
}
【问题讨论】:
-
ID 是整数还是字符串?参数必须有“@”。所以你应该有 "select @Title...." 和 SqlParameter("@Title"....
-
ExecuteReader.ToString() 不是你想的那样。
-
这真的是 MySQL 吗?因为如果是这样,您使用了错误的提供程序(
SqlCommand,SqlParameter)。您需要使用MySqlCommand等。
标签: c# mysql sql asp.net database