• SQL Procedure Operations


    This Class is used to handle SQL procedures, including Verify SP exist or not, Create SP into DB and Execute SP via C#.

    using System;
    using System.Data.SqlClient;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    namespace SqlBIS.Auto.Framework
    {
        public class SqlHelper
        {
            public string ConnString { get; set; }
            public SqlConnection Connection { get; set; }
    
            public SqlHelper(string connString)
            {
                this.ConnString = connString;
                Connection = new SqlConnection(connString);
            }
    
            /// <summary>
            /// Verify if DB contain procedure or not
            /// </summary>
            /// <param name="proName">procedure name</param>
            /// <returns></returns>
            public  bool ProcedureIsExist(string proName)
            {
                bool result = false;
                String sqlComm = String.Format("IF OBJECT_ID('{0}') IS NOT NULL SELECT 'true' ELSE SELECT 'false'", proName);
                
                SqlCommand cmd = new SqlCommand(sqlComm, this.Connection);
                cmd.CommandType = CommandType.Text;
                this.Connection.Open();
                result = Convert.ToBoolean((cmd.ExecuteScalar()).ToString());
                this.Connection.Close();
    
                return result;
            }
    
            /// <summary>
            /// Create a new procedure
            /// </summary>
            /// <param name="proName"></param>
            /// <param name="proCommand"></param>
            public  void CreateProcedure(string proName, string proCommand)
            {
                SqlCommand cmd = new SqlCommand(proCommand, this.Connection);
                Connection.Open();
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                Connection.Close();
            }
    
            /// <summary>
            /// Execute procedure and return DataTable
            /// </summary>
            /// <param name="proName"></param>
            /// <param name="parameters"></param>
            /// <returns></returns>
            public DataTable ExecuteProcedure(string proName, SqlParameter[] parameters)
            {
                DataTable dt = new DataTable();
    
                SqlCommand cmd = new SqlCommand(proName, this.Connection);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                foreach (var item in parameters)
                    cmd.Parameters.Add(item);
                SqlDataAdapter sa = new SqlDataAdapter(cmd);
                sa.Fill(dt);
    
                return dt;
            }
        }
    }
  • 相关阅读:
    《博客园美化》添加雪花/修改icon
    js获取开始年与结束年之间的年份
    《博客园美化》为您的博客增加一个萌萌的看板娘吧
    JS对比时间大小
    同域名下两个子级域名共享cookie
    input输入框禁止显示历史记录
    C# 操作符 << 与 >>
    如何在IIS上发布网站
    Sql 插入操作时返回当前新增的Id
    JS Cookie操作
  • 原文地址:https://www.cnblogs.com/Blackeye286/p/4710150.html
Copyright © 2020-2023  润新知