【问题标题】:VB.NET SQL - String or binary data would be truncated. The statement has been terminatedVB.NET SQL - 字符串或二进制数据将被截断。该语句已终止
【发布时间】:2014-06-16 07:53:54
【问题描述】:

我是新手,在运行我的表单时遇到以下错误。 '字符串或二进制数据将被截断。语句已终止。'

这是表单的代码

<form runat="server">
        <table style="width:500px">
            <tr>
                <td>Customer Name*: </td>
                <td><asp:TextBox ID="CustomerName" runat="server" MaxLength="50"></asp:TextBox></td>
                <td><asp:RequiredFieldValidator id="rfvValidator" runat="server" Enabled="true" ControlToValidate="CustomerName" ErrorMessage="Customer Name is Required." ></asp:RequiredFieldValidator></td>
            </tr>
            <tr>
                <td>Address 1: </td>
                <td><asp:TextBox ID="Address1" runat="server" MaxLength="100"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Address 2:</td> 
                <td><asp:TextBox ID="Address2" runat="server" MaxLength="100"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Address 3:</td> 
                <td><asp:TextBox ID="Address3" runat="server" MaxLength="100"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Town: </td>
                <td><asp:TextBox ID="Town" runat="server" MaxLength="100"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Country:</td> 
                <td><asp:TextBox ID="Country" runat="server" MaxLength="50"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Postcode:</td>
                <td><asp:TextBox ID="PostCode" runat="server" MaxLength="8"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Telephone:</td> 
                <td><asp:TextBox ID="Telephone" runat="server" MaxLength="50"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Fax:</td> 
                <td><asp:TextBox ID="Fax" runat="server" MaxLength="50"></asp:TextBox></td>
            </tr>
        </table>
                <asp:Button id="btnSave" Text="Save" runat="server" />
                <input type=button name="cancel" value="Cancel" onClick="parent.jQuery.fancybox.close()">
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </form>

这是我的提交按钮代码

 Imports System.Data
 Imports System.Data.SqlClient

 Public Class EditCustomer
     Inherits System.Web.UI.Page

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     End Sub

     Dim con As SqlConnection
     Dim cmd As SqlCommand

     Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
         con = New SqlConnection("Data Source=IPAddress;Initial Catalog=medical01;Persist Security Info=True;User ID=******;Password=******")
         con.Open()
         cmd = New SqlCommand("INSERT INTO Customer_tbl (customername,address1,address2,address3,town,country,postcode,telephone,fax,isDeleted) VALUES('" & CustomerName.Text & "','" & Address1.Text & "','" & Address2.Text & "','" & Address3.Text & "','" & Town.Text & "', " & PostCode.Text & ", " & Telephone.Text & "," & Fax.Text & ", 1)", con)
         cmd.ExecuteNonQuery()
         Label1.Text = "Customer Added."
         con.Close()
     End Sub
 End Class

这是我的数据库:

我收到此错误:

String or binary data would be truncated.

语句已终止。

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.Data.SqlClient.SqlException:字符串或二进制数据将被截断。 声明已终止。

【问题讨论】:

  • 可以将customerid设置为自增
  • 不这样认为,如果我从代码中获取 SQL 并添加值并将其直接运行到 SQL 中,则插入工作并分配正确的 ID。

标签: asp.net sql vb.net


【解决方案1】:

您没有引用所有字段:

cmd = New SqlCommand("INSERT INTO Customer_tbl (customername,address1,address2,address3,town,country,postcode,telephone,fax,isDeleted)"
& "VALUES('" & CustomerName.Text & "','" & Address1.Text & "','" & Address2.Text & "','" & Address3.Text & "','" & Town.Text & "', '" & PostCode.Text & "', '" & Telephone.Text & "','" & Fax.Text & "', 1)", con)

我将它们添加到 PostCodeTelephoneFax。这一次将帮助你,但你应该真正使用参数。它会让你的生活更轻松,这句话会更好:

cmd = New SqlCommand("INSERT INTO Customer_tbl (customername,address1,address2,address3,town,country,postcode,telephone,fax,isDeleted)"
& "VALUES(@customername,@address1,@address2,@address3,@town,@country,@postcode,@telephone,@fax,@isDeleted)", con)

cmd.Parameters.AddWithValue("customername", CustomerName.Text)
cmd.Parameters.AddWithValue("address1", Address1.Text)
cmd.Parameters.AddWithValue("address2", Address2.Text)
cmd.Parameters.AddWithValue("address3", Address3.Text)
cmd.Parameters.AddWithValue("town", Town.Text)
cmd.Parameters.AddWithValue("country", Country.Text)
cmd.Parameters.AddWithValue("postcode", PostCode.Text)
cmd.Parameters.AddWithValue("telephone", Telephone.Text)
cmd.Parameters.AddWithValue("fax", Fax.Text)
cmd.Parameters.AddWithValue("isDeleted", 1)

【讨论】:

  • 我意识到我写的是 c# 而不是 VB.NET,所以我更新了代码。不过应该有那么多改变。
  • 不,这是完美的,工作享受。感谢您的宝贵时间。
猜你喜欢
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
  • 2013-03-19
  • 1970-01-01
  • 1970-01-01
  • 2012-11-03
相关资源
最近更新 更多