如何保存和更新数据 [英] How to save and update data
本文介绍了如何保存和更新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public static void AddNewStudent(int Stu_id, string Stu_Name, string Class, string Major)
{
OracleDatabase db =(OracleDatabase)DatabaseFactory.CreateDatabase("PHTS");
using (System.Data.Common.DbCommand cmd = db.GetStoredProcCommand("AddNewStudent"))
{
db.AddInParameter(cmd, "I_Stu_id", DbType.Int32, Stu_id);
db.AddInParameter(cmd, "I_Stu_Name", DbType.String, Stu_Name);
db.AddInParameter(cmd, "I_Class", DbType.String, Class);
db.AddInParameter(cmd, "I_Major", DbType.String, Major);
db.ExecuteNonQuery(cmd);
}
}
public static void UpdateStudent(int Stu_id, string Stu_Name, string Class, string Major)
{
OracleDatabase db =(OracleDatabase)DatabaseFactory.CreateDatabase("PHTS");
using (System.Data.Common.DbCommand cmd = db.GetStoredProcCommand("UpdateStudent"))
{
db.AddInParameter(cmd, "I_Stu_id", DbType.Int32, Stu_id);
db.AddInParameter(cmd, "I_Stu_Name", DbType.String, Stu_Name);
db.AddInParameter(cmd, "I_Class", DbType.String, Class);
db.AddInParameter(cmd, "I_Major", DbType.String, Major);
db.ExecuteNonQuery(cmd);
}
}
用户界面
User Interface
private void Savebutton_Click(object sender, EventArgs e)
{
int student_id = Convert.ToInt32(this.SIDtextBox.Text);
string stu_name = this.textBox1.Text;
string class = this.textBox2.Text;
string major= this.textBox3.Text;;
if(stu_id ==0)
{
PHTS.DataServices.cslMaterial.AddNewStudent(stu_id, stu_name, class, major);
MessageBox.Show("Record Saved");
}
else
{
PHTS.DataServices.cslMaterial.UpdateStudent(stu_id, stu_name, class, major);
MessageBox.Show("Record Saved");
}
}
我更改了代码并插入了if语句,但仍然无法正常工作.任何人都可以帮助我,用户如何使用同一按钮保存和更新数据.
谢谢
I changed the code and insterted a if statement but still not working. Can anyone help me how can a user save and update data using the same button.
Thanks
推荐答案
如果您的学生已经存在,请致电update.如果没有,请调用save.
If your student already exists, call update. If not, call save.
有很多方法.
您知道一个不存在的学生的Id必须为null(或者在您将其存储在int中时可能为零)
因此,在C#代码中您都可以说如果学生ID为零,则调用插入存储的proc,否则调用更新存储的proc"
或者您可以有一个存储的过程,说如果Id为0,则插入记录,否则使用传入的Id更新记录.
There''s a number of ways.
You know that the Id must be null for a non-existing student (or maybe zero as you''re storing it in an int)
So, either in c# code you can say "if the student id is zero call insert stored proc, else call update stored proc"
or you could have a single stored proc that says "if the Id is 0 insert the record, else update the record with the Id passed in.
这篇关于如何保存和更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文