我的 4 种数据库类型的 VB.NET 代码(将信息从 DataGridView 更新到数据库)
Private Sub sqldb_savedata()
Dim connectionString As String = "Server='" & sql_server & "';Database='" & sql_database & "';User Id='" & sql_user & "';Password='" & sql_pass & "'"
Dim sqlCon = New SqlConnection(connectionString)
If (sqlCon.State = ConnectionState.Closed) Then sqlCon.Open()
Dim SQLAdapter = New SqlDataAdapter("SELECT * FROM clinics", sqlCon)
Dim SQLDataSet As New DataSet
Dim myTable = DataGridViewClinic.DataSource
Dim cmdbuilder As New SqlCommandBuilder(SQLAdapter)
SQLAdapter.Update(myTable, "clinics")
MsgBox("Updated!", MsgBoxStyle.OkOnly, "")
End Sub
Private Sub mysqldb_savedata()
Dim connectionString As String = "Server='" & mysql_server & "';Database='" & mysql_database & "';User Id='" & mysql_user & "';Password='" & mysql_pass & "'"
Dim sqlCon = New MySqlConnection(connectionString)
If (sqlCon.State = ConnectionState.Closed) Then sqlCon.Open()
Dim SQLAdapter = New MySqlDataAdapter("SELECT * FROM clinics", sqlCon)
Dim SQLDataSet As New DataSet
Dim myTable = DataGridViewClinic.DataSource
Dim cmdbuilder As New MySqlCommandBuilder(SQLAdapter)
SQLAdapter.Update(myTable, "clinics")
MsgBox("Updated!", MsgBoxStyle.OkOnly, "")
End Sub
Private Sub firebirddb_savedata()
Dim connectionString As String = "Database='" & firebird_server & "';User=SYSDBA;Password=masterkey;Dialect=3;ServerType=1"
Dim sqlCon = New FirebirdSql.Data.FirebirdClient.FbConnection(connectionString)
If (sqlCon.State = ConnectionState.Closed) Then sqlCon.Open()
Dim SQLAdapter = New FirebirdSql.Data.FirebirdClient.FbDataAdapter("SELECT * FROM clinics", sqlCon)
Dim SQLDataSet As New DataSet
Dim myTable = DataGridViewClinic.DataSource
Dim cmdbuilder As New FirebirdClient.FbCommandBuilder(SQLAdapter)
SQLAdapter.Update(myTable, "clinics")
MsgBox("Updated!", MsgBoxStyle.OkOnly, "")
End Sub
Private Sub localdb_savedata()
DBconn = New SqlCeConnection("Data Source=Data Source=|DataDirectory|\Database.sdf")
If (DBconn.State = ConnectionState.Closed) Then DBconn.Open()
Dim SQLAdapter = New SqlCeDataAdapter("SELECT * FROM clinics", DBconn)
Dim SQLDataSet As New DataSet
Dim myTable = DataGridViewClinic.DataSource
Dim cmdbuilder As New SqlCeCommandBuilder(SQLAdapter)
SQLAdapter.Update(myTable, "clinics")
MsgBox("Updated!", MsgBoxStyle.OkOnly, "")
End Sub