用 OleDbCommandBuilder 实现 DataSet 更新到数据库

 新葡亰计算机网络     |      2020-02-07

using (OleDbConnection conn = new OleDbConnection(ConnectionString)) { conn.Open(); string sql = "SELECT id, title from news"; using (OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn)) { using (DataSet ds = new DataSet()) { adapter.Fill(ds, "news"); for (int i = 0; i < ds.Tables["news"].Rows.Count; i++) { string title = Convert.ToString(ds.Tables["news"].Rows[i]["title"]); ds.Tables["news"].Rows[i]["title"] = title + " - 千一网络"; } OleDbCommandBuilder cb = new OleDbCommandBuilder(adapter); adapter.Update(ds, "news"); // 要求表要有主键 } } conn.Close(); }

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;

很奇怪吧,OleDbCommandBuilder cb = new OleDbCommandBuilder(adapter); 这句就让 adapter.Update(ds, "news"); 这句起作用了,不过前提是要求取出来的列中有一个主键列,比如本例中的 id。

namespace TestUDL {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }

上一篇:Unity学习笔记 - uGUI 下一篇:没有了