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;
            }
  • 相关阅读:
    centos 配置puTTY rsa自动登录
    Linux LVM 简单操作
    linux 系统下有sda和hda的硬件设备分别代表什么意思
    Centos 安装Sublime text 3
    编译安装MySQL-5.7.13
    药品查询网的数据库
    获得Android设备的唯一序列号
    Android中设置TextView的颜色setTextColor
    介绍几款网页数据抓取软件 分类: 业余 2015-08-07 18:09 5人阅读 评论(0) 收藏
    网上处方药物手册Rxlist 及其药学信息资源 分类: 业余 2015-08-07 14:16 8人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/jstblog/p/12418163.html
Copyright © 2011-2022 走看看