这段代码不考虑异常抛出,有问题,请大家指出
“提交”按纽的代码:
private void button1_Click(object sender, EventArgs e)
{ //当“提交”按纽被按下时,将文本框的值赋值给数据库中“员工(YG)”对应的项
if ((textBox1.Text!="") && (textBox2.Text!="") && (textBox3.Text!="") &&
(textBox4.Text!="") &&( textBox5.Text!=""))
{
int YG_ID = Convert.ToInt32(textBox1.Text);
YGname = textBox2.Text;
sex = textBox3.Text;
int age = Convert.ToInt32(textBox4.Text);
int BM_ID = Convert.ToInt32(textBox5.Text);
CMySql SP_AddYuanGong = new CMySql();
SP_AddYuanGong.AddYuanGong(Convert.ToInt32(YG_ID), YGname, sex, Convert.ToInt32(age), Convert.ToInt32(BM_ID));
//CMySql是因为的类,专门用来负责SQL语言,里面定义了添加员工的方法
}
else
{
MessageBox.Show("请输入完整信息!");
return;
}
}
数据库中增加员工的方法:
public String AddYuanGong(int YG_ID, string YGname,string sex,int age,int BM_ID)
{//在SQL中增加员工
//建立数据库连接和数据库命令的对象
SqlConnection myConnection = new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("AddYuanGong", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
//
SqlParameter parameterYG_ID = new SqlParameter("@YG_ID", SqlDbType.Int,4);
parameterYG_ID.Value = YG_ID;
myCommand.Parameters.Add(parameterYG_ID);
SqlParameter parameterYGname = new SqlParameter("@YGname", SqlDbType.NVarChar, 20);
parameterYGname.Value = YGname;
myCommand.Parameters.Add(parameterYGname);
SqlParameter parametersex = new SqlParameter("@sex", SqlDbType.NVarChar, 20);
parametersex.Value = sex;
myCommand.Parameters.Add(parametersex);
SqlParameter parameterage = new SqlParameter("@age", SqlDbType.NVarChar, 20);
parameterage.Value = age;
myCommand.Parameters.Add(parameterage);
SqlParameter parameterBM_ID = new SqlParameter("@BM_ID", SqlDbType.Int, 4);
parameterBM_ID.Value = BM_ID;
myCommand.Parameters.Add(parameterBM_ID);
SqlParameter parameterID = new SqlParameter("@ID", SqlDbType.Int, 4);
parameterID.Direction = ParameterDirection.Output;
myCommand.Parameters.Add(parameterID);
try
{
myConnection.Open();
//
myCommand.ExecuteNonQuery();
//
myConnection.Close();
int UserId = (int)parameterID.Value;
return UserId.ToString(); //用哪个返回值?
}
catch
{
return String.Empty;
}
}
我刚起步,还很菜,望大家多多指教!!!