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;
            }
  • 相关阅读:
    应对高并发场景的redis加锁技巧
    Spring中@Transactional事务回滚(含实例具体解说,附源代码)
    计算机网络10--计算机网络体系结构简单介绍
    IIS身份验证的配置
    AMR音频文件格式分析
    IOS版本号被拒的经历
    两分钟读懂《成大事者不纠结》——读书笔记
    同一个TextView设置不同的颜色和大小
    似非而是的程序猿悖论---为什么救火比防火更加吃香?
    OS
  • 原文地址:https://www.cnblogs.com/jstblog/p/12418163.html
Copyright © 2011-2022 走看看