当前位置: 首页 > article >正文

c# sqlhelper类

主要包含了事务进来,是个亮点:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Net.Mail;
using System.Net;


namespace MIAS_DigitalTool_Platform.SqlhelperClass
{
    public static class sqlHelper
    {        
        public static readonly string connectionString = StaticClass.global.connectionCurrent;
        // 数据库连接字符串,根据实际情况进行修改
        //public static readonly string connectionString = "Data Source=192.168.70.8;Initial Catalog=数据库名;Integrated Security=True";

        // 执行查询并返回DataTable
        public static DataTable ExecuteDataTable(string sql, CommandType commandType, params SqlParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(sql, connection);
                command.CommandType = commandType;
                if (parameters != null)
                {
                    foreach (SqlParameter parameter in parameters)
                    {
                        command.Parameters.Add(parameter);
                    }
                }

                try
                {
                    connection.Open();
                    SqlDataAdapter adapter = new SqlDataAdapter(command);
                    DataTable dataTable = new DataTable();
                    adapter.Fill(dataTable);
                    return dataTable;
                }
                catch (Exception ex)
                {
                    Serilog.Log.Information(ex.Message);
                    throw new Exception("ExecuteDataTable Error: " + ex.Message);
                }
            }
        }

        // 执行非查询操作,返回受影响的行数
        public static int ExecuteNonQuery(string sql, CommandType commandType, params SqlParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(sql, connection);
                command.CommandType = commandType;
                if (parameters != null)
                {
                    foreach (SqlParameter parameter in parameters)
                    {
                        command.Parameters.Add(parameter);
                    }
                }

                try
                {
                    connection.Open();
                    return command.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    Serilog.Log.Information(ex.Message);
                    throw new Exception("ExecuteNonQuery Error: " + ex.Message);

                }
            }
        }

        // 执行查询并返回单个值
        public static object ExecuteScalar(string sql, CommandType commandType, params SqlParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(sql, connection);
                command.CommandType = commandType;
                if (parameters != null)
                {
                    foreach (SqlParameter parameter in parameters)
                    {
                        command.Parameters.Add(parameter);
                    }
                }

                try
                {
                    connection.Open();
                    return command.ExecuteScalar();
                }
                catch (Exception ex)
                {
                    Serilog.Log.Information(ex.Message);
                    throw new Exception("ExecuteScalar Error: " + ex.Message);
                }
            }
        }

        // 执行事务性操作的方法,支持参数化查询
        public static void ExecuteTransaction(string[] sqlQueries, SqlParameter[][] parameters, CommandType commandType)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        foreach (string sqlQuery in sqlQueries)
                        {
                            // 为每个查询创建SqlCommand对象,并添加参数
                            SqlCommand command = new SqlCommand(sqlQuery, connection, transaction);
                            command.CommandType = commandType; // 可以是CommandType.Text或CommandType.StoredProcedure

                            // 如果有参数,添加到SqlCommand对象
                            if (parameters != null && parameters.Length > 0)
                            {
                                foreach (SqlParameter parameter in parameters[0]) // 假设每个查询对应一组参数
                                {
                                    command.Parameters.Add(parameter);
                                }
                            }

                            // 执行命令
                            command.ExecuteNonQuery();
                        }
                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        Serilog.Log.Information(ex.Message);
                        transaction.Rollback();
                        throw new Exception("Transaction Error: " + ex.Message);
                    }
                }
            }
        }

        public static void SMTP(string from,string fromAlias, string[] to, string subject, string body, string ipaddr, int port, string creditAccount, string password)
        {
            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                // 创建电子邮件消息对象
                MailMessage message = new MailMessage
                {
                    From = new MailAddress(from, fromAlias)
                };
                foreach (string recipient in to)
                {
                    message.To.Add(new MailAddress(recipient));
                }

                // 设置邮件主题和正文
                message.Subject = subject;
                message.Body = body;
                message.BodyEncoding = Encoding.UTF8;

                // 创建SMTP客户端
                SmtpClient client = new SmtpClient(ipaddr, port); // SMTP服务器地址和端口
                client.EnableSsl = false; // 如果服务器支持SSL

                如果SMTP服务器需要身份验证
                client.Credentials = new NetworkCredential(creditAccount, password);


                // 发送邮件
                client.Send(message);
                Serilog.Log.Warning("邮件发送成功!");
            }
            catch (Exception ex)
            {
                Serilog.Log.Error(ex.Message);
            }
        }
    }
}
 


http://www.kler.cn/news/305546.html

相关文章:

  • lvgl | guider应用笔记
  • java项目之网上商城系统设计与实现(源码+文档)
  • Tomcat_WebApp
  • 020、二级Java选择题综合知识点(持续更新版)
  • 树莓派Pico2(RP2350)开发环境搭建
  • Linux内核初始化过程中加载TCP/IP协议栈
  • ios xib 子控件约束置灰不能添加约束
  • 【modou网络库】Reactor架构与TCP通信机制分析
  • 基于hispark_taurus开发板示例学习OpenHarmony编译(1)
  • 记录工作中遇到的问题(持续更新~)
  • TikTok云手机解决运营效率低、封号问题
  • QT消息对话框学习
  • 用户登陆网址都发生了什么?
  • 网络原理1-传输层
  • [mysql]mysql的运算符
  • it基础软件运维管理:从操作系统到数据库,再到中间件和应用系统
  • 测试ASP.NET Core的WebApi项目调用WebService
  • 血缘解析<二>:如何解析带CTE语句的Sql
  • [机器学习]决策树
  • Parallels Desktop 20 for Mac中文版发布了?会哪些新功能
  • 学习笔记-Golang中的Context
  • 基础算法(3)——二分
  • Java邮件:如何配置以实现自动化邮件通知?
  • 平安养老险阜阳中心支公司开展金融教育宣传专项活动
  • ElementUI 快速入门:使用 Vue 脚手架搭建项目
  • SQL 代表什么?SQL 的全称是什么?
  • 二叉树算法 JAVA
  • 微信小程序中的模块化、组件化开发:完整指南
  • 资源管理新视角:利用 FastAPI Lifespan 事件优化你的应用
  • Android Greendao的数据库复制到设备指定位置