I have the following XML file:
<test>
<test2>
<A>206942</A>
</test2>
<test2>
<A>203405</A>
</test2>
</test>
I need to insert into a SQL Server table:
XmlNodeList dataNodes = xmlDoc.SelectNodes("/test/test2");
SqlConnection dbConnection = new SqlConnection(connectionString);
try
{
dbConnection.Open();
foreach (XmlNode node in dataNodes)
{
String A = (node.SelectSingleNode("A") != null) ?
node.SelectSingleNode("A").InnerText.ToString() : string.Empty;
try
{
using (SqlCommand cmd = dbConnection.CreateCommand())
{
cmd.Parameters.AddWithValue(" @A", A);
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
I try the code above and I always get the following error:
Incorrect syntax near @A Must declare the scalar variable @A
How do I resolve this?
CommandTextbeing set for theSqlCommand. Also, you have a space to the left of the@in theAddWithValue.