首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

vb.net中怎么将textbox的""值保存为数据库的NULL

2012-03-11 
vb.net中如何将textbox的值保存为数据库的NULL数据库中建有一字段ppp,类型设为decimal或numeric用vb.net

vb.net中如何将textbox的""值保存为数据库的NULL
数据库中建有一字段ppp,类型设为decimal或numeric

用vb.net中控件textbox对其插入数据,当textbox为""时,提示类型转换错误

Dim comm As New SqlCommand("insert into ppt(ppp)values('" & textbox1.text & "')", xl)
  comm.ExecuteNonQuery()

现在我知道以下一段代码可以解决,但不知如何结合我上面的代码使用

IIf(TextBox1.Text = "", DBNull.Value, TextBox1.Text) '如果textbox1="",为真时返回null,为假时返回自身值。

[解决办法]
Dim comm As New SqlCommand("insert into ppt(ppp)values(nullif(" & textbox1.text & ",''))", xl)
comm.ExecuteNonQuery()
改成这样试试

不过好象改成这也行

Dim comm As New SqlCommand("insert into ppt(ppp)values(" & textbox1.text & ")", xl)
comm.ExecuteNonQuery()

[解决办法]
用 SqlParameter

http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlcommand.parameters(v=VS.80).aspx

sql应为:"insert into ppt (ppp) values (@ppp)"
[解决办法]
单纯拼字符串的话,可以这么写
Dim value as String = IIf(TextBox1.Text = "", "null", "'" & TextBox1.Text &"'") 
Dim sql as String = String.Format("insert into ppt(ppp) values ({0})", value)

热点排行