请尝试更新查询工作正常时帮助我,但更改不会影响数据库值。 我正在使用ms access 2010数据库并通过C#.net
进行操作 private void update()
{
try
{
conn = new OleDbConnection(cs.DBConn);
conn.Open();
string cb="update Student_Data set Student_Name=@s2,Father_Name=@s3,Mother_Name=@s4,DOB=@s5,Standard=@s6,Addmission_For_Batch=@s7,Gender=@8,Joining_Date=@s9,Package_Fees=@s10,Fees_Mode=@s11,School_College_Name=@s12,Address=@s13,Parents_Contact=@s14,Self_Contact=@s15,SMS_Contact=@s16,Email_ID=@s17,Religion=@s18,Caste_Quoto=@s19,Total_Fees=@s20,Prefered_Batch=@s21,Photo=@22 where Addmission_Number=@d1";
OleDbCommand cmd = new OleDbCommand(cb);
cmd.Connection = conn;
cmd.Parameters.Add(new OleDbParameter ("@d1", System.Data.OleDb.OleDbType.VarChar, 15, "Addmission_Number"));
cmd.Parameters.Add(new OleDbParameter("@s2",System.Data.OleDb.OleDbType.VarChar,40,"Student_Name"));
cmd.Parameters.Add(new OleDbParameter("@s3", System.Data.OleDb.OleDbType.VarChar, 40, "Father_Name"));
cmd.Parameters.Add(new OleDbParameter("@s4",System.Data.OleDb.OleDbType.VarChar,40,"Mother_Name"));
cmd.Parameters.Add(new OleDbParameter("@s5", System.Data.OleDb.OleDbType.DBDate, 15, "DOB"));
cmd.Parameters.Add(new OleDbParameter("@s6", System.Data.OleDb.OleDbType.VarChar, 20, "Standard"));
cmd.Parameters.Add(new OleDbParameter("@s7", System.Data.OleDb.OleDbType.VarChar, 20, "Addmission_For_Batch"));
cmd.Parameters.Add(new OleDbParameter("@s8", System.Data.OleDb.OleDbType.VarChar, 10, "Gender"));
cmd.Parameters.Add(new OleDbParameter("@s9", System.Data.OleDb.OleDbType.VarChar, 20, "Joining_Date"));
cmd.Parameters.Add(new OleDbParameter("@s10", System.Data.OleDb.OleDbType.Integer, 10, "Package_Fees"));
cmd.Parameters.Add(new OleDbParameter("@s11", System.Data.OleDb.OleDbType.VarChar, 15, "Fees_Mode"));
cmd.Parameters.Add(new OleDbParameter("@s12", System.Data.OleDb.OleDbType.VarChar, 20, "School_College_Name"));
cmd.Parameters.Add(new OleDbParameter("@s13", System.Data.OleDb.OleDbType.VarChar, 40, "Address"));
cmd.Parameters.Add(new OleDbParameter("@s14", System.Data.OleDb.OleDbType.VarChar, 15, "Parents_Contact"));
cmd.Parameters.Add(new OleDbParameter("@s15", System.Data.OleDb.OleDbType.VarChar, 15, "Self_Contact"));
cmd.Parameters.Add(new OleDbParameter("@s16", System.Data.OleDb.OleDbType.VarChar, 15, "SMS_Contact"));
cmd.Parameters.Add(new OleDbParameter("@s17", System.Data.OleDb.OleDbType.VarChar, 40, "Email_ID"));
cmd.Parameters.Add(new OleDbParameter("@s18", System.Data.OleDb.OleDbType.VarChar, 20, "Religion"));
cmd.Parameters.Add(new OleDbParameter("@s19", System.Data.OleDb.OleDbType.VarChar, 20, "Caste_Quoto"));
cmd.Parameters.Add(new OleDbParameter("@s20", System.Data.OleDb.OleDbType.VarChar, 20, "Total_Fees"));
cmd.Parameters.Add(new OleDbParameter("@s21", System.Data.OleDb.OleDbType.VarChar, 20, "Prefered_Batch"));
cmd.Parameters["@d1"].Value = ScholarNo.Text.Trim();
cmd.Parameters["@s2"].Value =TxtStudName.Text.Trim();
cmd.Parameters["@s3"].Value =TxtFathername.Text.Trim();
cmd.Parameters["@s4"].Value =TxtMotherName.Text.Trim();
cmd.Parameters["@s5"].Value = DTPDob.Text.Trim();
cmd.Parameters["@s6"].Value = TxtStandard.Text.Trim();
cmd.Parameters["@s7"].Value = TxtAdmForBatch.Text.Trim();
cmd.Parameters["@s8"].Value = CmbGender.Text.Trim();
cmd.Parameters["@s9"].Value = DTPJoiningdate.Text.Trim();
cmd.Parameters["@s10"].Value = TxtPackagefees.Text.Trim();
cmd.Parameters["@s11"].Value = CmbMode.Text.Trim();
cmd.Parameters["@s12"].Value = TxtSchooclg.Text.Trim();
cmd.Parameters["@s13"].Value =TxtAddress.Text.Trim();
cmd.Parameters["@s14"].Value = TxtParentContact.Text.Trim();
cmd.Parameters["@s15"].Value = TxtSelfcntct.Text.Trim();
cmd.Parameters["@s16"].Value = TxtSmsCntct.Text.Trim();
cmd.Parameters["@s17"].Value =TxtEmailID.Text.Trim();
cmd.Parameters["@s18"].Value =CMBreligion.Text.Trim();
cmd.Parameters["@s19"].Value = CmbCastqta.Text.Trim();
cmd.Parameters["@s20"].Value = TxtTotalfees.Text.Trim();
cmd.Parameters["@s21"].Value = TxtPreferdBatch.Text.Trim();
ms = new MemoryStream();
Bitmap bmpImage = new Bitmap(PicBox.Image);
bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] data = ms.GetBuffer();
OleDbParameter p = new OleDbParameter("@s22", OleDbType.VarBinary);
p.Value = data;
cmd.Parameters.Add(p);
cmd.ExecuteNonQuery();
MessageBox.Show("Successfully Updated", "Student Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
BtnViewdtl.Enabled = false;
conn.Close();
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
conn.Close();
}
}
答案 0 :(得分:2)
OleDbCommand
不支持命名参数。实际上它支持,但它不是关心。只关心他们的订单。
据我所知,您的参数定义和参数顺序不相同。
例如,您的Student_Name=@s2
参数与您的@d1
参数定义匹配。
更改参数添加部分与声明命令相同。此外,我会使用using
statement自动处理您的连接和命令,而不是手动调用Close
方法。
如果除此之外一切都很好,这应该解决它。