您的位置:首页 > 其它

哪位大虾帮个忙....大家都来看下...谢谢

2007-04-21 21:07 459 查看
using System;
using System.Data;
using System.Data.OleDb;

public class chibang
{
 public static void Main(string[] args)
 {
  try
  {
   OleDbConnection conn = new OleDbConnection();
   conn.ConnectionString = @"provider = microsoft.jet.oledb.4.0;data source = ../../../d.mdb";
   conn.Open();

   OleDbDataAdapter da = new OleDbDataAdapter();
   da.SelectCommand = new OleDbCommand("select * from f",conn);
   DataSet ds = new DataSet();
   da.Fill(ds,"f");
   //  Console.WriteLine(ds.Tables["f"].Rows[1]["ID"]);
   //  ds.Tables["f"].Rows[1]["ID"] = "33";
   //  Console.WriteLine(ds.Tables["f"].Rows[1]["ID"]);
   DataColumn[] keys = new DataColumn[2];
   keys[0] = ds.Tables["f"].Columns["ID"];
   keys[1] = ds.Tables["f"].Columns["di"];

   string[] name = {"2222","hack"};
   ds.Tables["f"].PrimaryKey = keys;
   DataRow dr = ds.Tables["f"].Rows.Find(name);
   if(dr == null)
   {
    DataRow dn = ds.Tables["f"].NewRow();
    dn["ID"] = name[0];
    dn["di"] = name[1];
    ds.Tables["f"].Rows.Add(dn);
   }
   
   da.Update(ds,"f");
   conn.Close();
   //Console.WriteLine(ds.Tables["f"].Rows[1]["ID"]);
   Console.ReadLine();
  }
  catch(Exception ex)
  {
   Console.WriteLine(ex.Message);
   
   Console.ReadLine();
  }
 }


 

 

最后运行完的结果是:当传递具有新行的  DataRow 集合时,更新要求有效的 InsertCommand....
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息