• 常用的C# 操作ACCESS数据库类(winform)


    using System;
    using System.Configuration;
    using System.Collections;
    using System.Data;
    using System.Data.OleDb;
    namespace MyData
    {
    public static class Class_oledb_conn
    {
    //public static string ConnStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DataBase/*.mdb;";
    public static string ConnStr = MyData.Properties.Settings.Default.my_soft_oledbConn;
    //打开数据库链接
    public static OleDbConnection Open_Conn(string ConnStr)
    {
    OleDbConnection Conn = new OleDbConnection(ConnStr);
    Conn.Open();
    return Conn;
    }
    //关闭数据库链接
    public static void Close_Conn(OleDbConnection Conn)
    {
    if (Conn != null)
    {
    Conn.Close();
    Conn.Dispose();
    }
    GC.Collect();
    }
    //运行OleDb语句
    public static int Run_SQL(string SQL, string ConnStr)
    {
    OleDbConnection Conn = Open_Conn(ConnStr);
    OleDbCommand Cmd = Create_Cmd(SQL, Conn);
    try
    {
    int result_count = Cmd.ExecuteNonQuery();
    Close_Conn(Conn);
    return result_count;
    }
    catch
    {
    Close_Conn(Conn);
    return 0;
    }
    }
    // 生成Command对象
    public static OleDbCommand Create_Cmd(string SQL, OleDbConnection Conn)
    {
    OleDbCommand Cmd = new OleDbCommand(SQL, Conn);
    return Cmd;
    }
    // 运行OleDb语句返回 DataTable
    public static DataTable Get_DataTable(string SQL, string ConnStr, string Table_name)
    {
    OleDbDataAdapter Da = Get_Adapter(SQL, ConnStr);
    DataTable dt = new DataTable(Table_name);
    Da.Fill(dt);
    return dt;
    }
    // 运行OleDb语句返回 OleDbDataReader对象
    public static OleDbDataReader Get_Reader(string SQL, string ConnStr)
    {
    OleDbConnection Conn = Open_Conn(ConnStr);
    OleDbCommand Cmd = Create_Cmd(SQL, Conn);
    OleDbDataReader Dr;
    try
    {
    Dr = Cmd.ExecuteReader(CommandBehavior.Default);
    }
    catch
    {
    throw new Exception(SQL);
    }
    Close_Conn(Conn);
    return Dr;
    }
    // 运行OleDb语句返回 OleDbDataAdapter对象
    public static OleDbDataAdapter Get_Adapter(string SQL, string ConnStr)
    {
    OleDbConnection Conn = Open_Conn(ConnStr);
    OleDbDataAdapter Da = new OleDbDataAdapter(SQL, Conn);
    return Da;
    }
    // 运行OleDb语句,返回DataSet对象
    public static DataSet Get_DataSet(string SQL, string ConnStr, DataSet Ds)
    {
    OleDbDataAdapter Da = Get_Adapter(SQL, ConnStr);
    try
    {
    Da.Fill(Ds);
    }
    catch (Exception Err)
    {
    throw Err;
    }
    return Ds;
    }
    // 运行OleDb语句,返回DataSet对象
    public static DataSet Get_DataSet(string SQL, string ConnStr, DataSet Ds, string tablename)
    {
    OleDbDataAdapter Da = Get_Adapter(SQL, ConnStr);
    try
    {
    Da.Fill(Ds, tablename);
    }
    catch (Exception Ex)
    {
    throw Ex;
    }
    return Ds;
    }
    // 运行OleDb语句,返回DataSet对象,将数据进行了分页
    public static DataSet Get_DataSet(string SQL, string ConnStr, DataSet Ds, int StartIndex, int PageSize, string tablename)
    {
    OleDbConnection Conn = Open_Conn(ConnStr);
    OleDbDataAdapter Da = Get_Adapter(SQL, ConnStr);
    try
    {
    Da.Fill(Ds, StartIndex, PageSize, tablename);
    }
    catch (Exception Ex)
    {
    throw Ex;
    }
    Close_Conn(Conn);
    return Ds;
    }
    // 返回OleDb语句执行结果的第一行第一列
    public static string Get_Row1_Col1_Value(string SQL, string ConnStr)
    {
    OleDbConnection Conn = Open_Conn(ConnStr);
    string result;
    OleDbDataReader Dr;
    try
    {
    Dr = Create_Cmd(SQL, Conn).ExecuteReader();
    if (Dr.Read())
    {
    result = Dr[0].ToString();
    Dr.Close();
    }
    else
    {
    result = "";
    Dr.Close();
    }
    }
    catch
    {
    throw new Exception(SQL);
    }
    Close_Conn(Conn);
    return result;
    }
    }
    }
    ------------------------------------------------------------------
    app.config 里的数据库链接字符串
    ------------------------------------------------------------------
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <configSections>
    </configSections>
    <connectionStrings>
    <add name="MyData.Properties.Settings.my_soft_oledbConn" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=数据库路径\*.mdb"
    providerName="System.Data.OleDb" />
    </connectionStrings>
    </configuration>

  • 相关阅读:
    POJ 2752 Seek the Name, Seek the Fame
    POJ 2406 Power Strings
    KMP 算法总结
    SGU 275 To xor or not to xor
    hihocoder 1196 高斯消元.二
    hihoCoder 1195 高斯消元.一
    UvaLive 5026 Building Roads
    HDU 2196 computer
    Notions of Flow Networks and Flows
    C/C++代码中的笔误
  • 原文地址:https://www.cnblogs.com/hfzsjz/p/1780817.html
Copyright © 2020-2023  润新知