• 常用ADO.NET操作ACCESS数据库


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;//
    using System.Data.OleDb;//
    using System.Drawing;//
    
    namespace EColor
    {
        class ControlDBS
        {
            //定义链接字符串
            private OleDbConnection con;
    
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="path">数据库路径</param>
            public ControlDBS(string path)
            {
                con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path);
            }
    
            /// <summary>
            /// 获取sql查询数据到数据表
            /// </summary>
            /// <param name="SQL">SQL语句</param>
            /// <returns>数据表</returns>
            public DataTable GetDataTable(string SQL)
            {
                OleDbDataAdapter adp = new OleDbDataAdapter(SQL, con);
                try
                {
                    con.Open();
                    DataTable dt = new DataTable();
                    adp.Fill(dt);
                    return dt;
                }
                catch
                {
                    return null;
                }
                finally
                {
                    con.Close();
                }
            }
    
            /// <summary>
            /// 获取数据到数据集
            /// </summary>
            /// <param name="SQL">SQL语句</param>
            /// <returns>数据集</returns>
            public DataSet GetDataSet(string SQL)
            {
                OleDbDataAdapter adp = new OleDbDataAdapter(SQL, con);
                try
                {
                    con.Open();
                    DataSet ds = new DataSet();
                    adp.Fill(ds);
                    return ds;
                }
                catch
                {
                    return null;
                }
                finally
                {
                    con.Close();
                }
            }
    
            /// <summary>
            /// 获取数据到DataReader
            /// </summary>
            /// <param name="SQL">SQL语句</param>
            /// <returns>DataReader</returns>
            public OleDbDataReader GetDataReader(string SQL)
            {
                OleDbCommand cmd = new OleDbCommand(SQL, con);
                try
                {
                    con.Open();
                    OleDbDataReader dr = cmd.ExecuteReader();
                    return dr;
                }
                catch
                {
                    return null;
                }
            }
    
            /// <summary>
            /// 返回sql查询的第一行第一列
            /// </summary>
            /// <param name="SQL">SQL语句</param>
            /// <returns>第一行第一列的字符串</returns>
            public string GetFirstValue(string SQL)
            {
                OleDbCommand cmd = new OleDbCommand(SQL, con);
                try
                {
                    con.Open();
                    string strchar = cmd.ExecuteScalar().ToString();
                    return strchar;
                }
                catch
                {
                    return null;
                }
                finally
                {
                    con.Close();
                }
            }
    
            /// <summary>
            /// 执行SQL语句
            /// </summary>
            /// <param name="SQL">SQL语句</param>
            public void RunSqlCmd(string SQL)
            {
                OleDbCommand cmd = new OleDbCommand(SQL, con);
                try
                {
                    con.Open();
                    cmd.ExecuteNonQuery();
                }
                catch
                { }
                finally
                {
                    con.Close();
                }
            }
        }
    }
  • 相关阅读:
    SQL Server 2008中如何为XML字段建立索引
    比如取得一个div得innerHTML
    C#生成CHM文件(入门篇)
    jquery outerhtml
    WCF 中状态的保存
    MVC进阶学习HtmlHelper控件解析(一)
    MVC进阶学习HtmlHelper控件解析(四)
    MVC进阶学习HtmlHelper之GridView控件拓展(一)
    MVC进阶学习HtmlHelper控件解析(三)
    MVC进阶学习表单提交总结
  • 原文地址:https://www.cnblogs.com/mane/p/4583637.html
Copyright © 2020-2023  润新知