• 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;
            }
  • 相关阅读:
    代码重构~提取方法
    代码重构~提取到类
    不说技术~有时,开发者还是应该讲究一点!
    代码重构~封装成员变量
    将不确定变为确定~LINQ查询包含对不同数据上下文上所定义项的引用
    真实的用户,真实的中国互联网
    John Resig: JavaScript's Chuck Norris
    chrome插件IE tab使用技巧
    人生的疆域与生存的幻象—阅读丰富人生
    WPF 创建多行TextBox
  • 原文地址:https://www.cnblogs.com/jstblog/p/12418163.html
Copyright © 2020-2023  润新知