SQL 字符串或二进制数据将被截断。该语句已终止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8698967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String or binary data would be truncated. The statement has been terminated
提问by Mike Jhy
I always got an error when adding a new data. the error says
添加新数据时总是出错。错误说
String or binary data would be truncated. The statement has been terminated
字符串或二进制数据将被截断。该语句已终止
As I've looked back on my backend or code. It looks like there's a conflict adding a TWO LABEL DATA
in one column because I would like to join the (Year)-(StudentNumber)
正如我回顾我的后端或代码。TWO LABEL DATA
由于我想加入 (Year)-(StudentNumber),因此在一列中添加 a 似乎存在冲突
Here's the code of my INSERT INTO Statement
这是我的 INSERT INTO 语句的代码
INSERT INTO
[Student_Information] (StudentID, LastName, FirstName, MiddleName, Gender,
ContactNumber, Citizenship, Religion, Birthday, Address)
VALUES
( '" & lbl_cyear.Text - studid.Text & "','" + txt_lname.Text + "', '" + txt_fname.Text + "', '" + txt_mname.Text + "', '" + DDGender.Text + "', '" & txt_cnumber.Text & "', '" & txt_citizenship.Text & "' , '" + txt_religion.Text + "' , '" & txt_bday.Text & "', '" & txt_address.Text & "' )"
and here's the code how I generate the Year and the Student Number
这是我如何生成年份和学生编号的代码
Sub SNYear()
Dim test As Date
test = Convert.ToDateTime(Today)
lbl_cyear.Text = test.Year
End Sub
Sub SNGenerate()
'displaying Studentid
Dim SN As Integer ' Student Number
Dim SID As String 'Student ID Num as String
Dim rdr As SqlDataReader
cmd1.Connection = cn
cmd1.Connection.Open()
cmd1.CommandText = "Select Max (StudentID) as expr1 from [Student_Information]"
rdr = cmd1.ExecuteReader
If rdr.HasRows = True Then
rdr.Read()
End If
If rdr.Item(0).ToString = Nothing Then
SN = rdr.Item(0) + 1
SID = Format(SN, "0000")
ElseIf rdr.Item(0).ToString = 0 Then
SN = rdr.Item(0) + 1
SID = Format(SN, "0000")
Else
SN = rdr.Item(0) + 1
SID = Format(SN, "0000")
End If
studid.Text = SID
cmd1.Connection.Close()
End Sub
Can someone help me with the code? How to join 2 data in different label text and save it to one column in my table.
有人可以帮我写代码吗?如何连接不同标签文本中的 2 个数据并将其保存到我的表中的一列。
回答by Anders Tornblad
Woah! Never ever write sql queries like that. It's subject to dangerous SQL injection, and code like that is actually used as worst-case scenarios in SQL injection lectures everywhere!
哇!永远不要写这样的 sql 查询。它受到危险的 SQL 注入的影响,而这样的代码实际上在各地的 SQL 注入讲座中都被用作最坏的情况!
That being said, the error message String or binary data would be truncated. The statement has been terminated.actually spells out what is wrong. You are trying to insert too much data into a field that has a specific dimension.
话虽如此,错误消息字符串或二进制数据将被截断。该语句已终止。实际上说明出了什么问题。您正试图在具有特定维度的字段中插入过多数据。
You are trying to insert the following expression into the StudentID
field:
您正在尝试将以下表达式插入该StudentID
字段:
lbl_cyear.Text - studid.Text
I'm not even sure what you want to do there. Since Visual Basic is loosely typed by default, It will probably handle the lbl_cyear.Text
as a number and try to subtract the studid.Text
(as a number)from it. You probably mean something like this:
我什至不确定你想在那里做什么。由于默认情况下 Visual Basic 是松散类型的,它可能会将 thelbl_cyear.Text
作为数字处理并尝试从中减去studid.Text
(作为数字)。你的意思可能是这样的:
lbl_cyear.Text & "-" & studid.Text
It seems you are trying to use the StudentID
column for two different types of information. Don't do that. Decide on one format for the student ids and dimension the column for it.
您似乎正在尝试将该StudentID
列用于两种不同类型的信息。不要那样做。确定学生 ID 的一种格式并为其设置列的尺寸。
Is this a homework assignment?
这是家庭作业吗?
回答by Origin
Y@atornblad has some very valid points about re-structuring your code. However, the specific problem you are asking about is likely because you are trying to insert data into a column that is longer than the column can accept.
Y@atornblad 有一些关于重新构建代码的非常有效的观点。但是,您询问的具体问题可能是因为您试图将数据插入到长度超过该列可接受的列中。
E.g. - you are trying to insert "Lorem Ipsum" into a column that has a maximum length of 5 characters.
例如 - 您正在尝试将“Lorem Ipsum”插入到最大长度为 5 个字符的列中。
EditYou need to take another look at how your table is defined and make sure it is appropriate for the data you are storing. Also - make sure the data you are trying to store is in the correct format that the table was designed for. Don't assume it's in the format you want, actually step through the program in debug mode and look at what the variable is before it gets sent to the database.
编辑您需要再看看您的表是如何定义的,并确保它适合您存储的数据。另外 - 确保您尝试存储的数据采用表格设计的正确格式。不要假设它是您想要的格式,实际上在调试模式下逐步执行程序,并在将变量发送到数据库之前查看它是什么。