Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 字符串或二进制数据将在cmd.ExecuteNonQuery()上被截断_C#_Sql Server - Fatal编程技术网

C# 字符串或二进制数据将在cmd.ExecuteNonQuery()上被截断

C# 字符串或二进制数据将在cmd.ExecuteNonQuery()上被截断,c#,sql-server,C#,Sql Server,我在执行项目时出错。我在这个按钮中有两个命令。有人能帮我吗?我看不出这个问题。它以前工作正常,但现在出现了问题。我正在开发一个图书馆系统 SqlCommand cmd = con.CreateCommand(); cmd.CommandText = "INSERT INTO Borrowbook ([Student ID], ISBN, Title, Date, [Due Date]) VALUES (@StudentID, @ISBN, @Title, @Date, @DueDate)"; C

我在执行项目时出错。我在这个按钮中有两个命令。有人能帮我吗?我看不出这个问题。它以前工作正常,但现在出现了问题。我正在开发一个图书馆系统

SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "INSERT INTO Borrowbook ([Student ID], ISBN, Title, Date, [Due Date]) VALUES (@StudentID, @ISBN, @Title, @Date, @DueDate)";
Control[] controls = { textBox2, textBox3, textBox4 };

foreach (Control c in controls)
{
   if (c.Text.Trim() == "")
   {
      MessageBox.Show("Please complete the fields", "Information...",
                      MessageBoxButtons.OK, MessageBoxIcon.Warning,
                      MessageBoxDefaultButton.Button1);
      return;
   }
}

SqlParameter p1 = new SqlParameter("@StudentID", SqlDbType.NChar);
p1.Value = textBox2.Text;
cmd.Parameters.Add(p1);

SqlParameter p2 = new SqlParameter("@ISBN", SqlDbType.NVarChar);
p2.Value = textBox4.Text;
cmd.Parameters.Add(p2);

SqlParameter p3 = new SqlParameter("@Title", SqlDbType.VarChar);
p3.Value = textBox3.Text;
cmd.Parameters.Add(p3);

SqlParameter p4 = new SqlParameter("@Date", SqlDbType.DateTime);
p4.Value = dateTimePicker1.Text;
cmd.Parameters.Add(p4);

SqlParameter p5 = new SqlParameter("@DueDate", SqlDbType.DateTime);
p5.Value = duedate.Text;
cmd.Parameters.Add(p5);

cmd.ExecuteNonQuery();

cmd.CommandText = ("UPDATE Books SET Availability = 'Borrowed' WHERE ISBN = '" + textBox4.Text + "'");
cmd.ExecuteNonQuery();

cmd.CommandText = ("INSERT INTO Penalty([Student ID], Title, [Date Borrow], [Due Date]) VALUES  (@StudentID1, @Title1, @Date1, @Date2)");

SqlParameter pp1 = new SqlParameter("@StudentID1", SqlDbType.VarChar);
pp1.Value=textBox2.Text;
cmd.Parameters.Add(pp1);

SqlParameter pp3 = new SqlParameter("@Title1", SqlDbType.NVarChar);
pp3.Value = textBox3.Text;
cmd.Parameters.Add(pp3);

SqlParameter pp4 = new SqlParameter("@Date1", SqlDbType.DateTime);
pp4.Value = dateTimePicker1.Text;
cmd.Parameters.Add(pp4);

SqlParameter pp5 = new SqlParameter("@Date2",SqlDbType.DateTime);
pp5.Value=duedate.Text;
cmd.Parameters.Add(pp5);

cmd.ExecuteNonQuery();
MessageBox.Show("The books has been successfully borrowed!", "Information ... ", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);

首先:这个错误到底发生在哪里?您的代码中有多个命令-哪个命令会导致错误

第二:错误很明显:您试图存储的值中至少有一个太长。因此,您可能在数据库中定义了一个
Varchar(20)
,并试图存储一个长度为25个字符的字符串

找到这个问题,解决它。使数据库列变长(例如,将其长度增加到25个字符),或缩短插入该列的字符串,以符合定义的最大长度

不幸的是,SQL Server并不能真正帮助您找出问题出在哪个参数上——您必须自己进行调试,看看哪个参数太长

cmd.ExecuteNonQuery();
MessageBox.Show("The books has been successfully borrowed!", "Information ... ",MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);

有趣的是,您如何知道“书籍已成功借阅”-您没有处理执行命令时可能产生的任何错误。

错误出现在最后一个cmd.ExecuteNonQuery()上;现在可以了,另一张表太短了。这应该是评论,而不是回答。如果命令失败,
ExecuteNonQuery()
抛出异常,因此它不会进入
MessageBox.Show
调用。