I have 98 columns in a table named "molasses_analysis" and I need to insert records using my C # desktop application.
An example of my code is given below.
string insert_sql = @"insert into molasses_analysis(mo_entry_date, mo_entry_time, mo_code, mo_brix, mo_pol, mo_purity, mo_crtd_by) " +
" values(@entry_date, @entry_time, @mol_code, @brix, @pol, @purity, @crtd_by)";
try
{
List<SqlParameter> param = new List<SqlParameter>();
param.Add(new SqlParameter("@entry_date", entry_date));
param.Add(new SqlParameter("@entry_time", entry_time));
param.Add(new SqlParameter("@mol_code", mol_code));
param.Add(new SqlParameter("@brix", brix));
param.Add(new SqlParameter("@pol", pol));
param.Add(new SqlParameter("@purity", purity));
param.Add(new SqlParameter("@crtd_by", crtd_by));
int inserted_rows = SqlHelper.ExecuteNonQuery(dbConn.sqlConn(),CommandType.Text, insert_sql, param.ToArray());
}
catch (Exception ex)
{
MessageBox.Show("Data not saved!\nError message - "+ex.Message, "Error!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Here I just used only seven fields / columns, but it would be very harsh and painful to write such code for 98 columns and assign an Sql Parameter for each column. My question is, is there any clean and good code to insert multiple columns using C # code?
source
share