c#连接Access(含有插入日期类型)
来源:互联网 发布:手机淘宝怎么退货申请 编辑:程序博客网 时间:2024/04/28 13:38
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Data;
- using System.Data.OleDb;
- namespace We_Are_AA_CSharp_ACCESS_
- {
- class BuyThingTable
- {
- public string mydataSource = "WeAreAA.mdb";
- public string connstr = "Provider=Microsoft.jet.OLEDB.4.0;Data Source=WeAreAA.mdb";
-
- public DataSet getbuyThingTable()
- {
- DataSet ds = new DataSet();
- string cmdStr = "select id,buyUserName,ThingName,moneyTotal,buyDate from buyThingTable order by buyDate desc";
- OleDbConnection conn = new OleDbConnection(connstr);
- OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmdStr, conn);
- dataAdapter.Fill(ds, "buyThingTable");
- return ds;
- }
-
- public DataSet getbuyThingTableByDate( DateTime fromDate)
- {
- DataSet ds = new DataSet();
- string cmdStr = "select id,buyUserName,ThingName,moneyTotal,buyDate from buyThingTable where buyDate >= #" + fromDate + "# order by buyDate desc";
- OleDbConnection conn = new OleDbConnection(connstr);
- OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmdStr, conn);
- dataAdapter.Fill(ds, "buyThingTable");
- return ds;
- }
-
- public DataSet getBuyThingTableByDateGroup(DateTime fromDate)
- {
- DataSet ds = new DataSet();
- string cmdStr = "select buyUserName,sum(moneyTotal) from buyThingTable
- where buyDate >= #" + fromDate + "# group by buyUserName "
- OleDbConnection conn = new OleDbConnection(connstr);
- OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmdStr, conn);
- dataAdapter.Fill(ds, "buyThingTable");
- return ds;
- }
-
- public Int32 getbuyThingTableId(string buyUserName, string thingName, float moneyTotal, DateTime buyDate)
- {
- DataSet ds = new DataSet();
- string cmdStr = "select id from buyThingTable where buyUserName='" + buyUserName + "'and thingName='" + thingName + "' and moneyTotal=" + moneyTotal + " and buyDate = #" + buyDate + "#";
- OleDbConnection conn = new OleDbConnection(connstr);
- OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmdStr, conn);
- dataAdapter.Fill(ds, "theId");
- DataTable dt = ds.Tables["theId"];
-
- return Int32.Parse(dt.Rows[0].ItemArray[0].ToString());
-
- }
-
- public bool delRecordById(Int32 id)
- {
- string cmdstr = "delete from buyThingTable where [id] = " + id + "";
- using (OleDbConnection conn = new OleDbConnection(connstr))
- {
- OleDbCommand ocmd = new OleDbCommand(cmdstr, conn);
-
- conn.Open();
- ocmd.ExecuteNonQuery();
- conn.Close();
- return true;
- }
- }
- public void AddbuyThingTable(string buyUserName, string thingName, float moneyTotal, DateTime buyDate)
- {
- string cmdstr = "insert into buyThingTable(buyUserName, thingName, moneyTotal, buyDate) values('" + buyUserName + "','" + thingName + "'," + moneyTotal + ",#" + buyDate + "#)";
-
- using (OleDbConnection conn = new OleDbConnection(connstr))
- {
- OleDbCommand ocmd = new OleDbCommand(cmdstr, conn);
- conn.Open();
- ocmd.ExecuteNonQuery();
- conn.Close();
- }
- }
- }
- }