Assigning Parameters to ADO.NET Commands
When creating SQL commands that interact with a database, it is important to properly specify the parameters used within the command. In this example, an attempt was made to add a new record to a database table using a SqlCommand, but an error occurred.
To resolve this issue, the code should be modified as follows:
SqlCommand cmd = new SqlCommand("INSERT INTO Product_table (Product_Name, Product_Price, Product_Profit, p) 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();
In this corrected code:
The above is the detailed content of How to Properly Assign Parameters to ADO.NET Commands for Database Inserts?. For more information, please follow other related articles on the PHP Chinese website!