【问题标题】:Visual Basic connected to sql database. Insert statement gives me an error of "string or binary data would be truncated."Visual Basic 连接到 sql 数据库。插入语句给我一个错误“字符串或二进制数据将被截断”。
【发布时间】:2017-02-01 07:50:42
【问题描述】:

[这是我的 Visual Basic 代码]

    Dim Command As New SqlCommand
    Command.Connection = CN
    Dim ms As New MemoryStream
    ConON()

    Command.CommandText = "Insert into tbl_profile(Lastname,Firstname,Gender,Birthdate,ContactNum," & _
                  "Housenumstreet,Brgydistrict,Cityprovince) values ('" & AddProfile.tbxlname.Text & "','" & AddProfile.tbxfname.Text & _
                  "','female','" & AddProfile.tbxbdate.Text & "','" & AddProfile.tbxcontactnum.Text & _
                 "','" & AddProfile.tbxhousenostrt.Text & "','" & AddProfile.tbxbrgy.Text & "','" & AddProfile.tbxcity.Text & "')"

    Command.ExecuteNonQuery()

还有我在 sql 上的代码:

Create table tbl_profile
(
[IdentityID] bigint identity(1,1),
Lastname varchar(max),
Firstname varchar(max),
Gender varchar null,
Birthdate varchar(max) null,
ContactNum varchar(max) null,
Housenumstreet varchar(max) null,
Brgydistrict varchar(max) null,
Cityprovince varchar(max) null,
Picture1 Image,
Picture2 Image null
)

【问题讨论】:

  • 请将您的代码发布为文本而不是图像

标签: sql visual-studio-2012 insert truncated


【解决方案1】:

您没有为“性别”列指定长度。默认情况下,varchar 的默认长度约为 30 个字符,但如果您的文本有空格,它很可能会超过该长度。

编辑

提供一些您尝试插入的示例值。另外,这个错误是仅在您从 VB 执行时出现,还是在您直接通过 SMSS 插入 SAME 值时出现错误?

尝试这样的示例,可能没有图像

if not (OBJECT_ID('tempdb..#tbl_profile') is null)
    drop table #tbl_profile
create table #tbl_profile
(
    IdentityID int identity(1,1),
    LastName varchar(max) null,
    FirstName varchar(max) null,
    Gender varchar(50) null,
    Birthdate datetime null,
    ContactNum varchar(max) null,
    Housenumstreet varchar(max) null,
    Brgydistrict varchar(max) null,
    Cityprovince varchar(max) null,
    Picture1 image null,
    Picture2 image null,
    primary key clustered
    (
        IdentityID asc
    )
)

insert into
#tbl_profile
(
    LastName,
    FirstName,
    Gender,
    Birthdate,
    ContactNum,
    Housenumstreet,
    Brgydistrict,
    Cityprovince
)
select
    'Doe',
    'John',
    'Male',
    '2017-02-01',
    '12345678900',
    'Some random street somehwhere out there',
    'District 13',
    'Province of Glory'

select * from #tbl_profile

【讨论】:

  • 我已经尝试在没有性别列的情况下执行它,但它仍然不起作用。 TT
  • 它只在我的vb中。我可以在我的数据库查询中很好地插入值。 T_T
猜你喜欢
  • 1970-01-01
  • 2017-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-28
  • 2015-04-29
相关资源
最近更新 更多