zoukankan      html  css  js  c++  java
  • C# 增删改查应用(四)

          //读取配置文件,连接数据库语句
            public static string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["Family"].ConnectionString;
    
            private static OleDbConnection oleConn = new OleDbConnection(strCon);//access database
    
            /// <summary>
            /// 根据命令增加
            /// </summary>
            /// <param name="sql"></param>
            /// <returns></returns>
            public static int Insert(string sql)
            {
                int result = 0;
                try
                {
                    oleConn.Open();
                    OleDbCommand oleCommand = new OleDbCommand(sql, oleConn);
                    result = oleCommand.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally { oleConn.Close(); }
                return result;
            }
    
            /// <summary>
            /// 根据命令查询表
            /// </summary>
            /// <param name="sql">sql语句</param>
            /// <returns></returns>
            public static DataTable Select(string sql)
            {
                DataTable tb = new DataTable();
                try
                {
                    oleConn.Open();
                    OleDbDataAdapter adapter = new OleDbDataAdapter(sql, oleConn);
                    adapter.Fill(tb);
                }
                catch (Exception exception)
                {
                    string message = exception.Message;
                }
                finally { oleConn.Close(); }
                return tb;
            }
    
            /// <summary>
            /// 根据命令删除
            /// </summary>
            /// <param name="sql">sql语句</param>
            /// <returns></returns>
            public static int Delete(string sql)
            {
                int ifExecute = 0;
                try
                {
                    oleConn.Open();
                    OleDbCommand comm = new OleDbCommand(sql);
                    ifExecute = comm.ExecuteNonQuery();
                }
                finally
                {
                    oleConn.Close();
                }
                return ifExecute;
            }
            /// <summary>
            /// 更新数据
            /// </summary>
            /// <param name="sql">sql语句</param>
            /// <returns></returns>
            public static int Update(string sql)
            {
                int ifExecute = 0;
                try
                {
                    oleConn.Open();
                    OleDbCommand comm = new OleDbCommand(sql, oleConn);
                    ifExecute = comm.ExecuteNonQuery();
                }
                finally
                {
                    oleConn.Close();
                }
                return ifExecute;
            }
    
            /// <summary>
            /// 执行sql返回对象
            /// </summary>
            /// <param name="sql">sql语句</param>
            /// <returns></returns>
            public static object ExecuteSingle(string sql)
            {
                object obj = null;
                try
                {
                    oleConn.Open();
                    OleDbCommand comm = new OleDbCommand(sql, oleConn);
                    obj = comm.ExecuteScalar();
                }
                finally
                {
                    oleConn.Close();
                }
                return obj;
            }
  • 相关阅读:
    函数及习题
    数组和集合
    数组和集合实例
    普通集合和泛型集合的区别,哈希表和字典表的区别,队列和堆栈的区别以及堆和栈的区别。
    c#时间表示
    c#正则表达式
    js正则实例
    习题实例
    c#数据类型
    简单控件
  • 原文地址:https://www.cnblogs.com/jstblog/p/12418163.html
Copyright © 2011-2022 走看看