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

}

相关推荐
没有了遇见10 小时前
Android 独立开发痛点之静态Html Github放置
android
yumgpkpm10 小时前
hadoop集群搭建 (超详细) 接入Impala、Hive,AI 大模型的数据底座
hive·hadoop·mysql·zookeeper·flink·kafka·hbase
Kapaseker10 小时前
四大组件齐上阵,轻松拿捏实习生
android·kotlin
00后程序员张10 小时前
IPA 混淆技术全解,从成品包结构出发的 iOS 应用安全实践与工具组合
android·安全·ios·小程序·uni-app·cocoa·iphone
Just_Paranoid11 小时前
【Android UI】Android Drawable XML 标签解析
android·ui·vector·drawable·shape·selector
都是蠢货11 小时前
mysql中null是什么意思?
android·数据库·mysql
Just_Paranoid11 小时前
【Android UI】Android 创建渐变背景 Drawable
android·ui·drawable·shape·gradient
爱技术的阿呆11 小时前
MySQL子查询及其案例
数据库·mysql
千里马学框架11 小时前
AI豆包手机权限文章补充:Mainfest中某个权限的protectionLevel具体是如何被系统定义的?
android·智能手机·framework·权限·protectionlevel
鹏多多11 小时前
flutter使用package_info_plus库获取应用信息的教程
android·前端·flutter