我在尝试在C#应用中打开表单时遇到此错误,但我不知道可能出现的问题。错误出现在这行代码DataTable dt = new DataTable(); dt.Load(cmd.ExecuteReader());
错误:
{"'附近的语法不正确,'。"}
public void popTraseu()
{
string cs = "Data Source=CODRINMA\\CODRINMA;Initial Catalog=TrafficManager;Integrated Security=True";
string select = "ats.NrOrdine, a.IDAutogara, a.Denumire as Autogara, o.Oras as Oras, o.Judet FROM Curse c INNER JOIN Trasee t on c.IDTraseu=t.IDTraseu INNER JOIN AutogariTrasee ats on t.IDTraseu=ats.IDTraseu INNER JOIN Autogari a on ats.IDAutogara=a.IDAutogara INNER JOIN Orase o on a.IDOras=o.IDOras where IDCursa=@IDCursa ORDER BY ats.NrOrdine";
try
{
using (SqlConnection con = new SqlConnection (cs))
{
con.Open();
SqlCommand cmd = new SqlCommand(select, con);
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("IDCursa", int.Parse(grdCurse.CurrentRow.Cells[0].FormattedValue.ToString()));
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
con.Close();
grdDetalii.DataSource = dt;
}
}
catch (Exception er) { MessageBox.Show(er.Message); }
答案 0 :(得分:6)
您似乎忘了将select
放在查询的开头。
string select = "select ats.NrOrdine, a.IDAutogara, a.Denumire as Autogara, o.Oras as Oras, o.Judet FROM Curse c INNER JOIN Trasee t on c.IDTraseu=t.IDTraseu INNER JOIN AutogariTrasee ats on t.IDTraseu=ats.IDTraseu INNER JOIN Autogari a on ats.IDAutogara=a.IDAutogara INNER JOIN Orase o on a.IDOras=o.IDOras where IDCursa=@IDCursa ORDER BY ats.NrOrdine";
答案 1 :(得分:2)
您的选择不是以SELECT开头的。因此,SQL语句的语法无效。
还要注意这一行:
cmd.Parameters.Clear();
完全是多余的。新实例中没有参数。