事务是指用户定义的一个数据库操作序列,这些操作要么全做要么全不做,它是一个不可分割的工作单位。一个事务可以是一条SQL语句,一组SQL语句,或整个程序。
例子程序:
- public bool transactionOp()
- {
- // 事务成功返回true,事务失败返回false
- bool result = false;
- string SqlConnectionString = "Data Source=.;Initial Catalog=DataBaseName;User ID=sa;pwd=123456;Connection Lifetime=0;max pool size=200";
- SqlConnection cn = new SqlConnection(SqlConnectionString);
- SqlCommand cmd = new SqlCommand();
- SqlTransaction transaction = null;
- try
- {
- // 打开数据库
- if (cn.State == ConnectionState.Closed)
- {
- cn.Open();
- }
- // 开始事务
- transaction = cn.BeginTransaction();
- cmd.Transaction = transaction;
- cmd.Connection = cn;
- // 执行第一条SQL语句
- cmd.CommandType = CommandType.Text;
- cmd.CommandText = "insert into Users values('admin', 'admin')";
- if (cmd.ExecuteNonQuery() < 0)
- throw new Exception();
- // 执行第二条SQL语句
- cmd.CommandType = CommandType.Text;
- cmd.CommandText = "update Users set pwd = '123456' where name = '小明'";
- if (cmd.ExecuteNonQuery() < 0)
- throw new Exception();
- // 提交事务
- transaction.Commit();
- result = true;
- }
- catch
- {
- result = false;
- // 回滚事务
- transaction.Rollback();
- }
- finally
- {
- // 关闭数据库
- if (cn.State == ConnectionState.Open)
- {
- cn.Close();
- }
- cn.Dispose();
- cmd.Dispose();
- transaction.Dispose();
- }
- return result;
- }
博文介绍的SQL Server数据库操作类是C#语言的,可实现对SQL Server数据库的增删改查询等操作。并且该操作类可实现对图片的存储,博文的最后附上如何将Image图片转换为byte[]数组类型,以及如何将byte[]数组类型转换为Image图片。
博文中会贴出该类的下载地址,可以直接下载使用。
1. DataBaseOp类
下载:点击打开链接
http://download.csdn.net/detail/softimite_zifeng/9688497
- using System.Data;
- using System.Data.SqlClient;
- //C#的SQL Server操作类
- public class DataBaseOp
- {
- private string SqlConnectionString; //数据库连接
- /// <summary>
- /// 构造函数
- /// 初始化连接数据库参数
- /// </summary>
- public DataBaseOp()
- {
- SqlConnectionString = "Data Source=.;Initial Catalog=DataBaseName;User ID=sa;pwd=123456;Connection Lifetime=0;max pool size=200";
- }
- /// <summary>
- /// 构造函数
- /// 初始化连接数据库参数
- /// </summary>
- /// <param name="ConSqlServer">连接对象</param>
- public DataBaseOp(string ConSqlServer)
- {
- SqlConnectionString = ConSqlServer;
- }
- /// <summary>
- /// 打开数据库连接
- /// </summary>
- /// <param name="cn">连接</param>
- public void Open(SqlConnection cn)
- {
- if (cn.State == ConnectionState.Closed)
- {
- cn.Open();
- }
- }
- /// <summary>
- /// 关闭数据库连接
- /// </summary>
- /// <param name="cn">连接</param>
- public void Close(SqlConnection cn)
- {
- if (cn != null)
- {
- if (cn.State == ConnectionState.Open)
- {
- cn.Close();
- }
- cn.Dispose();
- }
- }
- /// <summary>
- /// 查询
- /// </summary>
- /// <param name="strSql">SQL语句</param>
- /// <returns>是否存在</returns>
- 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);
- }
- }
- /// <summary>
- /// 查询
- /// </summary>
- /// <param name="strSql">SQL语句</param>
- /// <returns>第一行第一列结果</returns>
- 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);
- }
- }
- /// <summary>
- /// 查询(SqlDataReader)
- /// </summary>
- /// <param name="strSql">SQL语句</param>
- /// <returns>查询结果</returns>
- 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();
- }
- }
- /// <summary>
- /// 查询(DataSet)
- /// </summary>
- /// <param name="strSql">SQL语句</param>
- /// <returns>查询结果</returns>
- 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);
- }
- }
- /// <summary>
- /// 查询(DataSet)
- /// </summary>
- /// <param name="strSql">SQL语句</param>
- /// <param name="tableName">指定DataSet["tableName"]表</param>
- /// <returns>查询结果</returns>
- 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);
- }
- }
- /// <summary>
- /// 查询(DataTable)
- /// </summary>
- /// <param name="strSql">SQL语句</param>
- /// <returns>查询结果</returns>
- public DataTable GetDT(string strSql)
- {
- return GetDS(strSql).Tables[0];
- }
- /// <summary>
- /// 查询(DataView)
- /// </summary>
- /// <param name="strSql">SQL语句</param>
- /// <returns>查询结果</returns>
- public DataView GetDV(string strSql)
- {
- return GetDS(strSql).Tables[0].DefaultView;
- }
- /// <summary>
- /// 增删改,无图片
- /// </summary>
- /// <param name="strSql">SQL语句</param>
- /// <returns>影响的行数</returns>
- 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);
- }
- }
- /// <summary>
- /// 增改,有图片
- /// </summary>
- /// <param name="strSql">SQL语句</param>
- /// <param name="picbyte">图片的二进制数据</param>
- /// <returns>影响的行数</returns>
- 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);
- }
- }
- }
2. Image图片与byte[]数组的相互转换- 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);
- }
- }