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);

}

}

}

}

相关推荐
咖猫13 分钟前
Google guava 最佳实践 学习指南之08 `BiMap`(双向映射)
java·开发语言·guava
大乔乔布斯17 分钟前
MySQL批量删除字段跑路?
数据库·sql·mysql
TDengine (老段)22 分钟前
TDengine 新功能 通过 UID 删除不可见表
大数据·数据库·物联网·时序数据库·tdengine
娶个名字趴22 分钟前
Redis(2)常用命令
java·数据库·redis·缓存
滿23 分钟前
处理错误的两种方式:try...catch 与 then...catch
java·开发语言
编码浪子27 分钟前
构建一个rust生产应用读书笔记7-确认邮件1
数据库·rust·php
Ttang2336 分钟前
Tomcat原理(4)——尝试手动Servlet的实现
java·开发语言·servlet·java-ee·tomcat·intellij-idea
buyue__1 小时前
MacOS安装MySQL
数据库·mysql
lzz的编码时刻1 小时前
Java 8 Optional 详细使用教程-优雅解决NPE
java
计算机毕设指导61 小时前
基于Springboot林业产品推荐系统【附源码】
java·开发语言·spring boot·后端·mysql·spring·intellij-idea