• C#工具:ASP.net 调用SQLserver帮助类


    一、正常调用

      1、创建DBHelper帮助类

      2、复制以下代码到类中

      
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    
    namespace 对应的命名空间
    {
        public class DBHelper
        {
            //连接数据库
            public static string connStr = "Data Source=.;Initial Catalog=数据库名称;Integrated Security=True";
            public static SqlConnection cnn = new SqlConnection(connStr);
    
            /// <summary>
            /// 执行多条SQL语句,实现数据库事务
            /// </summary>
            /// <param name="SQLStringList"></param>
            /// <returns></returns>
            public static int ExecuteSqlTran(List<string> SQLStringList)
            {
                using (SqlConnection conn = new SqlConnection(connStr))
                {
                    conn.Open();
                    SqlCommand command = new SqlCommand();
                    command.Connection = conn;
                    SqlTransaction tx = conn.BeginTransaction();
                    command.Transaction = tx;
                    try
                    {
                        int count = 0;
                        for (int n = 0; n < SQLStringList.Count; n++)
                        {
                            string strsql = SQLStringList[n];
                            if (strsql.Trim().Length > 1)
                            {
                                command.CommandText = strsql;
                                count += command.ExecuteNonQuery();
                            }
                        }
                        tx.Commit();
                        return count;
                    }
                    catch
                    {
                        tx.Rollback();
                        return 0;
                    }
                }
            }
    
            /// <summary>
            /// 执行增删改的操作
            /// </summary>
            /// <param name="sql">sql命令</param>
            /// <returns>受影响的行数</returns>
            public static int ExecuteNonQuery(string sql)
            {
                Open();
                SqlCommand command = new SqlCommand(sql, cnn);
                int result = command.ExecuteNonQuery();
                cnn.Close();
                return result;
            }
            /// <summary>
            /// 查询单个值
            /// </summary>
            /// <param name="sql"></param>
            /// <returns></returns>
            public static object ExecuteScalar(string sql)
            {
                Open();
                SqlCommand command = new SqlCommand(sql, cnn);
                object result = command.ExecuteScalar();
                cnn.Close();
                return result;
            }
            /// <summary>
            /// 返回数据表
            /// </summary>
            /// <param name="sql"></param>
            /// <returns></returns>
            public static DataTable GetDataTable(string sql)
            {
                SqlDataAdapter adapter = new SqlDataAdapter(sql, cnn);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                return ds.Tables[0];
            }
            /// <summary>
            /// 返回DataReader对象,使用结束后,勿忘关闭DataReader与数据库
            /// </summary>
            /// <param name="sql"></param>
            /// <returns></returns>
            public static SqlDataReader GetDataReader(string sql)
            {
                Open();
                SqlCommand command = new SqlCommand(sql, cnn);
                return command.ExecuteReader();
            }
            /// <summary>
            /// 打开数据库
            /// </summary>
            public static void Open() 
            {
                if (cnn.State==ConnectionState.Broken||cnn.State==ConnectionState.Open)
                {
                    cnn.Close();
                }
                cnn.Open();
            }
    
            /// <summary>
            /// 打开数据库
            /// </summary>
            public static void Close()
            {
                cnn.Close();
            }
        }
    }    
    View Code

      3、修改对应的命名空间和数据库名称

      4、成功调用

     二、存储过程调用

      1、创建DBHelperProc帮助类

      2、复制以下代码到类中

      
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    
    namespace 命名空间
    {
        public class DBHelper
        {
            public static string connStr = "Data Source=.;Initial Catalog=数据库名称;Integrated Security=True";
            public static SqlConnection cnn = new SqlConnection(connStr);
    
            /// <summary>
            /// 执行增删改的操作
            /// </summary>
            /// <param name="sql">sql命令</param>
            /// <returns>受影响的行数</returns>
            public static int ExecuteNonQuery(string sql)
            {
                Open();
                SqlCommand command = new SqlCommand(sql, cnn);
                int result = command.ExecuteNonQuery();
                cnn.Close();
                return result;
            }
            /// <summary>
            /// 查询单个值
            /// </summary>
            /// <param name="sql"></param>
            /// <returns></returns>
            public static object ExecuteScalar(string sql)
            {
                Open();
                SqlCommand command = new SqlCommand(sql, cnn);
                object result = command.ExecuteScalar();
                cnn.Close();
                return result;
            }
            /// <summary>
            /// 返回数据表
            /// </summary>
            /// <param name="sql"></param>
            /// <returns></returns>
            public static DataTable GetDataTable(string sql)
            {
                SqlDataAdapter adapter = new SqlDataAdapter(sql, cnn);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                return ds.Tables[0];
            }
            /// <summary>
            /// 返回DataReader对象,使用结束后,勿忘关闭DataReader与数据库
            /// </summary>
            /// <param name="sql"></param>
            /// <returns></returns>
            public static SqlDataReader GetDataReader(string sql)
            {
                Open();
                SqlCommand command = new SqlCommand(sql, cnn);
                return command.ExecuteReader();
            }
            /// <summary>
            /// 打开数据库
            /// </summary>
            public static void Open()
            {
                if (cnn.State == ConnectionState.Broken || cnn.State == ConnectionState.Open)
                {
                    cnn.Close();
                }
                cnn.Open();
            }
    
            /// <summary>
            /// 打开数据库
            /// </summary>
            public static void Close()
            {
                cnn.Close();
            }
    
            /// <summary>
            /// 使用存储过程查询数据结果
            /// </summary>
            /// <param name="sql"></param>
            /// <param name="paras"></param>
            /// <returns></returns>
            public static DataTable GetDataTable(string procName, SqlParameter[] paras = null)
            {
                Open();
                SqlCommand command = new SqlCommand(procName, cnn);
                command.CommandType = CommandType.StoredProcedure;
    
                if (paras != null)
                {
                    command.Parameters.AddRange(paras);
                }
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                Close();
                return ds.Tables[0];
            }
    
            /// <summary>
            /// 使用存储过程执行增删改
            /// </summary>
            /// <param name="sql"></param>
            /// <param name="paras"></param>
            /// <returns></returns>
            public static int ExecuteNonQuery(string procName, SqlParameter[] paras)
            {
                Open();
                SqlCommand command = new SqlCommand(procName, cnn);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddRange(paras);
    
                int result = command.ExecuteNonQuery();
                Close();
    
                return result;
            }
        }
    }
    View Code

      3、修改对应的命名空间和数据库名称

      4、成功调用

  • 相关阅读:
    9.堆排序
    8.全排列
    37.微信跳一跳辅助开发(C语言+EasyX)
    7.图形化实现快速排序法
    codeforces 632A A. Grandma Laura and Apples(暴力)
    codeforces 633D D. Fibonacci-ish(dfs+暴力+map)
    codeforces 633B B. A Trivial Problem(数论)
    codeforces 633A A. Ebony and Ivory(暴力)
    codeforces 622B B. The Time
    codeforces 622D D. Optimal Number Permutation(找规律)
  • 原文地址:https://www.cnblogs.com/yu-shang/p/10644409.html
Copyright © 2020-2023  润新知