')'附近的语法不正确

时间:2016-04-07 15:31:24

标签: c# sql-server

我收到错误:

  

')附近的语法错误。

代码:

string connetionString = null;

SqlConnection connection;
SqlCommand command;

SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();

XmlReader xmlFile;
string sql = null;

connetionString = "Data Source=tsrv2062;Initial Catalog=Bums;User ID=USERNAME;Password=PASSWORD";

connection = new SqlConnection(connetionString);

xmlFile = XmlReader.Create("navetout.xml", new XmlReaderSettings());
ds.ReadXml(xmlFile);

connection.Open();

SqlCommand command1 = new SqlCommand("INSERT INTO Seamen(PersonalIdentityNumber) VALUES (@PersonalIdentityNumber))", connection);

foreach (var P in PersonalIdentityNumber)
{
    command1.Parameters.Clear();
    command1.Parameters.AddWithValue("@PersonalIdentityNumber", P);

    command1.ExecuteNonQuery();
}

connection.Close();
Console.WriteLine("Done");

我在运行时遇到这个错误,所以不知道它发生在哪里,我所知道的是它发生在上面的代码中。任何人都可以看到问题出在哪里?

2 个答案:

答案 0 :(得分:4)

这部分错了:

"INSERT INTO Seamen(PersonalIdentityNumber) values(@PersonalIdentityNumber))"

应该是

"INSERT INTO Seamen(PersonalIdentityNumber) values(@PersonalIdentityNumber)"

注意最后一个括号。

答案 1 :(得分:1)

此:

SqlCommand command1 = new SqlCommand("INSERT INTO Seamen(PersonalIdentityNumber) values(@PersonalIdentityNumber))", connection);

应该是:

SqlCommand command1 = new SqlCommand("INSERT INTO Seamen(PersonalIdentityNumber) values(@PersonalIdentityNumber)", connection);

请注意SQL末尾的缺失括号。

一般来说,您可能希望改进数据库处理代码。使用几个using块会更好。