zoukankan      html  css  js  c++  java
  • SqlHelper类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Data;
    
    namespace HuaGongWeb
    {
        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;
            }
        }
    }
  • 相关阅读:
    Lighthead SiteCrawler
    svnbook
    SFTP in Python: Really Simple SSH
    WP Robot Wordpress plugin logo
    use curl to post data for work punch
    Setting up Hudson on port 80 on a Debian or Ubuntu machine
    Problem with PEXPECT in Python
    H2 Database Engine — h2database.com — Readability
    execnet: rapid multiPython deployment
    Maven问题总结 3 技术改变生活商业成就梦想 51CTO技术博客
  • 原文地址:https://www.cnblogs.com/panmy/p/5806068.html
Copyright © 2011-2022 走看看