G
Guest
Hello,
I am new to ADO.NET and SQL. What I am trying to do is very basic - to
insert some variable values into database. The following is the C# code:
private static void InsertIntoSqlDb(OleDbDataReader reader)
{
string insStr = "";
string list_no = reader.GetString(0);
string item_no = reader.GetString(1);
string description = reader.GetString(2);
string priceStr = reader.GetString(3);
insStr = "INSERT INTO dliTestDb (list, item, description, price) VALUES
(list_no, item_no, description, priceStr)";
SqlCommand cmd = new SqlCommand(insStr, conn);
//Insert
//
try
{
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
Upon execution of the above, I got exception:
System.Data.SqlClient.SqlException: The name 'list_no' is not permitted in
this
context. Only constants, expressions, or variables allowed here. Column
names ar
e not permitted.
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
Doesn't any one know how to use variable names exactly in the SQL insertion
string?
Thanks.
I am new to ADO.NET and SQL. What I am trying to do is very basic - to
insert some variable values into database. The following is the C# code:
private static void InsertIntoSqlDb(OleDbDataReader reader)
{
string insStr = "";
string list_no = reader.GetString(0);
string item_no = reader.GetString(1);
string description = reader.GetString(2);
string priceStr = reader.GetString(3);
insStr = "INSERT INTO dliTestDb (list, item, description, price) VALUES
(list_no, item_no, description, priceStr)";
SqlCommand cmd = new SqlCommand(insStr, conn);
//Insert
//
try
{
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
Upon execution of the above, I got exception:
System.Data.SqlClient.SqlException: The name 'list_no' is not permitted in
this
context. Only constants, expressions, or variables allowed here. Column
names ar
e not permitted.
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
Doesn't any one know how to use variable names exactly in the SQL insertion
string?
Thanks.