我在更新表时遇到错误 [英] I have an error updating a table
问题描述
我正在开发一个具有master和addEmployee的解决方案,分别作为表和表单。该解决方案将使用addEmployee表单将数据更新为master。该解决方案使用C#作为前端,使用sql2008数据库作为后端。编译解决方案后,后端没有任何数据,我收到错误未更新
有人可以帮助我吗?
我尝试过:
I am developing a solution that has master and addEmployee as a table and a form respectively. The solution will update data into master using addEmployee form. The solution uses C# as front-end and sql2008 database as back-end. After compiling the solution, the back-end does not have any data and i receive an error " Not Updated"
Can somebody help me?
What I have tried:
private void btnUpdate_Click_1(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("Please enter valid data...");
}
else
{
FileStream fs = new FileStream(textBox1.Text, FileMode.OpenOrCreate, FileAccess.Read);
byte[] bt = new byte[fs.Length];
fs.Read(bt, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=AKINTOMIDE-HP\\SQLEXPRESS;Initial Catalog=fham;Integrated Security=True";
SqlCommand cmd = new SqlCommand("update master set mon=@Mon , yr=@Yr, dat=@datAd,staf=@Staf,adrs=@Adrs,coy=@Coy,loc=@Loc,dpt=@Dpt,grd= @Grd,datead= @datEmp,datemod=@DatMod,bsal=@BSal,[tran]=@Tran,hous=@Hous,lunc=@Lunc,uti=@Uti,furn=@Furn,harz=@Harz,educ=@Educ,housM=@HousM,dres=@Dres,risk=@Risk,veh=@Veh,driv=@Driv,dome=@Dome,pfs=@PfS,nhf=@NHF,nhfs=@NHFS,oded=@Oded,housU=@HousU,staffM=@StaffM,thrift=@Thrift,accno=@Accno,bank= @Bank,email=@email,penadmin=@PenAdmin,pennum=@PenNum,status=@Status,dupfront=@DUpfront,picstaffpix=@picStafPix,pens=@Pens,paye=@PAYE,Tpay=@TPay,npay=@NPay WHERE eid= @eid ", con);
con.Open();
cmd.Parameters.AddWithValue("@eid", txteid.Text);
cmd.Parameters.AddWithValue("@yr", cmbYr.Text);
cmd.Parameters.AddWithValue("@dat", datAd.Text);
cmd.Parameters.AddWithValue("@datead", datEmp.Text);
cmd.Parameters.AddWithValue("@datemod", DatMod.Text);
cmd.Parameters.AddWithValue("@staf", txtStaf.Text);
cmd.Parameters.AddWithValue("@adrs", txtAdrs.Text);
cmd.Parameters.AddWithValue("@coy", txtCoy.Text);
cmd.Parameters.AddWithValue("@dpt", cmbDpt.Text);
cmd.Parameters.AddWithValue("@loc", cmbLoc.Text);
cmd.Parameters.AddWithValue("@grd", CmbGrd.Text);
cmd.Parameters.AddWithValue("@accno", TxtAccno.Text);
cmd.Parameters.AddWithValue("@email", txtemail.Text);
cmd.Parameters.AddWithValue("@picstaffpix", bt);
cmd.Parameters.AddWithValue("@bank", cmbBank.Text);
cmd.Parameters.AddWithValue("@penadmin", cmbPenAdmin.Text);
cmd.Parameters.AddWithValue("@status", cmbStatus.Text);
cmd.Parameters.AddWithValue("@pennum", txtPenNum.Text);
cmd.Parameters.AddWithValue("@bsal", txtBSal.Text);
cmd.Parameters.AddWithValue("@dres", txtDres.Text);
cmd.Parameters.AddWithValue("@dupfront", txtDUpfront.Text);
cmd.Parameters.AddWithValue("@educ", txtEduc.Text);
cmd.Parameters.AddWithValue("@furn", txtFurn.Text);
cmd.Parameters.AddWithValue("@harz", txtHarz.Text);
cmd.Parameters.AddWithValue("@tran", txtTran.Text);
cmd.Parameters.AddWithValue("@hous", txtHous.Text);
cmd.Parameters.AddWithValue("@lunc", txtLunc.Text);
cmd.Parameters.AddWithValue("@uti", txtUti.Text);
cmd.Parameters.AddWithValue("@housM", txtHousM.Text);
cmd.Parameters.AddWithValue("@risk", txtRisk.Text);
cmd.Parameters.AddWithValue("@veh", txtVeh.Text);
cmd.Parameters.AddWithValue("@pfs", txtPfS.Text);
cmd.Parameters.AddWithValue("@nhf", txtNHF.Text);
cmd.Parameters.AddWithValue("@nhfs", txtNHFS.Text);
cmd.Parameters.AddWithValue("@oded", txtOded.Text);
cmd.Parameters.AddWithValue("@pens", txtPens.Text);
cmd.Parameters.AddWithValue("@paye", this.txtPAYE.Text);
cmd.Parameters.AddWithValue("@tded", txtTDed.Text);
cmd.Parameters.AddWithValue("@driv", txtDriv.Text);
cmd.Parameters.AddWithValue("@dome", txtDome.Text);
cmd.Parameters.AddWithValue("@tpay", txtTPay.Text);
cmd.Parameters.AddWithValue("@npay", txtNPay.Text);
cmd.Parameters.AddWithValue("@housU", this.txtHousU.Text);
cmd.Parameters.AddWithValue("@staffM", this.txtStaffM.Text);
cmd.Parameters.AddWithValue("@thrift", this.txtThrift.Text);
cmd.Parameters.AddWithValue("@mon", cmbMon.Text);
cmd.Parameters.AddWithValue("@otherP", this.txtOtherPay.Text);
cmd.Connection = con;
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Updated...");
textBox1.Text = "";
}
catch (Exception)
{
MessageBox.Show("Not Updated");
}
finally
{
con.Close();
}
}
}
推荐答案
首先不丢弃重要信息!
您不应该使用异常
进行全局catch
你应该尝试更明确,只捕获你可以处理的那些错误,例如SqlException
但在这种情况下,它不如不丢弃实际问题重要。
替换你的收益:
Start by not discarding important information!
You shouldn't really do a "global"catch
withException
you should try to be more explicit, and catch only those errors you can handle, such asSqlException
but in this case it's less important than not discarding the actual problem.
Replace your catch:
catch (Exception ex)
{
MessageBox.Show("Not Updated:\n" + ex.Message);
}
这样,消息框不仅会告诉你异常是什么,而且你可以使用 ex
对象。调试器更详细地检查问题。
目前,您丢弃系统可以提供的所有信息,没有它,任何人都可以猜测是什么问题实际上是!
That way, not only will the message box tell you what the exception was, but you can use the ex
object in the debugger to examine the problem in greater detail.
At the moment, you discard all the information the system can give you, and without that, it's anybodies guess what the problem actually is!
这篇关于我在更新表时遇到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!