Dim sqlStr As String = "insert into tb values("
Dim i As Integer
For i = 0 To DataGridView1.Columns.Count - 1
sqlStr &= "'" &DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(i) &"'"
If i <DataGridView1.Columns.Count - 1 Then sqlStr &= ","
Next
sqlStr &= ")"
Dim con As New SqlConnection(connectionString) '这个自己写去
con.Open()
Dim cmd As New SqlCommand(sqlStr, con)
Try
Dim result As Integer = cmd.ExecuteNonQuery()
If result >0 Then
MsgBox "添加数据成功"
Else
MsgBox "添加数据失败"
End If
Catch (ex As Exception)
MsgBox "添加数据过程发生异常,原因:" &ex.Message
End Try
End Sub
你这是向sql后台数据库写数据吗?sqldataadapter1.update 改须指定有效的updatecommand,我一直没有看到
给你个例子你看吧:
'首先指定有效的UpdateCommand
sqlstr = "insert into Playlist(PlayName,PlayPath)values('" &TextBox4.Text.Trim &"','" &TextBox5.Text.Trim &"')"
Module1.Mycommand.CommandText = sqlstr
'增加新一行(此行必须,要不不能与后台同步,并且后面调用update会出错)
Module1.MyDataSet.Tables("Music").Rows.Add()
Module1.MyDataAdaPter.InsertCommand = Module1.Mycommand
Module1.MyDataAdaPter.Update(Module1.MyDataSet, "Music")
MsgBox("增加数据成功!!")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)