Question:
When attempting to insert a record into a database using an SQL command, the code execution fails. The provided code includes:
SqlCommand comand = new SqlCommand("INSERT INTO Product_table Values(@Product_Name,@Product_Price,@Product_Profit,@p)", connect); SqlParameter ppar = new SqlParameter(); ppar.ParameterName = "@Product_Name"; ppar.Value = textBox1.Text; MessageBox.Show("Done"); comaand.Parameters.Add(ppar);
Answer:
To successfully insert a record using parameters, the following code should be used:
SqlCommand cmd = new SqlCommand("INSERT INTO Product_table Values(@Product_Name, @Product_Price, @Product_Profit, @p)", connect); cmd.Parameters.Add("@Product_Name", SqlDbType.NVarChar, ProductNameSizeHere).Value = txtProductName.Text; cmd.Parameters.Add("@Product_Price", SqlDbType.Int).Value = txtProductPrice.Text; cmd.Parameters.Add("@Product_Profit", SqlDbType.Int).Value = txtProductProfit.Text; cmd.Parameters.Add("@p", SqlDbType.NVarChar, PSizeHere).Value = txtP.Text; cmd.ExecuteNonQuery();
Explanation:
The above is the detailed content of Why is my SQL INSERT statement with parameters failing, and how can I fix it?. For more information, please follow other related articles on the PHP Chinese website!