插入数据时的字符串输入错误c#(输入字符串未正确格式化错误)

时间:2017-04-01 08:35:53

标签: c# sql sql-server stored-procedures

enter image description here enter image description here我遇到了问题,我发布了存储过程,c#代码以及将数据插入数据库时​​发生的错误。我需要一个解决方案

 string dataString = @"Data Source=DESKTOP-E5V2JHG;Initial catalog=hospital_management;Integrated Security=SSPI";
          SqlConnection connect = new SqlConnection(dataString);


          connect.Open();



          SqlCommand cmd = new SqlCommand("Add_doctor", connect);
          cmd.CommandType = CommandType.StoredProcedure;

          cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = textBox1.Text;
          cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = textBox2.Text;
          cmd.Parameters.Add("@ContactNo", SqlDbType.Decimal).Value = Convert.ToDecimal(textBox3.Text);
          cmd.Parameters.Add("@EmailId", SqlDbType.VarChar).Value = textBox4.Text;
          cmd.Parameters.Add("@GenderId", SqlDbType.Int).Value = Convert.ToInt32(comboBox1.SelectedText);
          cmd.Parameters.Add("@Nationality", SqlDbType.VarChar).Value = textBox5.Text;
          cmd.Parameters.Add("@salary", SqlDbType.Int).Value = Convert.ToInt32(textBox7.Text);
          cmd.Parameters.Add("@DOB", SqlDbType.DateTime2).Value = dateTimePicker1;
          cmd.Parameters.Add("@shiftId", SqlDbType.Int).Value = Convert.ToInt32(comboBox3.SelectedText);
          cmd.Parameters.Add("@qualification", SqlDbType.VarChar).Value = textBox9.Text;
          cmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = textBox10.Text;
          cmd.Parameters.Add("@HireDate", SqlDbType.DateTime2).Value = dateTimePicker2;
          cmd.Parameters.Add("@Pass", SqlDbType.VarChar).Value = textBox8.Text;
          cmd.Parameters.Add("@Religion", SqlDbType.VarChar).Value = textBox6.Text;
          cmd.Parameters.Add("@specId", SqlDbType.Int).Value = Convert.ToInt32(comboBox2.SelectedText);

          cmd.ExecuteNonQuery();
          MessageBox.Show("Command Executed");
          connect.Close();

这是在c#

Create Proc AddDoctor
@EMPtypeID VARCHAR(50),
@FirstName VARCHAR(50),
@LastName VARCHAR(50),
@ContactNo INT,
@EmailId VARCHAR(50),
@GenderId INT,
@Nationality VARCHAR(50),
@salary INT,
@DOB DATETIME2,
@shiftID INT,
@qualification VARCHAR(50),
@Address VARCHAR(50),
@HireDate DATETIME2,
@Pass VARCHAR(50),    
@Religion VARCHAR(50),
@specId INT
AS
BeGIN


insert into EMPLOYEES (_EMPtypeID,_FirstName,_LastName,_ContactNo,_EmailId,
_GenderId,_Nationality,_salary,_DOB,_ShiftId,_qualification,
_Address,_HireDate,_Password,_Religion,_specialityId)

values (@EMPtypeID,@FirstName,@LastName,@ContactNo,@EmailId, 
@GenderId,@Nationality,@salary,@DOB,@shiftID,@qualification,
@Address,@HireDate,@Pass,@Religion,@specId);

END

这是数据库中的存储过程

this error occurs

1 个答案:

答案 0 :(得分:0)

在您的DOBHireDate中是问题所在 改变这个

cmd.Parameters.Add("@DOB", SqlDbType.DateTime2).Value = dateTimePicker1;
cmd.Parameters.Add("@HireDate", SqlDbType.DateTime2).Value = dateTimePicker2;

到此

cmd.Parameters.Add("@DOB", SqlDbType.DateTime2).Value = dateTimePicker1.Text.ToString();
cmd.Parameters.Add("@HireDate", SqlDbType.DateTime2).Value = dateTimePicker2.Text.ToString();

因为在该2表中没有输入接收,就像日期的抽象图。所以你需要把它变成一个具体的字符串。对您有好处SQL Server可以接受date格式的string,如果它在yyyy-mm-dd