一、MySql
static string strConn = "server=.;database=mast;uid=sa;pwd=123456;integrated Security=SSPI;persist Security info=false;";
/// <summary> /// 执行SQL语句,返回影响的记录数 /// </summary> /// <param name="SQLString">SQL语句</param> /// <returns>影响的记录数</returns>
public static int MyExecuteSql(string SQLString) { using (MySqlConnection connection = new MySqlConnection(strConn)) { using (MySqlCommand cmd = new MySqlCommand(SQLString, connection)) { try { connection.Open(); int rows = cmd.ExecuteNonQuery(); return rows; } catch (System.Data.SqlClient.SqlException e) { connection.Close(); throw e; } } } }
/// <summary> /// 执行查询语句,返回DataTable /// </summary> /// <param name="SQLString">查询语句</param> /// <returns>DataSet</returns> public static DataTable MyQuery(string SQLString) { using (MySqlConnection connection = new MySqlConnection(strConn)) { DataSet ds = new DataSet(); try { connection.Open(); MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection); command.Fill(ds, "ds"); } catch (System.Data.SqlClient.SqlException ex) { throw new Exception(ex.Message); } return ds.Tables[0]; } }
二、SQLServer
static string strConn = "Data Source=.;database=mast;User id=root;Password=123456;pooling=false;CharSet=utf8;port=3306;"; /// <summary> /// 执行SQL语句,返回影响的记录数 /// </summary> /// <param name="SQLString">SQL语句</param> /// <returns>影响的记录数</returns> public static int ExecuteSql(string SQLString) { using (SqlConnection connection = new SqlConnection(strConn)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { connection.Open(); int rows = cmd.ExecuteNonQuery(); return rows; } catch (System.Data.SqlClient.SqlException e) { connection.Close(); throw e; } } } }
/// <summary> /// 执行查询语句,返回DataTable /// </summary> /// <param name="SQLString">查询语句</param> /// <returns>DataSet</returns> public static DataTable Query(string SQLString) { using (SqlConnection connection = new SqlConnection(strConn)) { DataSet ds = new DataSet(); try { connection.Open(); SqlDataAdapter command = new SqlDataAdapter(SQLString, connection); command.Fill(ds, "ds"); } catch (System.Data.SqlClient.SqlException ex) { throw new Exception(ex.Message); } return ds.Tables[0]; } }
三、Access
//using System.Data.OleDb;
public static string OtherPAth = "";
public static string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
public static string Sql = String.Empty;
#region 执行简单SQL语句,使用OleDb查询
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString)
{
using (OleDbConnection connection = new OleDbConnection(strConn + OtherPAth ))
{
using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
throw e;
}
}
}
}
/// <summary>
/// 执行查询语句,返回DataTable
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataTable Query(string SQLString)
{
using (OleDbConnection connection = new OleDbConnection(strConn + OtherPAth))
{
DataSet ds = new DataSet();
try
{
connection.Open();
OleDbDataAdapter command = new OleDbDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
return ds.Tables[0];
}
}
/// <summary>
/// 执行查询语句,返回DataTable
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataTable QueryDataName()
{
using (OleDbConnection connection = new OleDbConnection(strConn + OtherPAth))
{
DataTable ds = new DataTable();
try
{
connection.Open();
ds = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
return ds;
}
}
#endregion
四、Sqlite
public static string strConn = @"Data Source=" + @"F:资料文档20190629CAD 625AnPinCadDLLclDataCad_try0626.db";
public static string Sql = String.Empty;
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString)
{
using (SQLiteConnection connection = new SQLiteConnection(strConn))
{
//事务
using (SQLiteTransaction singleTrans = connection.BeginTransaction(IsolationLevel.ReadCommitted))
{
using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
{
try
{
connection.Open();
cmd.Transaction = singleTrans;
int rows = cmd.ExecuteNonQuery();
singleTrans.Commit();
return rows;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
singleTrans.Rollback();
throw e;
}
}
}
}
}
/// <summary>
/// 执行查询语句,返回DataTable
/// </summary>
/// <param name = "SQLString" > 查询语句 </ param >
/// < returns > DataSet </ returns >
public static DataTable Query(string SQLString)
{
using (SQLiteConnection connection = new SQLiteConnection(strConn))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
return ds.Tables[0];
}
catch (Exception ex)
{
connection.Close();
throw ex;
}
}
}
/// <summary>
/// 执行存储过程,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteProc(string procName, SQLiteParameter[] coll)
{
using (SQLiteConnection connection = new SQLiteConnection(strConn))
{
//事务
using (SQLiteTransaction singleTrans = connection.BeginTransaction(IsolationLevel.ReadCommitted))
{
using (SQLiteCommand cmd = new SQLiteCommand(connection))
{
try
{
connection.Open();
for (int i = 0; i < coll.Length; i++)
{
cmd.Parameters.Add(coll[i]);
}
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = procName;
cmd.Transaction = singleTrans;
int rows = cmd.ExecuteNonQuery();
singleTrans.Commit();
return rows;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
singleTrans.Rollback();
throw e;
}
}
}
}
}
/// <summary>
/// 执行带参数的SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSqlPar(string sqlPar, SQLiteParameter[] coll)
{
using (SQLiteConnection connection = new SQLiteConnection(strConn))
{
//事务
using (SQLiteTransaction singleTrans = connection.BeginTransaction(IsolationLevel.ReadCommitted))
{
using (SQLiteCommand cmd = new SQLiteCommand(connection))
{
try
{
connection.Open();
for (int i = 0; i < coll.Length; i++)
{
cmd.Parameters.Add(coll[i]);
}
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlPar;
cmd.Transaction = singleTrans;
int rows = cmd.ExecuteNonQuery();
singleTrans.Commit();
return rows;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
singleTrans.Rollback();
throw e;
}
}
}
}
}