SQL Server事务操作(C#)

事务是指用户定义的一个数据库操作序列,这些操作要么全做要么全不做,它是一个不可分割的工作单位。一个事务可以是一条SQL语句,一组SQL语句,或整个程序。

例子程序:

[csharp] view plain copy
  1. public bool transactionOp()  
  2. {  
  3.     // 事务成功返回true,事务失败返回false  
  4.     bool result = false;  
  5.     string SqlConnectionString = "Data Source=.;Initial Catalog=DataBaseName;User ID=sa;pwd=123456;Connection Lifetime=0;max pool size=200";  
  6.     SqlConnection cn = new SqlConnection(SqlConnectionString);  
  7.     SqlCommand cmd = new SqlCommand();  
  8.     SqlTransaction transaction = null;  
  9.   
  10.     try  
  11.     {  
  12.         // 打开数据库  
  13.         if (cn.State == ConnectionState.Closed)  
  14.         {  
  15.             cn.Open();  
  16.         }  
  17.   
  18.         // 开始事务  
  19.         transaction = cn.BeginTransaction();  
  20.         cmd.Transaction = transaction;  
  21.         cmd.Connection = cn;  
  22.   
  23.         // 执行第一条SQL语句  
  24.         cmd.CommandType = CommandType.Text;  
  25.         cmd.CommandText = "insert into Users values('admin', 'admin')";  
  26.         if (cmd.ExecuteNonQuery() < 0)  
  27.             throw new Exception();  
  28.   
  29.         // 执行第二条SQL语句  
  30.         cmd.CommandType = CommandType.Text;  
  31.         cmd.CommandText = "update Users set pwd = '123456' where name = '小明'";  
  32.         if (cmd.ExecuteNonQuery() < 0)  
  33.             throw new Exception();  
  34.   
  35.         // 提交事务  
  36.         transaction.Commit();  
  37.         result = true;  
  38.     }  
  39.     catch  
  40.     {  
  41.         result = false;  
  42.         // 回滚事务  
  43.         transaction.Rollback();  
  44.     }  
  45.     finally  
  46.     {  
  47.         // 关闭数据库  
  48.         if (cn.State == ConnectionState.Open)  
  49.         {  
  50.             cn.Close();  
  51.         }  
  52.         cn.Dispose();  
  53.         cmd.Dispose();  
  54.         transaction.Dispose();  
  55.     }  
  56.     return result;  
  57. }
  58. 博文介绍的SQL Server数据库操作类是C#语言的,可实现对SQL Server数据库的增删改查询等操作。并且该操作类可实现对图片的存储,博文的最后附上如何将Image图片转换为byte[]数组类型,以及如何将byte[]数组类型转换为Image图片。

           博文中会贴出该类的下载地址,可以直接下载使用。

    1. DataBaseOp类

    下载:点击打开链接

    http://download.csdn.net/detail/softimite_zifeng/9688497

    [csharp] view plain copy
    1. using System.Data;  
    2. using System.Data.SqlClient;  
    3.   
    4. //C#的SQL Server操作类  
    5. public class DataBaseOp  
    6. {  
    7.     private string SqlConnectionString;    //数据库连接  
    8.   
    9.     /// <summary>  
    10.     /// 构造函数  
    11.     /// 初始化连接数据库参数  
    12.     /// </summary>  
    13.     public DataBaseOp()  
    14.     {  
    15.         SqlConnectionString = "Data Source=.;Initial Catalog=DataBaseName;User ID=sa;pwd=123456;Connection Lifetime=0;max pool size=200";  
    16.     }  
    17.   
    18.     /// <summary>  
    19.     /// 构造函数  
    20.     /// 初始化连接数据库参数  
    21.     /// </summary>  
    22.     /// <param name="ConSqlServer">连接对象</param>  
    23.     public DataBaseOp(string ConSqlServer)  
    24.     {  
    25.         SqlConnectionString = ConSqlServer;  
    26.     }  
    27.   
    28.     /// <summary>  
    29.     /// 打开数据库连接  
    30.     /// </summary>  
    31.     /// <param name="cn">连接</param>  
    32.     public void Open(SqlConnection cn)  
    33.     {  
    34.         if (cn.State == ConnectionState.Closed)  
    35.         {  
    36.             cn.Open();  
    37.         }  
    38.     }  
    39.   
    40.     /// <summary>  
    41.     /// 关闭数据库连接  
    42.     /// </summary>  
    43.     /// <param name="cn">连接</param>  
    44.     public void Close(SqlConnection cn)  
    45.     {  
    46.         if (cn != null)  
    47.         {  
    48.             if (cn.State == ConnectionState.Open)  
    49.             {  
    50.                 cn.Close();  
    51.             }  
    52.             cn.Dispose();  
    53.         }  
    54.     }  
    55.   
    56.     /// <summary>  
    57.     /// 查询  
    58.     /// </summary>  
    59.     /// <param name="strSql">SQL语句</param>  
    60.     /// <returns>是否存在</returns>  
    61.     public bool ChaXun(string strSql)  
    62.     {  
    63.         SqlConnection cn = new SqlConnection(SqlConnectionString);  
    64.         SqlCommand cmd = new SqlCommand();  
    65.         try  
    66.         {  
    67.             Open(cn);  
    68.             cmd = new SqlCommand(strSql, cn);  
    69.             return cmd.ExecuteReader().Read();  
    70.         }  
    71.         catch (Exception e)  
    72.         {  
    73.             throw new Exception(e.Message);  
    74.         }  
    75.         finally  
    76.         {  
    77.             cmd.Dispose();  
    78.             Close(cn);  
    79.         }  
    80.     }  
    81.   
    82.     /// <summary>  
    83.     /// 查询  
    84.     /// </summary>  
    85.     /// <param name="strSql">SQL语句</param>  
    86.     /// <returns>第一行第一列结果</returns>  
    87.     public string ChaXun2(string strSql)  
    88.     {  
    89.         SqlConnection cn = new SqlConnection(SqlConnectionString);  
    90.         SqlCommand cmd = new SqlCommand();  
    91.         try  
    92.         {  
    93.             Open(cn);  
    94.             cmd = new SqlCommand(strSql, cn);  
    95.             return cmd.ExecuteScalar().ToString().Trim();  
    96.         }  
    97.         catch (Exception e)  
    98.         {  
    99.             throw new Exception(e.Message);  
    100.         }  
    101.         finally  
    102.         {  
    103.             cmd.Dispose();  
    104.             Close(cn);  
    105.         }  
    106.     }  
    107.   
    108.     /// <summary>  
    109.     /// 查询(SqlDataReader)  
    110.     /// </summary>  
    111.     /// <param name="strSql">SQL语句</param>  
    112.     /// <returns>查询结果</returns>  
    113.     public SqlDataReader GetDR(string strSql)  
    114.     {  
    115.         SqlConnection cn = new SqlConnection(SqlConnectionString);  
    116.         SqlCommand cmd = new SqlCommand();  
    117.         try  
    118.         {  
    119.             Open(cn);  
    120.             cmd = new SqlCommand(strSql, cn);  
    121.             return cmd.ExecuteReader(CommandBehavior.CloseConnection);  
    122.         }  
    123.         catch (Exception e)  
    124.         {  
    125.             throw new Exception(e.Message);  
    126.         }  
    127.         finally  
    128.         {  
    129.             cmd.Dispose();  
    130.         }  
    131.     }  
    132.   
    133.     /// <summary>  
    134.     /// 查询(DataSet)  
    135.     /// </summary>  
    136.     /// <param name="strSql">SQL语句</param>  
    137.     /// <returns>查询结果</returns>  
    138.     public DataSet GetDS(string strSql)  
    139.     {  
    140.         SqlConnection cn = new SqlConnection(SqlConnectionString);  
    141.         SqlDataAdapter sda = new SqlDataAdapter();  
    142.         try  
    143.         {  
    144.             Open(cn);  
    145.             sda = new SqlDataAdapter(strSql, cn);  
    146.             DataSet ds = new DataSet();  
    147.             sda.Fill(ds);  
    148.             return ds;  
    149.         }  
    150.         catch (Exception e)  
    151.         {  
    152.             throw new Exception(e.Message);  
    153.         }  
    154.         finally  
    155.         {  
    156.             sda.Dispose();  
    157.             Close(cn);  
    158.         }  
    159.     }  
    160.   
    161.     /// <summary>  
    162.     /// 查询(DataSet)  
    163.     /// </summary>  
    164.     /// <param name="strSql">SQL语句</param>  
    165.     /// <param name="tableName">指定DataSet["tableName"]表</param>  
    166.     /// <returns>查询结果</returns>  
    167.     public DataSet GetDS(string strSql, string tableName)  
    168.     {  
    169.         SqlConnection cn = new SqlConnection(SqlConnectionString);  
    170.         SqlDataAdapter sda = new SqlDataAdapter();  
    171.         try  
    172.         {  
    173.             Open(cn);  
    174.             sda = new SqlDataAdapter(strSql, cn);  
    175.             DataSet ds = new DataSet();  
    176.             sda.Fill(ds, tableName);  
    177.             return ds;  
    178.         }  
    179.         catch (Exception e)  
    180.         {  
    181.             throw new Exception(e.Message);  
    182.         }  
    183.         finally  
    184.         {  
    185.             sda.Dispose();  
    186.             Close(cn);  
    187.         }  
    188.     }  
    189.   
    190.     /// <summary>  
    191.     /// 查询(DataTable)  
    192.     /// </summary>  
    193.     /// <param name="strSql">SQL语句</param>  
    194.     /// <returns>查询结果</returns>  
    195.     public DataTable GetDT(string strSql)  
    196.     {  
    197.         return GetDS(strSql).Tables[0];  
    198.     }  
    199.   
    200.     /// <summary>  
    201.     /// 查询(DataView)  
    202.     /// </summary>  
    203.     /// <param name="strSql">SQL语句</param>  
    204.     /// <returns>查询结果</returns>  
    205.     public DataView GetDV(string strSql)  
    206.     {  
    207.         return GetDS(strSql).Tables[0].DefaultView;  
    208.     }  
    209.   
    210.     /// <summary>  
    211.     /// 增删改,无图片  
    212.     /// </summary>  
    213.     /// <param name="strSql">SQL语句</param>  
    214.     /// <returns>影响的行数</returns>  
    215.     public int RunSql(string strSql)  
    216.     {  
    217.         SqlConnection cn = new SqlConnection(SqlConnectionString);  
    218.         SqlCommand cmd = new SqlCommand();  
    219.         try  
    220.         {  
    221.             Open(cn);  
    222.             cmd = new SqlCommand(strSql, cn);  
    223.             return cmd.ExecuteNonQuery();  
    224.         }  
    225.         catch (Exception e)  
    226.         {  
    227.             throw new Exception(e.Message);  
    228.         }  
    229.         finally  
    230.         {  
    231.             cmd.Dispose();  
    232.             Close(cn);  
    233.         }  
    234.     }  
    235.   
    236.     /// <summary>  
    237.     /// 增改,有图片  
    238.     /// </summary>  
    239.     /// <param name="strSql">SQL语句</param>  
    240.     /// <param name="picbyte">图片的二进制数据</param>  
    241.     /// <returns>影响的行数</returns>  
    242.     public int RunSql(string strSql, byte[] picbyte)  
    243.     {  
    244.         SqlConnection cn = new SqlConnection(SqlConnectionString);  
    245.         SqlCommand cmd = new SqlCommand();  
    246.         try  
    247.         {  
    248.             Open(cn);  
    249.             cmd = new SqlCommand(strSql, cn);  
    250.             cmd.Parameters.Add("@Image", SqlDbType.Image);  
    251.             cmd.Parameters["@Image"].Value = picbyte;  
    252.             return cmd.ExecuteNonQuery();  
    253.         }  
    254.         catch (Exception e)  
    255.         {  
    256.             throw new Exception(e.Message);  
    257.         }  
    258.         finally  
    259.         {  
    260.             cmd.Dispose();  
    261.             Close(cn);  
    262.         }  
    263.     }  
    264. }  

    2. Image图片与byte[]数组的相互转换

    [csharp] view plain copy
    1. using System.Drawing;  
    2. using System.IO;  
    3. using System.Drawing.Imaging;  
    4.   
    5. // 将Image转换为二进制序列  
    6. public static byte[] ImageToBytes(Image image)  
    7. {  
    8.     MemoryStream ms = new MemoryStream();  
    9.     image.Save(ms, ImageFormat.Jpeg);  
    10.     byte[] bytes = new Byte[ms.Length];  
    11.     ms.Position = 0;  
    12.     ms.Read(bytes, 0, bytes.Length);  
    13.     ms.Close();  
    14.     return bytes;  
    15. }  
    16.   
    17. // 将二进制序列转换为Image  
    18. public static Image BytesToImage(byte[] bytes)  
    19. {  
    20.     try  
    21.     {  
    22.         using (Stream fStream = new MemoryStream(bytes.Length))  
    23.         {  
    24.             BinaryWriter bw = new BinaryWriter(fStream);  
    25.             bw.Write(bytes);  
    26.             bw.Flush();  
    27.             Bitmap bitMap = new Bitmap(fStream);  
    28.             bw.Close();   
    29.             fStream.Close();  
    30.             Image image = Image.FromHbitmap(bitMap.GetHbitmap());  
    31.             return image;  
    32.         }  
    33.     }  
    34.     catch (IOException ex)  
    35.     {  
    36.         throw new Exception("读取图片失败:" + ex.Message);  
    37.   
    38.     }  
    39. }  
      

猜你喜欢

转载自blog.csdn.net/andrewniu/article/details/80166113