使用vb.net向sql server数据库插入数据时出错

发布于 2024-12-09 02:46:48 字数 1486 浏览 1 评论 0原文

我正在使用以下代码将数据插入表中,并且在 cmd.executenonquery() 处收到错误...该异常被标记为 SQL 异常,并且底层消息显示“字符串或二进制数据将被截断”声明已终止。”

        Dim str As String  ' defines str as a string variable

        'takes insertion query as a string in str variable
        str = "Insert into Instructors Values(@inst_id, @inst_name, @contact, @game, 'N/A', 'N/A', 'Available')"

        'defines a new command which takes query string and connection string as parameters
        Dim cmd As New SqlCommand(str, con)

        ' defines Instructor ID parameter and takes its value from the form 
        Dim prmInstID As New SqlParameter("@inst_id", SqlDbType.Int)
        prmInstID.Value = TxtInstID.Text

        ' defines Instructor Name parameter and takes its value from the form 
        Dim prmInstName As New SqlParameter("@inst_name", SqlDbType.Char)
        prmInstName.Value = TxtInstName.Text

        ' defines Contact parameter and takes its value from the form 
        Dim prmContact As New SqlParameter("@contact", SqlDbType.VarChar)
        prmContact.Value = MskdTxtCntctno.Text


        ' defines Specialty Game parameter and takes its value from the form 
        Dim prmGame As New SqlParameter("@game", SqlDbType.VarChar)
        prmGame.Value = Me.ComboBox1.SelectedItem

        cmd.Parameters.Add(prmInstID)
        cmd.Parameters.Add(prmInstName)
        cmd.Parameters.Add(prmContact)
        cmd.Parameters.Add(prmGame)


        cmd.ExecuteNonQuery()

        con.Close()

I'm using the following code to insert the data in a table and i am getting the error at cmd.executenonquery()....The exception is labelled as SQL exception and the underlying message says "String or binary data would be truncated. The statement has been terminated."

        Dim str As String  ' defines str as a string variable

        'takes insertion query as a string in str variable
        str = "Insert into Instructors Values(@inst_id, @inst_name, @contact, @game, 'N/A', 'N/A', 'Available')"

        'defines a new command which takes query string and connection string as parameters
        Dim cmd As New SqlCommand(str, con)

        ' defines Instructor ID parameter and takes its value from the form 
        Dim prmInstID As New SqlParameter("@inst_id", SqlDbType.Int)
        prmInstID.Value = TxtInstID.Text

        ' defines Instructor Name parameter and takes its value from the form 
        Dim prmInstName As New SqlParameter("@inst_name", SqlDbType.Char)
        prmInstName.Value = TxtInstName.Text

        ' defines Contact parameter and takes its value from the form 
        Dim prmContact As New SqlParameter("@contact", SqlDbType.VarChar)
        prmContact.Value = MskdTxtCntctno.Text


        ' defines Specialty Game parameter and takes its value from the form 
        Dim prmGame As New SqlParameter("@game", SqlDbType.VarChar)
        prmGame.Value = Me.ComboBox1.SelectedItem

        cmd.Parameters.Add(prmInstID)
        cmd.Parameters.Add(prmInstName)
        cmd.Parameters.Add(prmContact)
        cmd.Parameters.Add(prmGame)


        cmd.ExecuteNonQuery()

        con.Close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥抱影子 2024-12-16 02:46:48

当数据库中的文本字段太小而无法容纳要插入的数据时,通常会发生此异常。例如,如果数据库中教师姓名字段的长度为 10 个字符,而 TxtInstName.Text 中的文本长度超过 10 个字符,您将收到此错误。

要解决此问题,您可以增大数据库中的字段或检查表单中的文本是否太长。

This exception usually occures when your text field in the database is too small to fit the data you want to insert. For example if the instructor name field in the database has a length of 10 (characters) and the text from TxtInstName.Text has more than 10 characters, you will get this error.

To solve this you can make the field in the database bigger or check inside your form that the text is not too long.

似狗非友 2024-12-16 02:46:48

正如 aKzen 所说,错误消息可能是因为您尝试插入的内容太长,无法放入您的字段之一。

我只是想我会添加一些指针,因为我假设您仍在学习等。如果您使用存储过程,您可以大量减少代码,并且还可以提高应用程序的效率。

存储过程给您带来了一些好处,它们更安全,因为您不通过网络传输查询(如果有人拦截您的查询,他们可以获得有关您的表结构的信息)此外,存储过程不必在每次执行时重新编译与传递查询字符串不同,服务器每次收到查询字符串时都必须编译查询。此外,使用 SP 可以让您在多个位置重复使用相同的查询,而无需复制粘贴您的查询字符串。

以下是我编写代码的方法。

' Declare a new command
Dim cmd As New SqlCommand

' Set the type to Stored Proc and Tell it which connection to use
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "spMySpNameGoesHERE"
cmd.Connection = con

' Add the parameter and its value
cmd.Parameters.AddWithValue("@ParamName",TxtInstID.Text ) 
' More Params go here

cmd.ExecuteNonQuery() 

con.Close() 


CREATE PROCEDURE spMySpNameGoesHERE
(
@inst_id INT,
@inst_name VARCHAR(50),
etc etc
)
AS
BEGIN

Insert into 
Instructors 
Values(@inst_id, @inst_name, @contact, @game, 'N/A', 'N/A', 'Available')        

END

As aKzen said the error message is probably because you are trying to insert something which is too long to fit in one of your fields.

I just thought I would add to this some pointers as I would assume you are still learning etc. If you use stored procedures you could cut down your code a lot, and also increase the efficiency of your application.

Stored Procedures give you a few benefits, they are more secure as you are not transmitting query across the network (if someone intercepts your query they can gain knowledge about your table structure) Also Stored procedures do not have to be recompiled every time they are executed unlike passing a query string where the server has to compile the query each time it receives it. Also using a SP lets you reuse the same query in multiple places without having to copy paste your queryString.

Below is the way I would approach writing the code.

' Declare a new command
Dim cmd As New SqlCommand

' Set the type to Stored Proc and Tell it which connection to use
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "spMySpNameGoesHERE"
cmd.Connection = con

' Add the parameter and its value
cmd.Parameters.AddWithValue("@ParamName",TxtInstID.Text ) 
' More Params go here

cmd.ExecuteNonQuery() 

con.Close() 


CREATE PROCEDURE spMySpNameGoesHERE
(
@inst_id INT,
@inst_name VARCHAR(50),
etc etc
)
AS
BEGIN

Insert into 
Instructors 
Values(@inst_id, @inst_name, @contact, @game, 'N/A', 'N/A', 'Available')        

END
幸福不弃 2024-12-16 02:46:48

此错误是因为您在一个或多个字段中输入的字符数多于数据库表列的大小。

例如,如果表中有一个名称列,其数据类型和长度为 VARCHER(10)。
如果您在名称栏中输入超过 10 个字符,您将收到此错误。

解决方案:
增加柱的长度。例如 25 或根据您的要求。

如果文本框是文本框,则限制文本框中的字符数,即 MaxLength="25"

This error is because you are entering larger number of characters in one or more fields than the size of DB table columns.

for example if you have a name column in a table with VARCHER(10) data type and length.
If you enter more than 10 characters in name column then you will receive this error.

Solution:
increase the length of column. e.g 25 or up to your requirement.
and
restrict number of characters in your text box saying MaxLength="25" if it is a textbox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文