SQL Server安装包
MySQL安装包
全部代码
mysql和sql server的操作基本一致,唯一不一样的就是各种名称空间,类名不太一样,如:SqlCommand和MySqlCommand,Sql开头的变成MySql开头 这里连接的是sql server数据库
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
/// <summary>
/// MSSQL数据库访问
///
/// </summary>
public abstract class SqlHelper
{
#region 连接字符串
private static string connectionString = "Data Source=.;Initial Catalog=S;Integrated Security=True";//连接字符串
#endregion
#region 初始化连接数据库
/// <summary>
/// 设置SqlCommand命令
/// </summary>
/// <param name="cmd">sql命令对象</param>
/// <param name="conn">sql连接对象</param>
/// <param name="trans">sql事务对象</param>
/// <param name="cmdText">sql语句</param>
/// <param name="cmdParms">sql参数</param>
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = CommandType.Text;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
#endregion
#region 执行操作
/*
* SqlParameter用法:
* 例如:string sql = "insert into T values(@name)";
* SqlParameter name = new SqlParameter("name","取一个名字");
* ExecuteSql(sql,name)
* ExecuteSql(sql,new SqlParameter("name","取一个名字"))
* 没有参数可以不写cmdParms
*/
/// <summary>
/// 执行sql操作,一般insert,update,delete
/// </summary>
/// <param name="sql">sql字符串</param>
/// <param name="cmdParms">sql参数,可以为空</param>
/// <returns>返回受影响的行数</returns>
public static int ExecuteSql(string sql, params SqlParameter[] cmdParms)
{
using(SqlConnection connection=new SqlConnection(connectionString))
{
using(SqlCommand cmd=new SqlCommand())
{
try
{
PrepareCommand(cmd, connection, null, sql, cmdParms);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (System.Data.SqlClient.SqlException E)
{
throw new Exception(E.Message);
}
}
}
}
/// <summary>
/// 得到第一行第一列的值,可用于计算count,max,min,avg,sum等
/// </summary>
/// <param name="sql">sql字符串</param>
/// <param name="cmdParms">sql参数,可以为空</param>
/// <returns>返回查询到的结果object类型</returns>
public static object GetFrist(string sql, params SqlParameter[] cmdParms)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
PrepareCommand(cmd, connection, null, sql, cmdParms);
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
if (Equals(obj, null) || Equals(obj, System.DBNull.Value))//判断是否返回的值为空
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
}
}
}
/// <summary>
/// 得到一个reader对象,遍历这个对象获得数据
/// 用法:while(reader.Read())
/// {
/// int id=reader.GetInt32(0);//列的索引
/// int id=(int)reader[0]
/// int id=(int)reader["id"]
/// }
/// </summary>
/// <param name="sql">sql字符串</param>
/// <param name="cmdParms">sql参数,可以为空</param>
/// <returns>reader对象</returns>
public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] cmdParms)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
PrepareCommand(cmd, connection, null, sql, cmdParms);
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return reader;
}
catch (System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
}
}
}
/// <summary>
/// 得到一个DataTable对象,实现查询数据
/// 用法:dt.Rows[5][0]或者dt.Rows[5]["id"]等
/// </summary>
/// <param name="sql">sql字符串</param>
/// <param name="cmdParms">sql参数,可以为空</param>
/// <returns>返回一个datatable对象</returns>
public static DataTable GetDataTable(string sql, params SqlParameter[] cmdParms)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using(SqlCommand cmd = new SqlCommand())
{
PrepareCommand(cmd, connection, null, sql, cmdParms);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
try
{
da.Fill(dt);
cmd.Parameters.Clear();
return dt;
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
}
}
}
}
/// <summary>
/// 执行sql事务
/// 确保事务发生的一致性,都成功了就提交,失败异常就回滚
/// 如果没有参数,哈希表的value写null
/// 执行sql如果返回0行也叫执行成功了
/// </summary>
/// <param name="SQLStringList">哈希表,key为sql语句,value为SqlParameter[]</param>
public static void ExecuteSqlTran(Hashtable SQLStringList)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlTransaction trans = conn.BeginTransaction())
{
using(SqlCommand cmd = new SqlCommand())
{
try
{
//循环
foreach (DictionaryEntry myDE in SQLStringList)
{
string sql = myDE.Key.ToString();
SqlParameter[] cmdParms = (SqlParameter[])myDE.Value;
PrepareCommand(cmd, conn, trans, sql, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
trans.Commit();
}
catch
{
trans.Rollback();
throw;
}
}
}
}
}
/// <summary>
/// 判断是否存在记录
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="cmdParms">sql参数,可以为空</param>
/// <returns></returns>
public static bool Exists(string sql, params SqlParameter[] cmdParms)
{
try
{
if (GetDataTable(sql, cmdParms).Rows.Count > 0)
{
return true;
}
else
{
return false;
}
}
catch (System.Data.SqlClient.SqlException E)
{
throw new Exception(E.Message);
}
}
#endregion
}