.net链接mysql简单而高效

using System;

using System.Collections.Generic;

using System.Data.SqlClient;

using System.Data;

using System.Linq;

using System.Web;

using MySql.Data;

using MySql.Data.MySqlClient;

using System.Configuration;

namespace WebApplication2.Model

{

public class MysqlHELPER

{

//public static string Conntr ="Server=118.195.237.101;port=3306;database=esab;user=root;password=flx123456;charset=utf8;";

private static string Conntr=ConfigurationManager.ConnectionStrings["mysql"].ConnectionString;

public static DataTable RunSQLReturnDT(string SQLString)

{

object lockThis = new object();

lock (lockThis)

{

using (MySqlConnection connection = new MySqlConnection(Conntr))

{

DataSet ds = new DataSet();

connection.Open(); MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection); command.Fill(ds, "ds"); connection.Close(); return ds.Tables[0]; } } } /// <summary> /// 执行SQL,返回是否执行成功 /// </summary> /// <param name="sql">sql语句,如:delete,update,insert等</param> /// <returns>True:成功,False失败</returns> public static bool RunSQL(string SQLString) { object lockThis = new object(); lock (lockThis) { using (MySqlConnection connection = new MySqlConnection(Conntr)) { using (MySqlCommand cmd = new MySqlCommand(SQLString, connection)) { try { connection.Open(); int rows = cmd.ExecuteNonQuery(); connection.Close(); if (rows > 0) return true; else return false; } catch (System.Data.SqlClient.SqlException e) { connection.Close(); throw e; } } } } } }

}

应用:

public DataTable PgfData(string partname,string serialnumber )

{

string sql = "";

DataTable dt= MysqlHELPER.RunSQLReturnDT(sql);

return dt;

}

相关推荐
poemyang9 分钟前
像Git一样管理数据:深入解析数据库并发控制MVCC的实现
mysql·事务·mvcc
Nerve4 小时前
FluxImageLoader : 基于Coil3封装的 Android 图片加载库,旨在提供简单、高效且功能丰富的图片加载解决方案
android·android jetpack
元气满满-樱4 小时前
MySQL基础管理
android·mysql·adb
summerkissyou19874 小时前
android13-audio-AudioTrack-写数据流程
android·音视频
wuxuanok5 小时前
ThinkPHP ——安装部署与配置
sql·mysql·nginx·php
ttthe_MOon6 小时前
MySQL 高可用解决方案 MHA:原理、配置与实践
数据库·mysql
翔云1234566 小时前
在MySQL中,gtid_purged 的初始化和更新机制
数据库·mysql
董三毛6 小时前
Kotlin Coroutine 底层实现原理
android
L108706 小时前
AutoJsPro GoogleMaterial3 M3组件使用示例
android
smileNicky6 小时前
大型MySQL查询优化实战:从全表扫描到毫秒级响应的通用索引设计
数据库·mysql