Pass multiple values to SQL insert statement
I have a web form which has 23 textbox's , I need to pass it to a function, which has SQL command to开发者_如何学运维 insert the values of 23 textbox to the table..
Is there a better way using DataSet and SQLDataAdapter? Becuase if not then i need to pass all the 23 vlues in the function like
//function to insert textbox values to the table
public int createRecord(val1,val2,val3,....val23)
{
string query = "insert into tablename values (.23 values...)";
sqlCommand command = new sqlCommand(query,connectionstring);
..
}
please suggest me atleast a good method or give a good link which can solve my problem..
If you are inserting a record and all 23 values are necessary, then there's not really a better way.
However, it'd be better to use a stored procedure and pass the parameters to the stored procedure instead of using hardcoded SQL in your application, which is always a bad idea.
Something like this:
SqlCommand cmd = new SqlCommand("YourSPName", conn);
cmd.CommandType = CommandType.StoredProcedure;
// add the parameters the SP needs
cmd.Parameters.Add(new SqlParameter("@Parm1", parm1Value));
cmd.Parameters.Add(new SqlParameter("@Parm2", parm2Value));
.
.
// execute the SP
cmd.ExecuteNonQuery();
You may re-write your function to the following, and pass a list of values instead of 23 parameters.
Replace your function to
public int createRecord(your_type[] list)
{
// .. building insert statement from the list of values.
}
精彩评论