C# microsoftvisualstudio中的SQL查询
在上面的代码中,where子句在select查询中不起作用C# microsoftvisualstudio中的SQL查询,c#,sql-server,C#,Sql Server,在上面的代码中,where子句在select查询中不起作用 public Availableopt(ref SqlConnection nsc, String ac, String nonac, String sit1, String sleep1, String fromopt, String toopt) { InitializeComponent(); // String s = "select * from Available where From =
public Availableopt(ref SqlConnection nsc, String ac, String nonac, String sit1, String sleep1, String fromopt, String toopt)
{
InitializeComponent();
// String s = "select * from Available where From = @fromopt ";
SqlCommand ncd = new SqlCommand("select * from Available", nsc);
SqlDataAdapter sdan1 = new SqlDataAdapter();
sdan1.SelectCommand = ncd;
DataTable db = new DataTable();
sdan1.Fill(db);
BindingSource bs = new BindingSource();
bs.DataSource = db;
grid.DataSource = bs;
sdan1.Update(db);
}
行中有一个SqlException
select * from Available where from = 'ahmedabad';
除了where子句之外,SQL查询工作得非常好。From是SQL中的一个关键字。如果您有一个名称不好的列,您可以使用:
sqldataadapter.fill(datatable)
您还应该@fromopt:
请始终包含您收到的错误/异常消息,而不是存在某些sqlexception。从你的代码中,我不会总是像在这种情况下那么明显地看出哪里出了问题。好吧!下次我会注意的。不管怎样,谢谢你谢谢Hanks@Kobi。它现在正在工作,很抱歉在提问时出错了。下次我会小心的。
select * from Available where [From] = @fromopt
ncd.Parameters.AddWithValue("@fromopt", fromopt);