zoukankan      html  css  js  c++  java
  • 操作数据库的SqlHelper类

    public static class SqlHelper
    {
    public static readonly string connstr =
    ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;

    public static SqlConnection OpenConnection()
    {
    SqlConnection conn = new SqlConnection(connstr);
    conn.Open();
    return conn;
    }

    public static int ExecuteNonQuery(string cmdText,
    params SqlParameter[] parameters)
    {
    using (SqlConnection conn = new SqlConnection(connstr))
    {
    conn.Open();
    return ExecuteNonQuery(conn, cmdText, parameters);
    }
    }

    public static object ExecuteScalar(string cmdText,
    params SqlParameter[] parameters)
    {
    using (SqlConnection conn = new SqlConnection(connstr))
    {
    conn.Open();
    return ExecuteScalar(conn, cmdText, parameters);
    }
    }

    public static DataTable ExecuteDataTable(string cmdText,
    params SqlParameter[] parameters)
    {
    using (SqlConnection conn = new SqlConnection(connstr))
    {
    conn.Open();
    return ExecuteDataTable(conn, cmdText, parameters);
    }
    }

    public static int ExecuteNonQuery(SqlConnection conn,string cmdText,
    params SqlParameter[] parameters)
    {
    using (SqlCommand cmd = conn.CreateCommand())
    {
    cmd.CommandText = cmdText;
    cmd.Parameters.AddRange(parameters);
    return cmd.ExecuteNonQuery();
    }
    }

    public static object ExecuteScalar(SqlConnection conn, string cmdText,
    params SqlParameter[] parameters)
    {
    using (SqlCommand cmd = conn.CreateCommand())
    {
    cmd.CommandText = cmdText;
    cmd.Parameters.AddRange(parameters);
    return cmd.ExecuteScalar();
    }
    }

    public static DataTable ExecuteDataTable(SqlConnection conn, string cmdText,
    params SqlParameter[] parameters)
    {
    using (SqlCommand cmd = conn.CreateCommand())
    {
    cmd.CommandText = cmdText;
    cmd.Parameters.AddRange(parameters);
    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
    {
    DataTable dt = new DataTable();
    adapter.Fill(dt);
    return dt;
    }
    }
    }

    public static object ToDBValue(this object value)
    {
    return value == null ? DBNull.Value : value;
    }

    public static object FromDBValue(this object dbValue)
    {
    return dbValue == DBNull.Value ? null : dbValue;
    }
    }

  • 相关阅读:
    GROK解析正则表达式
    夜神模拟器和Hbuilder连接
    使用notepad++批量在每行首尾添加内容
    mybatis-plus
    laravel controller 层---数据库操作
    laravel controller 层---请求参数传递
    laravel controller 层---数据验证
    laravel model 层
    laravel 博客收藏,转载篇
    laravel 用法大全开始
  • 原文地址:https://www.cnblogs.com/CeleryCabbage/p/4255812.html
Copyright © 2011-2022 走看看