vb2010 DataGridView insert 到 数据库
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 vb 2010 insert DataGridView --------------------编程问答-------------------- sqlStr &= "" & DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(i) & "" --------------------编程问答-------------------- 这样的话 程序都不让我调试,,报错:
错误 1 没有为类型“String”和“System.Windows.Forms.DataGridViewCell”定义运算符“&”。 --------------------编程问答--------------------
if not (DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(i) is nothing) then--------------------编程问答-------------------- 上面是ToString(),少写了g --------------------编程问答-------------------- sqlStr &= "" & DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(i).Value.ToString() & ""
sqlStr &= "" & DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(i).Value.ToStrin() & ""
end if
未将对象引用设置到对象的实例。 --------------------编程问答-------------------- 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
补充:.NET技术 , VB.NET