Passing Parameters to ADO.NET Commands
In order to insert a record into a database using an ADO.NET command, parameters should be utilized. The example provided faces an issue due to an incomplete implementation:
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);
To resolve this issue and correctly add parameters, follow these steps:
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();
In this code:
This approach ensures that parameters are properly added to the command and the record is successfully inserted into the database.
The above is the detailed content of How to Correctly Pass Parameters to ADO.NET INSERT Commands?. For more information, please follow other related articles on the PHP Chinese website!