Here is my aspx.cs Code behind Class I am Setting properties and set correctly
Here is my getter/setter properties I get properties in SQL insert see below query
以下是将表单发布到SQL Server 2012数据库的VoucherSubmit
方法:
public class InsertData
{
public void VoucherSubmit()
{
myconection mycon = new myconection();
VoucherForm vr = new VoucherForm();
string Query1 = "insert into Voucher(VrType,VrDate,CreatedBy,CreationDate,SubmittedBy,SubmittedDate,ApprovedBy,ApprovedDate,Method,BillNo,ChequeNo,DemandNo,Branch,AccountDebit,AccountCredit,Debit,Credit,Description)values('" + vr.getvouchertype() + "','" + vr._date + "','" + vr._createdby + "','" + vr.getvoucher_creationdate() + "','" + vr._submittedby + "','" + vr._submitteddate + "','" + vr._approvedby + "','" + vr._approveddate + "','" + vr._method + "','" + vr._billno + "','" + vr._chequeno + "','" + vr._demandno + "','" + vr._branch + "','" + vr._accountdebit + "','" + vr._accountcredit + "','" + vr._debit + "','" + vr._credit + "','" + vr._description + "')";
SqlCommand cmd = new SqlCommand(Query1, mycon.GetConnection());
cmd.ExecuteNonQuery();
mycon.GetConnection().Close();
}
}
答案 0 :(得分:2)
您没有传递已初始化的VoucherForm
,而是在VoucherSubmit
方法中创建新的。{/ p>
public void btn_save_Click(object sender, EventArgs e)
{
// All the stuff you have there just change single line
insert.VoucherSubmit(vf);
}
public void VoucherSubmit(VoucherForm vf)
{
myconection mycon = new myconection();
// remove VoucherForm vf = new VoucherForm();
string Query1 = "insert into Voucher(VrType,VrDate,CreatedBy,CreationDate,SubmittedBy,SubmittedDate,ApprovedBy,ApprovedDate,Method,BillNo,ChequeNo,DemandNo,Branch,AccountDebit,AccountCredit,Debit,Credit,Description)values(@VrType,@VrDate,@CreatedBy,@CreationDate,@SubmittedBy,@SubmittedDate,@ApprovedBy,@ApprovedDate,@Method,@BillNo,@ChequeNo,@DemandNo,@Branch,@AccountDebit,@AccountCredit,@Debit,@Credit,@Description)";
SqlCommand cmd = new SqlCommand(Query1, mycon.GetConnection());
cmd.InsertCommand.Parameters.Add("@VrTyp").Value = vr.getvouchertype();
cmd.InsertCommand.Parameters.Add("@VrDate").Value = vr._date;
cmd.InsertCommand.Parameters.Add("@CreatedBy").Value = vr._createdby;
cmd.InsertCommand.Parameters.Add("@CreationDate").Value = vr.getvoucher_creationdate();
cmd.InsertCommand.Parameters.Add("@SubmittedBy").Value = vr._submittedby;
cmd.InsertCommand.Parameters.Add("@SubmittedDate").Value = vr._submitteddate;
cmd.InsertCommand.Parameters.Add("@ApprovedBy").Value = vr._approvedby;
cmd.InsertCommand.Parameters.Add("@ApprovedDate").Value = vr._approveddate;
cmd.InsertCommand.Parameters.Add("@Method").Value = vr._method;
cmd.InsertCommand.Parameters.Add("@BillNo").Value = vr._billno;
cmd.InsertCommand.Parameters.Add("@ChequeNo").Value = vr._chequeno;
cmd.InsertCommand.Parameters.Add("@DemandNo").Value = vr._demandno;
cmd.InsertCommand.Parameters.Add("@Branch").Value = vr._branch;
cmd.InsertCommand.Parameters.Add("@AccountDebit").Value = vr._accountdebit;
cmd.InsertCommand.Parameters.Add("@AccountCredit").Value = vr._accountcredit;
cmd.InsertCommand.Parameters.Add("@Debit").Value = vr._debit;
cmd.InsertCommand.Parameters.Add("@Credit").Value = vr._credit;
cmd.InsertCommand.Parameters.Add("@Description").Value = vr._description;
cmd.ExecuteNonQuery();
mycon.GetConnection().Close();
}
<强>更新强>
注释中的Dan Guzman建议使用指定sql数据类型的重载来避免性能问题。
我引用的文章的主要观点是SqlDbType 应该在案例中明确指定最大长度 varchar列。例如,
cmd.Parameters.Add("@VrDate", SqlDbType.DateTime).Value = DateTime.ParseExact(vr._date, "yyyy-MM-dd", null);
这将确保所需的数据类型 传递并避免SQL Server中过多的缓存计划。
感谢您的更新!
相关链接: