• c#sqlhelper


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data.SqlClient;
    using System.Data;
    using System.Configuration;
    
    namespace StudentMgmt.DBUtility
    {
        public abstract class SqlHelper
        {
            public static readonly string ConnectionStringProfile = ConfigurationManager.ConnectionStrings["SQLProfileConnString"].ConnectionString;
    
            public static void RunSQL(string strsql, params SqlParameter[] commandParameters)
            {
                SqlCommand cmd = new SqlCommand();
    
                using (SqlConnection conn = new SqlConnection(ConnectionStringProfile))
                {
                    PrepareCommand(cmd, conn, strsql, commandParameters);
                    cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                }
            }
    
            public static int RunSQLReturnValue(string strsql, params SqlParameter[] commandParameters)
            {
                SqlCommand cmd = new SqlCommand();
    
                using (SqlConnection connection = new SqlConnection(ConnectionStringProfile))
                {
                    PrepareCommand(cmd, connection, strsql, commandParameters);
                    int val = (int)cmd.ExecuteScalar();
                    cmd.Parameters.Clear();
                    return val;
                }
            }
    
            public static string RunSQLReturnString(string strsql, params SqlParameter[] commandParameters)
            {
                SqlCommand cmd = new SqlCommand();
    
                using (SqlConnection connection = new SqlConnection(ConnectionStringProfile))
                {
                    PrepareCommand(cmd, connection, strsql, commandParameters);
                    string val = (string)cmd.ExecuteScalar();
                    cmd.Parameters.Clear();
                    return val;
                }
            }
    
    
            public static int RunSQLReturnDataTable(string strsql, out DataTable objTable, params SqlParameter[] commandParameters)
            {
                objTable = new DataTable();
                SqlCommand cmd = new SqlCommand();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                int val = 0;
    
                using (SqlConnection conn = new SqlConnection(ConnectionStringProfile))
                {
                    PrepareCommand(cmd, conn, strsql, commandParameters);
                    try
                    {
                        da.Fill(objTable);
                        cmd.Parameters.Clear();
                        val = 1;
                    }
                    catch (Exception)
                    {
                        cmd.Parameters.Clear();
                        val = 0;
                    }
                    return val;
                }
            }
    
    
            private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, string cmdText, SqlParameter[] cmdParms)
            {
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                }
    
                cmd.Connection = conn;
                cmd.CommandText = cmdText;
                cmd.CommandType = CommandType.Text;
    
                if (cmdParms != null)
                {
                    foreach (SqlParameter parm in cmdParms)
                    {
                        cmd.Parameters.Add(parm);
                    }
                }
            }
        }
    }

    使用示例:

    objTable = new DataTable();
                StringBuilder builder = new StringBuilder();
                builder.Append("select S.StuNo as '学号', S.StuName as '学生姓名', case when S.Sex ='M' then '男' else '女' end '性别', S.Birthday as '出生日期', S.Native_Place as '籍贯', C.Class_Name as '所在班级', D.Department_Name as '所在院系' ");
                builder.Append("from Student as S inner join Classes as C on S.ClassId = C.Class_Id ");
                builder.Append("inner join Department as D on C.Department_Id = D.Department_Id ");
                builder.Append("where 1=1 ");
                if (stuNo.Length > 0)
                {
                    builder.Append("and S.StuNo = '" + stuNo + "' ");
                }
                if (class_id.Length > 0)
                {
                    builder.Append("and C.Class_Id = '" + class_id + "' ");
                }
                if (department_id.Length > 0)
                {
                    builder.Append("and C.Department_Id = '" + department_id + "' ");
                }
                if (stuName.Length > 0)
                {
                    builder.Append("and S.StuName like '%" + stuName + "%'");
                }
    
                int val = SqlHelper.RunSQLReturnDataTable(builder.ToString(), out objTable);
                return val;
  • 相关阅读:
    linux cfs调度器
    运算符重载
    linux cfs调度器_模型实现
    linux cfs调度器_理论模型
    jiffies存放
    在C++中实现不可继承的类
    装饰模式(Decorator Pattern)--------结构型模式
    How to use base class's assignment operator in C++
    组合模式(Composite Pattern) ------------结构型模式
    what's the help of "unnecessary" pointer comparison
  • 原文地址:https://www.cnblogs.com/QQQmadNULL/p/6918580.html
Copyright © 2020-2023  润新知