所以我在下面的代码中给出了3个表格 我不知道是否有任何方法可以优化它,因为我认为这不是插入数据的正确方法,特别是如果你正在处理规范化。
string mySqlCon = "Server = localhost; port = 3306; database = rmsdb; Uid = root; pwd=''"; //connection string
private void loginbtn_Click(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection(mySqlCon);
MySqlCommand cmd;
try
{
if (firstnametb.Text.Length > 0 && lastnametb.Text.Length > 0 && gendercb.Text.Length > 0 && addresstb.Text.Length > 0 && birthdatetb.Text.Length > 0 && gradelevelcb.Text.Length > 0 && sectioncb.Text.Length > 0 && positioncb.Text.Length > 0 && empstatuscb.Text.Length > 0 && fiutb.Text.Length > 0 && oadtb.Text.Length > 0 && prctb.Text.Length > 0 && gsistb.Text.Length > 0 && csetb.Text.Length > 0 && contacttb.Text.Length > 0 && usernametb.Text.Length > 0 && passwordtb.Text.Length > 0 && questioncb.Text.Length > 0 && answertb.Text.Length > 0)
{
string CmdString = "INSERT INTO TeachersINFO('teachersid',firstname,lastname,gender,address,birthdate,position,empstatus,contactnumber,gradelevel,section,status) values (@teachersid,@firstname,@lastname,@gender,@address,@birthdate,@position,@empstatus,@contactnumber,@gradelevel,@section,@status);" +Environment.NewLine+
"INSERT INTO teacherslogin(teachersid,username,password,secretquestion,answer,status) values (@teachersid2,@username,@password,@secretquestion,@answer,@status2);" +Environment.NewLine+
"INSERT INTO teachersref(teachersid,FIU,oad,PrcNo,GSISBPNo,UMIDNo,TinNo,PhilHealthNo,CivilServiceE) values (@teachersid3,@FIU,@oad,@PrcNo,@GSISBPNo,@UMIDNo,@TinNo,@PhilHealthNo)";
cmd = new MySqlCommand(CmdString, con);
cmd.Parameters.AddWithValue("@teachersid", teacheridtb.Text);
cmd.Parameters.AddWithValue("@firstname", firstnametb.Text);
cmd.Parameters.AddWithValue("@lastname", lastnametb.Text);
cmd.Parameters.AddWithValue("@gender", gendercb.Text);
cmd.Parameters.AddWithValue("@address", addresstb.Text);
cmd.Parameters.AddWithValue("@birthdate", birthdatetb.Text);
cmd.Parameters.AddWithValue("@position", positioncb.Text);
cmd.Parameters.AddWithValue("@empstatus", empstatuscb.Text);
cmd.Parameters.AddWithValue("@contactnumber", contacttb.Text);
cmd.Parameters.AddWithValue("@gradelevel", gradelevelcb.Text);
cmd.Parameters.AddWithValue("@section", sectioncb.Text);
cmd.Parameters.AddWithValue("@status", 1);
//secondtable values p0ta kahit pandurugas to wala kong pake stored procedures parin tawag dito HAHAHA//
cmd.Parameters.AddWithValue("@teachersid2", teacheridtb.Text);
cmd.Parameters.AddWithValue("@username", usernametb.Text);
cmd.Parameters.AddWithValue("@password", passwordtb.Text);
cmd.Parameters.AddWithValue("@secretquestion", questioncb.Text);
cmd.Parameters.AddWithValue("@answer", answertb.Text);
cmd.Parameters.AddWithValue("@status2",1);
//thirdtable values hahaha pota pandaraya so much
con.Open();
int RowsAffected = cmd.ExecuteNonQuery();
if (RowsAffected > 0)
{
MessageBox.Show("Registered Successfully", "System Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
firstnametb.Text = "";
lastnametb.Text = "";
gendercb.SelectedItem = null;
lastnametb.Text = "";
addresstb.Text = "";
gradelevelcb.SelectedItem = null;
sectioncb.SelectedItem = null;
answertb.Text = "";
usernametb.Text = "";
passwordtb.Text = "";
questioncb.Text = "";
empstatuscb.Text = "";
positioncb.Text = "";
fiutb.Text = "";
oadtb.Text = "";
prctb.Text = "";
gsistb.Text = "";
csetb.Text = "";
con.Close();
//clearing textboxes fields.
}
}
else
{
MessageBox.Show("Incomplete Data", "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
答案 0 :(得分:0)
我建议使用一个存储过程来接收所有这些参数然后插入。
使用存储过程可以帮助优化代码,因为存储过程在C#代码中调用之前已经编译过。这应该有助于优化。
您还可以使用EntityFramework之类的东西来帮助您避免自己编写SQL命令。
答案 1 :(得分:0)
更新数据的方法没有错。 但是,C#代码优化以及程序可靠性还有很大的空间。
首先,try/catch/finally
块不适用于管理IDisposable
资源。您可以通过使用using
块来使您的C#代码更具可读性。然后捕获数据库调用并至少记录任何错误。这样,您的程序将维持数据库服务器故障并继续执行。稍后,您可以检查日志并查看错误,并在下一个程序版本中找出解决方法。
string mySqlCon = "Server = localhost; port = 3306; database = rmsdb; Uid = root; pwd=''"; //connection string
private void loginbtn_Click(object sender, EventArgs e)
{
var succeeded = false;
var canUpdate = firstnametb.Text.Length > 0
&& lastnametb.Text.Length > 0
&& gendercb.Text.Length > 0
&& addresstb.Text.Length > 0
&& birthdatetb.Text.Length > 0
&& gradelevelcb.Text.Length > 0
&& sectioncb.Text.Length > 0
&& positioncb.Text.Length > 0
&& empstatuscb.Text.Length > 0
&& fiutb.Text.Length > 0
&& oadtb.Text.Length > 0
&& prctb.Text.Length > 0
&& gsistb.Text.Length > 0
&& csetb.Text.Length > 0
&& contacttb.Text.Length > 0
&& usernametb.Text.Length > 0
&& passwordtb.Text.Length > 0
&& questioncb.Text.Length > 0
&& answertb.Text.Length > 0;
if (canUpdate)
{
using (var con = new MySqlConnection(mySqlCon))
using (var cmd = con.CreateCommand())
{
cmd = "INSERT INTO TeachersINFO('teachersid',firstname,lastname,gender,address,birthdate,position,empstatus,contactnumber,gradelevel,section,status) values (@teachersid,@firstname,@lastname,@gender,@address,@birthdate,@position,@empstatus,@contactnumber,@gradelevel,@section,@status);" + Environment.NewLine
+ "INSERT INTO teacherslogin(teachersid,username,password,secretquestion,answer,status) values (@teachersid2,@username,@password,@secretquestion,@answer,@status2);" + Environment.NewLine
+ "INSERT INTO teachersref(teachersid,FIU,oad,PrcNo,GSISBPNo,UMIDNo,TinNo,PhilHealthNo,CivilServiceE) values (@teachersid3,@FIU,@oad,@PrcNo,@GSISBPNo,@UMIDNo,@TinNo,@PhilHealthNo)";
cmd.Parameters.AddWithValue("@teachersid", teacheridtb.Text);
cmd.Parameters.AddWithValue("@firstname", firstnametb.Text);
cmd.Parameters.AddWithValue("@lastname", lastnametb.Text);
cmd.Parameters.AddWithValue("@gender", gendercb.Text);
cmd.Parameters.AddWithValue("@address", addresstb.Text);
cmd.Parameters.AddWithValue("@birthdate", birthdatetb.Text);
cmd.Parameters.AddWithValue("@position", positioncb.Text);
cmd.Parameters.AddWithValue("@empstatus", empstatuscb.Text);
cmd.Parameters.AddWithValue("@contactnumber", contacttb.Text);
cmd.Parameters.AddWithValue("@gradelevel", gradelevelcb.Text);
cmd.Parameters.AddWithValue("@section", sectioncb.Text);
cmd.Parameters.AddWithValue("@status", 1);
//secondtable values p0ta kahit pandurugas to wala kong pake stored procedures parin tawag dito HAHAHA//
cmd.Parameters.AddWithValue("@teachersid2", teacheridtb.Text);
cmd.Parameters.AddWithValue("@username", usernametb.Text);
cmd.Parameters.AddWithValue("@password", passwordtb.Text);
cmd.Parameters.AddWithValue("@secretquestion", questioncb.Text);
cmd.Parameters.AddWithValue("@answer", answertb.Text);
cmd.Parameters.AddWithValue("@status2", 1);
//thirdtable values hahaha pota pandaraya so much
con.Open();
try
{
var RowsAffected = cmd.ExecuteNonQuery();
succeeded = RowsAffected > 0;
}
catch (Exception ex)
{
// Log the captured exception here
// E.g. if using NUnit do:
// Log.Fatal(ex.ToString());
}
}
if (succeeded)
{
MessageBox.Show("Registered Successfully", "System Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
firstnametb.Text = "";
lastnametb.Text = "";
gendercb.SelectedItem = null;
lastnametb.Text = "";
addresstb.Text = "";
gradelevelcb.SelectedItem = null;
sectioncb.SelectedItem = null;
answertb.Text = "";
usernametb.Text = "";
passwordtb.Text = "";
questioncb.Text = "";
empstatuscb.Text = "";
positioncb.Text = "";
fiutb.Text = "";
oadtb.Text = "";
prctb.Text = "";
gsistb.Text = "";
csetb.Text = "";
//clearing textboxes fields.
}
else
{
MessageBox.Show("Some I/O error has occured.", "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Incomplete Data", "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}