• [原]DbHelper-SQL数据库访问助手


    using System;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace Whir.Software.Framework.Ultimate
    {
        /// <summary>
        ///     数据库访问助手
        /// </summary>
        public class DbHelper
        {
            #region 判断连接是否成功
            /// <summary>
            ///     判断连接是否成功!
            /// </summary>
            /// <param name="con"> 链接字符串</param>
            /// <returns>true 表示链接成功,false表示连接失败</returns>
            public static bool IsConnected(string con)
            {
                bool flag;
                var conn = new SqlConnection(con);
                try
                {
                    conn.Open();
                    flag = true;
                }
                catch (Exception)
                {
                    flag = false;
                }
                finally
                {
                    conn.Close();
                }
                return flag;
            }
            #endregion
            #region 执行不带参数sql语句
            /// <summary>
            ///     执行不带参数sql语句
            /// </summary>
            /// <param name="sql">增,删,改sql语句</param>
            /// <param name="con"></param>
            /// <returns>返回所影响的行数</returns>
            public static bool Execute(string sql, string con)
            {
                var cmd = new SqlCommand();
                var connection = new SqlConnection(con);
                try
                {
                    using (connection)
                    {
                        cmd.Connection = connection;
                        cmd.CommandText = sql;
                        connection.Open();
                        cmd.ExecuteNonQuery();
                        return true;
                    }
                }
                catch (Exception)
                {
                    return false;
                }
            }
            #endregion
            #region 执行SQL语句返回DataTable
            /// <summary>
            ///     执行SQL语句返回DataTable
            /// </summary>
            /// <param name="sql"></param>
            /// <param name="con"></param>
            /// <returns></returns>
            public static DataTable ExcuteDataTable(string sql, string con)
            {
                var cmd = new SqlCommand();
                var connection = new SqlConnection(con);
                try
                {
                    using (connection)
                    {
                        cmd.Connection = connection;
                        cmd.CommandText = sql;
                        connection.Open();
                        var da = new SqlDataAdapter(cmd);
                        var ds = new DataSet();
                        da.Fill(ds);
                        return ds.Tables[0];
                    }
                }
                catch (Exception ex)
                {
                    var dt = new DataTable();
                    dt.Columns.Add("异常信息");
                    DataRow row = dt.NewRow();
                    row["异常信息"] = ex.Message;
                    dt.Rows.Add(row);
                    return dt;
                }
            }
            #endregion
            #region 执行SQL语句查询单条记录
            /// <summary>
            ///     执行SQL语句查询单条记录
            /// </summary>
            /// <param name="sql"></param>
            /// <param name="con"></param>
            /// <returns></returns>
            public static object ExecuteScalar(string sql, string con)
            {
                var cmd = new SqlCommand();
                var connection = new SqlConnection(con);
                try
                {
                    using (connection)
                    {
                        cmd.Connection = connection;
                        cmd.CommandText = sql;
                        connection.Open();
                        return cmd.ExecuteScalar();
                    }
                }
                catch (Exception)
                {
                    return string.Empty;
                }
            }
            #endregion
            #region 取得表最大Id
            /// <summary>
            ///     取得表最大Id
            /// </summary>
            /// <param name="tableName"></param>
            /// <param name="fieldName"></param>
            /// <param name="con"></param>
            /// <returns></returns>
            public static int GetMaxId(string tableName, string fieldName, string con)
            {
                string sql = "SELECT NVL(MAX({0}),0)+1 FROM {1}";
                try
                {
                    sql = string.Format(sql, fieldName, tableName);
                    int result;
                    Int32.TryParse(ExecuteScalar(sql, con).ToString(), out result);
                    return result;
                }
                catch (Exception)
                {
                    return -1;
                }
            }
            #endregion
        }
    }

    作者:a497785609 发表于2014-6-1 15:06:22 原文链接
    阅读:8 评论:0 查看评论
  • 相关阅读:
    Lombok 同时使用 @Data 和 @Builder 的巨坑,千万别乱用!
    我的开源代码被大公司盗用后:有人承认,有人让我滚!!
    Oracle Performance Active Session History ASH图
    生活中不需节食的简单减肥技巧 生活至上,美容至尚!
    maven模块化开发
    Python+GDAL实现矢量转栅格
    舟曲县行政区划编码
    arcgis pro Generate Transects Along Lines沿线生成样带 (数据管理)
    GeoServer安装GDAL扩展&Java如何连接GDAL
    gltf和3dtiles的关系
  • 原文地址:https://www.cnblogs.com/zhangqs008/p/3763825.html
Copyright © 2020-2023  润新知