c#连接Access(含有插入日期类型)

来源:互联网 发布:手机淘宝怎么退货申请 编辑:程序博客网 时间:2024/04/28 13:38
 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.Data.OleDb; 
  6. namespace We_Are_AA_CSharp_ACCESS_
  7. {
  8.     class BuyThingTable
  9.     {
  10.         public string mydataSource = "WeAreAA.mdb";
  11.         public string connstr = "Provider=Microsoft.jet.OLEDB.4.0;Data Source=WeAreAA.mdb";
  12.         //获得购物表
  13.         public DataSet getbuyThingTable()
  14.         {
  15.             DataSet ds = new DataSet();
  16.             string cmdStr = "select id,buyUserName,ThingName,moneyTotal,buyDate from buyThingTable order by buyDate desc";
  17.             OleDbConnection conn = new OleDbConnection(connstr);
  18.             OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmdStr, conn);
  19.             dataAdapter.Fill(ds, "buyThingTable");
  20.             return ds;
  21.         }
  22.         //获得购物表by日期
  23.         public DataSet getbuyThingTableByDate( DateTime fromDate)
  24.         {
  25.             DataSet ds = new DataSet();
  26.             string cmdStr = "select id,buyUserName,ThingName,moneyTotal,buyDate from buyThingTable where buyDate >= #" + fromDate + "#  order by buyDate desc";
  27.             OleDbConnection conn = new OleDbConnection(connstr);
  28.             OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmdStr, conn);
  29.             dataAdapter.Fill(ds, "buyThingTable");
  30.             return ds;
  31.         }
  32.         //人员分组获得本人代付钱数表
  33.         public DataSet getBuyThingTableByDateGroup(DateTime fromDate)
  34.         {
  35.             DataSet ds = new DataSet();
  36.             string cmdStr =  "select  buyUserName,sum(moneyTotal) from buyThingTable 
  37.                                          where buyDate >= #" + fromDate + "# group by buyUserName "
  38.             OleDbConnection conn = new OleDbConnection(connstr);
  39.             OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmdStr, conn);
  40.             dataAdapter.Fill(ds, "buyThingTable");
  41.             return ds;
  42.         }
  43.         //获得id,以便删除buyThingTable中某记录时,同时删除shareUserTable中的相应项
  44.         public Int32 getbuyThingTableId(string buyUserName, string thingName, float moneyTotal, DateTime buyDate)
  45.         {
  46.             DataSet ds = new DataSet();
  47.             string cmdStr = "select id from buyThingTable where buyUserName='" + buyUserName + "'and thingName='" + thingName + "' and moneyTotal=" + moneyTotal + "  and buyDate = #" + buyDate + "#";
  48.             OleDbConnection conn = new OleDbConnection(connstr);
  49.             OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmdStr, conn);
  50.             dataAdapter.Fill(ds, "theId");
  51.             DataTable dt = ds.Tables["theId"];
  52.            
  53.            return Int32.Parse(dt.Rows[0].ItemArray[0].ToString());
  54.       
  55.         }
  56.         //删除购物表记录by"ID"
  57.         public bool delRecordById(Int32  id)
  58.         {
  59.             string cmdstr = "delete from buyThingTable where [id] = " + id + "";
  60.             using (OleDbConnection conn = new OleDbConnection(connstr))
  61.             {
  62.                 OleDbCommand ocmd = new OleDbCommand(cmdstr, conn);
  63.    
  64.                     conn.Open();
  65.                     ocmd.ExecuteNonQuery();
  66.                     conn.Close();
  67.                     return true;
  68.             }
  69.         }
  70.        public void AddbuyThingTable(string buyUserName, string thingName, float  moneyTotal, DateTime buyDate)
  71.         {
  72.             string cmdstr = "insert into buyThingTable(buyUserName, thingName, moneyTotal, buyDate) values('" + buyUserName + "','" + thingName + "'," + moneyTotal + ",#" + buyDate + "#)";
  73.             
  74.             using (OleDbConnection conn = new OleDbConnection(connstr))
  75.             {
  76.                 OleDbCommand ocmd = new OleDbCommand(cmdstr, conn);
  77.                 conn.Open();
  78.                 ocmd.ExecuteNonQuery();
  79.                 conn.Close(); 
  80.             }
  81.         }
  82.     }
  83. }
原创粉丝点击