本文介绍了System.Data.SqlClient.SqlException:'='附近的语法不正确.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到以下错误.System.Data.SqlClient.SqlException: Incorrect syntax near ''=''.
是什么原因导致此错误?
拜托,有人可以帮我吗?
我的代码如下.
Hi,
I am getting the below error.System.Data.SqlClient.SqlException: Incorrect syntax near ''=''.
What could cause this Error?
Please, can anyone help me?
My code is below.
protected void btnLocate_Click(object sender, EventArgs e)
{
SqlConnection conDatabase = null;
try
{
conDatabase = new SqlConnection("server=.;Initial Catalog=harika;Integrated Security=True");
conDatabase.Open();
SqlCommand command = new SqlCommand("SELECT * FROM dbo.cars" +
"WHERE TagNumber = @TagNbr;", conDatabase);
command.Parameters.Add("@TagNbr", SqlDbType.VarChar);
command.Parameters["@TagNbr"].Value = txttag.Text;
DataSet dsCars = new DataSet("CarsSet");
SqlDataAdapter sdaCars = new SqlDataAdapter();
sdaCars.SelectCommand = command;
//command.CommandText = sdaCars.SelectCommand;
sdaCars.Fill(dsCars);
try
{
DataRow recCar = dsCars.Tables[0].Rows[0];
if (recCar.IsNull("CarID"))
throw new IndexOutOfRangeException("Invalid Tag Number");
txtmake.Text = (string)recCar["Make"];
txtmodel.Text = (string)recCar["Model"];
txtyear.Text = (string)recCar["CarYear"];
chkk7.Checked = (bool)recCar["HasK7Player"];
chkcd.Checked = (bool)recCar["HasCDPlayer"];
chkdvd.Checked = (bool)recCar["HasDVDPlayer"];
chkavail.Checked = (bool)recCar["Available"];
}
catch (IndexOutOfRangeException)
{
txttag.Text = "";
txtmake.Text = "";
txtmodel.Text = "";
txtyear.Text = "";
chkk7.Checked = false;
chkcd.Checked = false;
chkdvd.Checked = false;
chkavail.Checked = false;
}
}
finally
{
conDatabase.Close();
}
}
public SqlCommand Command { get; set; }
}
}
推荐答案
SqlCommand command = new SqlCommand("SELECT * FROM dbo.cars "
+ "WHERE TagNumber = @TagNbr", conDatabase);
SELECT * FROM dbo.cars" +
"WHERE TagNumber = @TagNbr;
去除 ;并尝试.
Remove the ; and try.
这篇关于System.Data.SqlClient.SqlException:'='附近的语法不正确.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!