下面是我在类后面的aspx.cs代码,我正在设置属性并正确设置
下面是我在SQL中获得的getter/setter属性,请参阅下面的查询
下面是我的VoucherSubmit方法,它将表单发布到Server 2012数据库中:
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();
}
}发布于 2018-01-07 10:43:27
不是传递初始化的VoucherForm,而是在VoucherSubmit方法中创建新的。
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();
}更新:
注释中的丹·古兹曼建议对指定的sql数据类型使用重载,以避免性能问题。
我所引用的文章的要点是,在varchar列的情况下,应该显式地指定SqlDbType和最大长度。例如, cmd.Parameters.Add("@VrDate",SqlDbType.DateTime).Value = DateTime.ParseExact(vr._date,“yyyy dd”,空); 这将确保传递所需的数据类型,并避免SQL Server中过多的缓存计划。
谢谢你的更新!
相关链接:
https://stackoverflow.com/questions/48136301
复制相似问题