vb2010 DataGridView insert 到 数据库

erichfromm 2013-03-22 12:42:57
sqlStr &= "" & CType(DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(i), Object) & ""
提示错误信息为:
没有为 字符串“” 和 类型“DataGridViewTextBoxCell” 定义运算符“&”。

数据库表:字段类型都已经改成了nchar(10)

以下是click事件代码:

Private Sub 保存_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 保存.Click
Dim sqlStr As String = "insert into 表1 values("
Dim i As Integer
For i = 0 To DataGridView1.Columns.Count - 1
sqlStr &= "" & CType(DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(i),Object) & ""
If i < DataGridView1.Columns.Count - 1 Then sqlStr &= ","
Next
sqlStr &= ")"
Dim cmd As 数据命令类DBCommand = New 数据命令类DBCommand(New 数据库连接类ConnectionString().连接信息ConnectionInfo)
If cmd.Insert(sqlStr) > 0 Then
MsgBox("保存成功!", MsgBoxStyle.DefaultButton1, "业务模块")
Else
MsgBox("保存失败!请联系系统管理员。")
End If
End Sub
...全文
96 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
gxingmin 2013-03-22
  • 打赏
  • 举报
回复
上面是ToString(),少写了g
gxingmin 2013-03-22
  • 打赏
  • 举报
回复
if not (DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(i) is nothing) then
sqlStr &= "" & DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(i).Value.ToStrin() & "" 
end if
erichfromm 2013-03-22
  • 打赏
  • 举报
回复
这样的话 程序都不让我调试,,报错: 错误 1 没有为类型“String”和“System.Windows.Forms.DataGridViewCell”定义运算符“&”。
gxingmin 2013-03-22
  • 打赏
  • 举报
回复
sqlStr &= "" & DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(i) & ""
gxingmin 2013-03-22
  • 打赏
  • 举报
回复
if not (DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(i).Value is nothing) then sqlStr &= "" & DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(i).Value.ToString() & "" end if
erichfromm 2013-03-22
  • 打赏
  • 举报
回复
sqlStr &= "" & DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(i).Value.ToString() & "" 未将对象引用设置到对象的实例。

16,720

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧