string query = "19,21,24";
string[] param = query.Split(',');
string temp = "";
for (int i = 0; i < param.Length; i++)
{
temp += "@param" + i.ToString() + ",";
}
string queryString = "SELECT id,gender,name FROM [user] Where id in(" + temp.Substring(0, temp.Length - 1) + ")";
SqlCommand cmd = new SqlCommand(queryString, conn);
for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add("@param" + i.ToString(), SqlDbType.Int).Value = param[i];
}
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
SqlParameter数组添加
protected void Button1_Click(object sender, EventArgs e)
{
string strSql="insert into Invoice_Rebate_Customer_L (FORM_ID,LINE_NO) values (@formNo,@lineNo)";
SqlParameter[] paras =new SqlParameter[2];
paras[0] = new SqlParameter("@formNo", SqlDbType.NVarChar);
paras[0].Value = "88888";
paras[1] = new SqlParameter("@lineNo", SqlDbType.Int);
paras[1].Value = 8;
ExecuteSql(strSql, paras, CommandType.Text);
}
void ExecuteSql(string strSql,SqlParameter[] paras,CommandType cmdType)
{
SqlConnection con = new SqlConnection("Server=aukm2;Database=BU;UID=buuser;PWD=buinfo");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = cmdType;
cmd.CommandText = strSql;
foreach (SqlParameter p in paras)
{
cmd.Parameters.Add(p);
}
cmd.ExecuteNonQuery();
con.Close();
}
一、AccessOperate类
using System.Data;
using System.Data.OleDb;
//C#的Microsoft Access操作类
public class AccessOperate
{
private string OleDbConnectionString; //数据库连接
///
/// 构造函数
/// 初始化连接数据库参数
///
public AccessOperate()
{
//Microsoft Access2003的连接语句
//OleDbConnectionString = "Provider = Microsoft.ACE.OLEDB.4.0;Data Source=.\\Data\\DataBaseName.mdb;Jet OLEDB:Database Password=123456";
//Microsoft Access2007及以上的连接语句
OleDbConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source=.\\Data\\DataBaseName.accdb;Jet OLEDB:Database Password=123456";
}
///
/// 构造函数
/// 初始化连接数据库参数
///
/// 连接对象
public AccessOperate(string ConSqlServer)
{
OleDbConnectionString = ConSqlServer;
}
///
/// 打开数据库连接
///
/// 连接
public void Open(OleDbConnection cnn)
{
if (cnn.State == ConnectionState.Closed)
{
cnn.Open();
}
}
///
/// 关闭数据库连接
///
/// 连接
public void Close(OleDbConnection cnn)
{
if (cnn != null)
{
if (cnn.State == ConnectionState.Open)
{
cnn.Close();
}
cnn.Dispose();
}
}
///
/// 查询
///
/// SQL语句
/// 是否存在
public bool ChaXun(string strSql)
{
OleDbConnection cnn = new OleDbConnection(OleDbConnectionString);
OleDbCommand cmd = new OleDbCommand();
try
{
Open(cnn);
cmd = new OleDbCommand(strSql, cnn);
return cmd.ExecuteReader().Read();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
Close(cnn);
}
}
///
/// 查询
///
/// SQL语句
/// 第一行第一列结果
public string ChaXun2(string strSql)
{
OleDbConnection cnn = new OleDbConnection(OleDbConnectionString);
OleDbCommand cmd = new OleDbCommand();
try
{
Open(cnn);
cmd = new OleDbCommand(strSql, cnn);
return cmd.ExecuteScalar().ToString().Trim();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
Close(cnn);
}
}
///
/// 查询(OleDbDataReader)
///
/// SQL语句
/// 查询结果
public OleDbDataReader GetDR(string strSql)
{
OleDbConnection cnn = new OleDbConnection(OleDbConnectionString);
OleDbCommand cmd = new OleDbCommand();
try
{
Open(cnn);
cmd = new OleDbCommand(strSql, cnn);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
}
}
///
/// 查询(DataSet)
///
/// SQL语句
/// 查询结果
public DataSet GetDS(string strSql)
{
OleDbConnection cnn = new OleDbConnection(OleDbConnectionString);
OleDbDataAdapter sda = new OleDbDataAdapter();
try
{
Open(cnn);
sda = new OleDbDataAdapter(strSql, cnn);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
sda.Dispose();
Close(cnn);
}
}
///
/// 查询(DataSet)
///
/// SQL语句
/// 指定DataSet["tableName"]表
/// 查询结果
public DataSet GetDS(string strSql, string tableName)
{
OleDbConnection cnn = new OleDbConnection(OleDbConnectionString);
OleDbDataAdapter sda = new OleDbDataAdapter();
try
{
Open(cnn);
sda = new OleDbDataAdapter(strSql, cnn);
DataSet ds = new DataSet();
sda.Fill(ds, tableName);
return ds;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
sda.Dispose();
Close(cnn);
}
}
///
/// 查询(DataTable)
///
/// SQL语句
/// 查询结果
public DataTable GetDT(string strSql)
{
return GetDS(strSql).Tables[0];
}
///
/// 查询(DataView)
///
/// SQL语句
/// 查询结果
public DataView GetDV(string strSql)
{
return GetDS(strSql).Tables[0].DefaultView;
}
///
/// 增删改,无图片
///
/// SQL语句
/// 影响的行数
public int RunSql(string strSql)
{
OleDbConnection cnn = new OleDbConnection(OleDbConnectionString);
OleDbCommand cmd = new OleDbCommand();
try
{
Open(cnn);
cmd = new OleDbCommand(strSql, cnn);
return cmd.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
Close(cnn);
}
}
///
/// 增改,有图片
///
/// SQL语句
/// 图片的二进制数据
/// 影响的行数
public int RunSql(string strSql, byte[] picbyte)
{
OleDbConnection cnn = new OleDbConnection(OleDbConnectionString);
OleDbCommand cmd = new OleDbCommand();
try
{
Open(cnn);
cmd = new OleDbCommand(strSql, cnn);
cmd.Parameters.AddWithValue("@Image", SqlDbType.Image);
cmd.Parameters["@Image"].Value = picbyte;
return cmd.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
Close(cnn);
}
}
}
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
// 将Image转换为二进制序列
public static byte[] ImageToBytes(Image image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Jpeg);
byte[] bytes = new Byte[ms.Length];
ms.Position = 0;
ms.Read(bytes, 0, bytes.Length);
ms.Close();
return bytes;
}
// 将二进制序列转换为Image
public static Image BytesToImage(byte[] bytes)
{
try
{
using (Stream fStream = new MemoryStream(bytes.Length))
{
BinaryWriter bw = new BinaryWriter(fStream);
bw.Write(bytes);
bw.Flush();
Bitmap bitMap = new Bitmap(fStream);
bw.Close();
fStream.Close();
Image image = Image.FromHbitmap(bitMap.GetHbitmap());
return image;
}
}
catch (IOException ex)
{
throw new Exception("读取图片失败:" + ex.Message);
}
}
二、SQL Server数据库操作类(C#)
using System.Data;
using System.Data.SqlClient;
//C#的SQL Server操作类
public class DataBaseOp
{
private string SqlConnectionString; //数据库连接
///
/// 构造函数
/// 初始化连接数据库参数
///
public DataBaseOp()
{
SqlConnectionString = "Data Source=.;Initial Catalog=DataBaseName;User ID=sa;pwd=123456;Connection Lifetime=0;max pool size=200";
}
///
/// 构造函数
/// 初始化连接数据库参数
///
/// 连接对象
public DataBaseOp(string ConSqlServer)
{
SqlConnectionString = ConSqlServer;
}
///
/// 打开数据库连接
///
/// 连接
public void Open(SqlConnection cn)
{
if (cn.State == ConnectionState.Closed)
{
cn.Open();
}
}
///
/// 关闭数据库连接
///
/// 连接
public void Close(SqlConnection cn)
{
if (cn != null)
{
if (cn.State == ConnectionState.Open)
{
cn.Close();
}
cn.Dispose();
}
}
///
/// 查询
///
/// SQL语句
/// 是否存在
public bool ChaXun(string strSql)
{
SqlConnection cn = new SqlConnection(SqlConnectionString);
SqlCommand cmd = new SqlCommand();
try
{
Open(cn);
cmd = new SqlCommand(strSql, cn);
return cmd.ExecuteReader().Read();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
Close(cn);
}
}
///
/// 查询
///
/// SQL语句
/// 第一行第一列结果
public string ChaXun2(string strSql)
{
SqlConnection cn = new SqlConnection(SqlConnectionString);
SqlCommand cmd = new SqlCommand();
try
{
Open(cn);
cmd = new SqlCommand(strSql, cn);
return cmd.ExecuteScalar().ToString().Trim();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
Close(cn);
}
}
///
/// 查询(SqlDataReader)
///
/// SQL语句
/// 查询结果
public SqlDataReader GetDR(string strSql)
{
SqlConnection cn = new SqlConnection(SqlConnectionString);
SqlCommand cmd = new SqlCommand();
try
{
Open(cn);
cmd = new SqlCommand(strSql, cn);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
}
}
///
/// 查询(DataSet)
///
/// SQL语句
/// 查询结果
public DataSet GetDS(string strSql)
{
SqlConnection cn = new SqlConnection(SqlConnectionString);
SqlDataAdapter sda = new SqlDataAdapter();
try
{
Open(cn);
sda = new SqlDataAdapter(strSql, cn);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
sda.Dispose();
Close(cn);
}
}
///
/// 查询(DataSet)
///
/// SQL语句
/// 指定DataSet["tableName"]表
/// 查询结果
public DataSet GetDS(string strSql, string tableName)
{
SqlConnection cn = new SqlConnection(SqlConnectionString);
SqlDataAdapter sda = new SqlDataAdapter();
try
{
Open(cn);
sda = new SqlDataAdapter(strSql, cn);
DataSet ds = new DataSet();
sda.Fill(ds, tableName);
return ds;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
sda.Dispose();
Close(cn);
}
}
///
/// 查询(DataTable)
///
/// SQL语句
/// 查询结果
public DataTable GetDT(string strSql)
{
return GetDS(strSql).Tables[0];
}
///
/// 查询(DataView)
///
/// SQL语句
/// 查询结果
public DataView GetDV(string strSql)
{
return GetDS(strSql).Tables[0].DefaultView;
}
///
/// 增删改,无图片
///
/// SQL语句
/// 影响的行数
public int RunSql(string strSql)
{
SqlConnection cn = new SqlConnection(SqlConnectionString);
SqlCommand cmd = new SqlCommand();
try
{
Open(cn);
cmd = new SqlCommand(strSql, cn);
return cmd.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
Close(cn);
}
}
///
/// 增改,有图片
///
/// SQL语句
/// 图片的二进制数据
/// 影响的行数
public int RunSql(string strSql, byte[] picbyte)
{
SqlConnection cn = new SqlConnection(SqlConnectionString);
SqlCommand cmd = new SqlCommand();
try
{
Open(cn);
cmd = new SqlCommand(strSql, cn);
cmd.Parameters.Add("@Image", SqlDbType.Image);
cmd.Parameters["@Image"].Value = picbyte;
return cmd.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
Close(cn);
}
}
}
C# MySQL 数据库操作类
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
namespace Common
{
public abstract class MySqlHelper
{
//数据库连接字符串
public static string Conn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString.ToString();
///
/// 给定连接的数据库用假设参数执行一个sql命令(不返回数据集)
///
/// 一个有效的连接字符串
/// 命令类型(存储过程, 文本, 等等)
/// 存储过程名称或者sql命令语句
/// 执行命令所用参数的集合
/// 执行命令所影响的行数
public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
{
MySqlCommand cmd = new MySqlCommand();
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
///
/// 用现有的数据库连接执行一个sql命令(不返回数据集)
///
/// 一个现有的数据库连接
/// 命令类型(存储过程, 文本, 等等)
/// 存储过程名称或者sql命令语句
/// 执行命令所用参数的集合
/// 执行命令所影响的行数
public static int ExecuteNonQuery(MySqlConnection connection, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
{
MySqlCommand cmd = new MySqlCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
///
///使用现有的SQL事务执行一个sql命令(不返回数据集)
///
///
///举例:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24));
///
/// 一个现有的事务
/// 命令类型(存储过程, 文本, 等等)
/// 存储过程名称或者sql命令语句
/// 执行命令所用参数的集合
/// 执行命令所影响的行数
public static int ExecuteNonQuery(MySqlTransaction trans, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
{
MySqlCommand cmd = new MySqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
///
/// 用执行的数据库连接执行一个返回数据集的sql命令
///
///
/// 举例:
/// MySqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24));
///
/// 一个有效的连接字符串
/// 命令类型(存储过程, 文本, 等等)
/// 存储过程名称或者sql命令语句
/// 执行命令所用参数的集合
/// 包含结果的读取器
public static MySqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
{
//创建一个MySqlCommand对象
MySqlCommand cmd = new MySqlCommand();
//创建一个MySqlConnection对象
MySqlConnection conn = new MySqlConnection(connectionString);
//在这里我们用一个try/catch结构执行sql文本命令/存储过程,因为如果这个方法产生一个异常我们要关闭连接,因为没有读取器存在,
//因此commandBehaviour.CloseConnection 就不会执行
try
{
//调用 PrepareCommand 方法,对 MySqlCommand 对象设置参数
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
//调用 MySqlCommand 的 ExecuteReader 方法
MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
//清除参数
cmd.Parameters.Clear();
return reader;
}
catch
{
//关闭连接,抛出异常
conn.Close();
throw;
}
}
///
/// 返回DataSet
///
/// 一个有效的连接字符串
/// 命令类型(存储过程, 文本, 等等)
/// 存储过程名称或者sql命令语句
/// 执行命令所用参数的集合
///
public static DataSet GetDataSet(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
{
//创建一个MySqlCommand对象
MySqlCommand cmd = new MySqlCommand();
//创建一个MySqlConnection对象
MySqlConnection conn = new MySqlConnection(connectionString);
//在这里我们用一个try/catch结构执行sql文本命令/存储过程,
//因为如果这个方法产生一个异常我们要关闭连接,因为没有读取器存在,
try
{
//调用 PrepareCommand 方法,对 MySqlCommand 对象设置参数
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
//调用 MySqlCommand 的 ExecuteReader 方法
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = cmd;
DataSet ds = new DataSet();
adapter.Fill(ds);
//清除参数
cmd.Parameters.Clear();
conn.Close();
return ds;
}
catch (Exception e)
{
throw e;
}
}
///
/// 用指定的数据库连接字符串执行一个命令并返回一个数据集的第一列
///
///
///例如:
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24));
///
///一个有效的连接字符串
/// 命令类型(存储过程, 文本, 等等)
/// 存储过程名称或者sql命令语句
/// 执行命令所用参数的集合
/// 用 Convert.To{Type}把类型转换为想要的
public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
{
MySqlCommand cmd = new MySqlCommand();
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
///
/// 用指定的数据库连接执行一个命令并返回一个数据集的第一列
///
///
/// 例如:
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24));
///
/// 一个存在的数据库连接
/// 命令类型(存储过程, 文本, 等等)
/// 存储过程名称或者sql命令语句
/// 执行命令所用参数的集合
/// 用 Convert.To{Type}把类型转换为想要的
public static object ExecuteScalar(MySqlConnection connection, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
{
MySqlCommand cmd = new MySqlCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
///
/// 准备执行一个命令
///
/// sql命令
/// OleDb连接
/// OleDb事务
/// 命令类型例如 存储过程或者文本
/// 命令文本,例如:Select * from Products
/// 执行命令的参数
public static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, CommandType cmdType, string cmdText, MySqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (MySqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
}
}