我目前在做一个医院管理系统,这里时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);
}

解决方案 »

  1.   

    1.Sex 字段是什么类型的,是不是不匹配。
    2.sqlDataAdapter1.InsertCommand直接设置它的CommandText,不要使用sqlDataAdapter1.InsertCommand=Comm; 
      

  2.   

    为了加快开发进程,除了员工编号字段类型为bigint之外,其他全设置为可变类型varchar。具体规范,要到最后阶段才添加各字段限制类型
      

  3.   

    问题是什么看不出来,不过有办法解决。你将你拼出来的这个Insert语句输出到文本,然后用查询分析器去运行。一分钟就可以解决问题。
      

  4.   

    单词educational中d,u顺序有误之外,再查询分析器里也没有再找到多余的可用信息
    因为将insert语句添加后,竟然成功执行了.......!!!??打开数据表,看到了,执行插入的新记录........!!!!!!!!