我目前在做一个医院管理系统,这里时COPY出来的一个单页:添加员工信息
我尝试了好几种方法,都出现了一个问题,字段不能全部完整的插入进去,只能插入其中的几个字段。
其中目前正在使用,没有被注释的代码可用,但是只能输入4个字段信息,
若继续添加,则出现错误提示:未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。
插入代码buttonSave控件:
private void buttonSave_Click(object sender, System.EventArgs e)
{
/*
SqlCommand Comm=sqlConnection1.CreateCommand();//,Rate,Job,Identityer,DiagFee,Workload,Eudcational,Professional,CardID,NativePlace,Address,Telephone,Res
Comm.CommandText="insert into employee(EmpNo,EmpName,Sex) "
+ "values('"+this.textBoxEmployeeNo.Text+"','"+this.textBoxEmployeeName+"','"+this.comboBoxSex.Text+"')";//,'"+this.textBoxRate.Text+"','"+this.comboBoxJob.Text+"','"+this.comboBoxIdentity.Text+"','"+this.textBoxDiagFee.Text+"','"+this.textBoxWorkload.Text+"','"+this.comboBoxEducational.Text+"','"+this.textBoxProfessional.Text+"','"+this.textBoxCardID.Text+"','"+this.textBoxNativePlace.Text+"','"+this.textBoxAddress.Text+"','"+this.textBoxTelephone.Text+"','"+this.textBoxRes.Text+"'
sqlDataAdapter1.InsertCommand=Comm;
this.sqlConnection1.Open();
Comm.ExecuteNonQuery();
this.sqlConnection1.Close();
*/
//this.sqlConnection1.Open();
//string sql="insert into employee(EmpNo,EmpName,Sex,Job,Identityer,DiagFee,Workload,Rate,Eudcational,Professional,JoinDate,CardID,Telephone,NativePlace,Address,Res) values('"+this.textBoxEmployeeNo.Text+"','"+this.textBoxEmployeeName+"','"+this.comboBoxSex.Text+"','"+this.comboBoxJob.Text+"','"+this.comboBoxIdentity.Text+"','"+this.textBoxDiagFee.Text+"','"+this.textBoxWorkload.Text+"','"+this.textBoxRate.Text+"','"+this.comboBoxEducational.Text+"','"+this.textBoxProfessional.Text+"','"+this.dateTimePickerJoinDate.Text+"','"+this.textBoxCardID.Text+"','"+this.textBoxTelephone.Text+"','"+this.textBoxNativePlace.Text+"','"+this.textBoxAddress.Text+"','"+this.textBoxRes.Text+"')";
//SqlCommand comm=new SqlCommand(sql,this.sqlConnection1);
//comm.ExecuteNonQuery();
//this.sqlConnection1.Close();
SqlCommand myCommand=sqlConnection1.CreateCommand();
myCommand.CommandText="insert into employee (EmpNo,EmpName,Sex,Job) values('"+this.textBoxEmployeeNo.Text.ToString()+"','"+this.textBoxEmployeeName.Text.ToString()+"','"+this.comboBoxSex.Text.ToString()+"','"+this.comboBoxJob.Text.ToString()+"')";
sqlConnection1.Open();
myCommand.ExecuteNonQuery();
sqlConnection1.Close();
MessageBox.Show("成功添加","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
我尝试了好几种方法,都出现了一个问题,字段不能全部完整的插入进去,只能插入其中的几个字段。
其中目前正在使用,没有被注释的代码可用,但是只能输入4个字段信息,
若继续添加,则出现错误提示:未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。
插入代码buttonSave控件:
private void buttonSave_Click(object sender, System.EventArgs e)
{
/*
SqlCommand Comm=sqlConnection1.CreateCommand();//,Rate,Job,Identityer,DiagFee,Workload,Eudcational,Professional,CardID,NativePlace,Address,Telephone,Res
Comm.CommandText="insert into employee(EmpNo,EmpName,Sex) "
+ "values('"+this.textBoxEmployeeNo.Text+"','"+this.textBoxEmployeeName+"','"+this.comboBoxSex.Text+"')";//,'"+this.textBoxRate.Text+"','"+this.comboBoxJob.Text+"','"+this.comboBoxIdentity.Text+"','"+this.textBoxDiagFee.Text+"','"+this.textBoxWorkload.Text+"','"+this.comboBoxEducational.Text+"','"+this.textBoxProfessional.Text+"','"+this.textBoxCardID.Text+"','"+this.textBoxNativePlace.Text+"','"+this.textBoxAddress.Text+"','"+this.textBoxTelephone.Text+"','"+this.textBoxRes.Text+"'
sqlDataAdapter1.InsertCommand=Comm;
this.sqlConnection1.Open();
Comm.ExecuteNonQuery();
this.sqlConnection1.Close();
*/
//this.sqlConnection1.Open();
//string sql="insert into employee(EmpNo,EmpName,Sex,Job,Identityer,DiagFee,Workload,Rate,Eudcational,Professional,JoinDate,CardID,Telephone,NativePlace,Address,Res) values('"+this.textBoxEmployeeNo.Text+"','"+this.textBoxEmployeeName+"','"+this.comboBoxSex.Text+"','"+this.comboBoxJob.Text+"','"+this.comboBoxIdentity.Text+"','"+this.textBoxDiagFee.Text+"','"+this.textBoxWorkload.Text+"','"+this.textBoxRate.Text+"','"+this.comboBoxEducational.Text+"','"+this.textBoxProfessional.Text+"','"+this.dateTimePickerJoinDate.Text+"','"+this.textBoxCardID.Text+"','"+this.textBoxTelephone.Text+"','"+this.textBoxNativePlace.Text+"','"+this.textBoxAddress.Text+"','"+this.textBoxRes.Text+"')";
//SqlCommand comm=new SqlCommand(sql,this.sqlConnection1);
//comm.ExecuteNonQuery();
//this.sqlConnection1.Close();
SqlCommand myCommand=sqlConnection1.CreateCommand();
myCommand.CommandText="insert into employee (EmpNo,EmpName,Sex,Job) values('"+this.textBoxEmployeeNo.Text.ToString()+"','"+this.textBoxEmployeeName.Text.ToString()+"','"+this.comboBoxSex.Text.ToString()+"','"+this.comboBoxJob.Text.ToString()+"')";
sqlConnection1.Open();
myCommand.ExecuteNonQuery();
sqlConnection1.Close();
MessageBox.Show("成功添加","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
2.sqlDataAdapter1.InsertCommand直接设置它的CommandText,不要使用sqlDataAdapter1.InsertCommand=Comm;
因为将insert语句添加后,竟然成功执行了.......!!!??打开数据表,看到了,执行插入的新记录........!!!!!!!!