需要聲明使用CommandBuilder對(duì)象,你是 SQL 數(shù)據(jù)庫(kù)還是 ACCESS 數(shù)據(jù)庫(kù),這兩者的對(duì)象不一樣的。
創(chuàng)新互聯(lián)主營(yíng)普陀網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶App定制開(kāi)發(fā),普陀h5微信小程序開(kāi)發(fā)搭建,普陀網(wǎng)站營(yíng)銷(xiāo)推廣歡迎普陀等地區(qū)企業(yè)咨詢
對(duì)于:SQL server
Dim Mybuilder As SqlCommandBuilder = New SqlCommandBuilder(da)
da.Update(dt) '使用Update方法實(shí)現(xiàn)更新到數(shù)據(jù)庫(kù)
對(duì)于:ACCESS
Dim Mybuilder As OleDbCommandBuilder = New OleDbCommandBuilder(da)
da.Update(dt) '使用Update方法實(shí)現(xiàn)更新到數(shù)據(jù)庫(kù)
以下是完整模塊
Imports?System.Data
Imports?System.IO
Imports?System.Data.OleDb
Module?Module1
Public?cn?As?New?OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data?Source=C:\ACCESS數(shù)據(jù)庫(kù).mdb")?'定義連接---這里請(qǐng)更改為實(shí)際數(shù)據(jù)庫(kù)路徑及名稱
Public?DataBaseRST?As?Integer?'用來(lái)返回?cái)?shù)據(jù)庫(kù)執(zhí)行結(jié)果
Public?Function?DataModify(ByVal?str?As?String)?As?Boolean?'進(jìn)行數(shù)據(jù)庫(kù)修改操作函數(shù)
Dim?cmdinsert?As?New?OleDbCommand
Try
cmdinsert.CommandText?=?str
cmdinsert.Connection?=?cn
If?cn.State?=?ConnectionState.Closed?Then?cn.Open()
DataBaseRST?=?cmdinsert.ExecuteNonQuery()?'用來(lái)返回執(zhí)行的結(jié)果
cn.Close()
Return?True
Catch?ex?As?Exception
MessageBox.Show(Err.Description,?"Error",?MessageBoxButtons.OK,?MessageBoxIcon.Error)
Return?False
End?Try
End?Function
Public?Function?Search(ByVal?str?As?String,?ByVal?DGV?As?DataGridView)?As?Boolean?'查詢?str---查詢命令,DGV---DataGridView,用來(lái)顯示數(shù)據(jù)的控件
Dim?tb?As?New?DataTable
Try
Dim?ap?As?New?OleDb.OleDbDataAdapter(str,?cn)
ap.Fill(tb)
DGV.DataSource?=?tb
Return?True
Catch?ex?As?Exception
MessageBox.Show(Err.Description,?"Error",?MessageBoxButtons.OK,?MessageBoxIcon.Error)
Return?False
End?Try
End?Function
End?Module
'以下是調(diào)用方法
DataModify("?insert?into?aa?values?('1','2')")'-------這里是數(shù)據(jù)庫(kù)更新操作
Search("select?bb?from?aa",DataGridView1)'-----------這里是數(shù)據(jù)表查詢操作
c#
string[] strMsg = {"Field1",.....};
string[] strFieldValue = {"FieldValue1",...}
stringBuild sb = new stringBuild("update tablename set ");
int index = 0;
foreach(string var in strMsg)
{
sb .append(var + " = " + strFieldValue [index]+",");
}
sb.remove(sb.lenth-1,1);