Option Strict On
Option Explicit On
Imports System.Data.OleDb
'Adaptor操作类
Public Class cAdaptor
Private m_fName As String = Application.StartupPath & "\SMBus.mdb"
Private m_ds As New DataSet '待填充和操作的数据集
Private m_da As New OleDbDataAdapter
Public m_table As DataTable
Private m_tbName As String = "Factory"
'测试程序1
Public Sub TestUpdate1()
Create_a_Adaptor(m_tbName) '创建一个适配器
m_ds.Tables(m_tbName).Rows(0).Item("FactName") = "Girl_03" & 10 * Rnd() '修改内容
UpdateTable(m_tbName) '★★★ 若删除主键,则这里通不过 Why ??
End Sub
'测试程序2
Public Sub TestUpdate2()
m_table = Create_a_Adaptor(m_tbName)
m_table.Clear() '清除DataTable(有效)
'm_ds.Tables(m_tbName).Clear() '清除DataTable(无效)
UpdateTable(m_tbName)
End Sub
Public Sub TestUpdate3()
m_table = Create_a_Adaptor(m_tbName)
m_table.Clear()
Dim row As DataRow
Dim lsv As ListViewItem
For Each lsv In Form1.ListView1.Items '遍历listview控件,填充DataTable
row = m_table.NewRow()
row.Item("FactNo") = lsv.Index
row.Item("FactName") = lsv.Text
m_table.Rows.Add(row)
Next
PrintTable(m_table)
UpdateTable(m_tbName)
End Sub
'更新 1 个数据表
Private Function UpdateTable(ByVal m_tbName As String) As Boolean
'cDBOP.DBOperate("delete * from " & m_tbName) '★★★ 注释掉就出错,"主关键字,索引......" Why ??
m_da.Update(m_ds.Tables(m_tbName)) '★★★ 若删除数据库的主键,可以通过,但数据添加到记录的尾部,而不是更新, Why ??
m_ds.Tables(m_tbName).AcceptChanges()
End Function
'创建 1 个数据表和相应的DataAdaptor
Private Function Create_a_Adaptor(ByVal m_tbName As String) As DataTable
Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & m_fName)
m_da = New OleDbDataAdapter("select * from " & m_tbName, cn)
Dim cb As New OleDbCommandBuilder(m_da)
cn.Open()
m_da.Fill(m_ds, m_tbName)
cn.Close()
m_ds.Tables(m_tbName).TableName = m_tbName
Return m_ds.Tables(m_tbName)
End Function
End Class
stringBuilder strSql=new stringBuilder ();
strSql.Append("update table1 set A=@a,");
strSql.Append("B=@b,");
strSql.Append("C=@c");
strSql.Append(" WHERE id=@id");
string sqlUpdate = strSql.ToString();
DbCommand cbInfoUpdate = db.GetSqlStringCommand(sqlUpdate);
db.AddInParameter(cbInfoUpdate , "@A", DbType.String, "A", DataRowVersion.Current);
db.AddInParameter(cbInfoUpdate , "@b", DbType.String, "b", DataRowVersion.Current);
db.AddInParameter(cbInfoUpdate , "@c", DbType.String, "c", DataRowVersion.Current);
db.AddInParameter(cbInfoUpdate , "@id", DbType.String, "id", DataRowVersion.Current);
db.UpdateDataSet(数据源, "表", insert语句, update语句, Delete语句, 事物);
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。