一个针对SQLServer操作的封装类

1using System;
2using System.Data.SqlClient;
3using System.Data;
4using System.IO;
5using System.Windows.Forms;
6using System.Collections;
7namespace SQL
8...{
9/**//// <summary>
10/// 作 者:殷庆飞
11/// 时 间:2008-8-1
12/// 用 途:封装针对SQL Server的所有操作
13/// 修 改 人:
14/// 修改日期:
15/// </summary>

16 public class SQL
17...{
18变量和结构#region 变量和结构
19public static SqlConnection Connection = null; //数据库连接对象
20 public static SqlTransaction Transaction = null; //数据库事务
21 public static bool IsInTransaction = false; //是否在事务中
22
23//ParameterEx结构
24 public struct ParameterEx
25...{
26/**//// <summary>
27/// 参数名称
28/// </summary>

29 public string Text;
30/**//// <summary>
31/// 参数值
32/// </summary>

33 public object Value;
34/**//// <summary>
35/// 参数数据类型
36/// </summary>

37 public object DataType;
38/**//// <summary>
39/// 参数传入传出方向
40/// </summary>

41 public object ParaDirection;
42/**//// <summary>
43/// 参数大小
44/// </summary>

45 public object ParaSize;
46 }

47
48
49#endregion

50
51构造函数#region 构造函数
52public SQL()
53...{
54string ConnectionText; //Store The Connection Sentence for Database
55 if (Connection == null)
56...{
57 ConnectionText = ReadConn();
58 Connection = new SqlConnection( ConnectionText );
59try
60...{
61 Connection.Open(); //Open Database
62 }

63catch(Exception e)
64...{
65 Connection = null;
66 MessageBox.Show("连接数据库失败!\n错误信息:" + e.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
67 }

68 }

69 }

70#endregion

71
72读写数据库连接字符串#region 读写数据库连接字符串
73
74//写连接字符串
75 public static void WriteConn(string conn, string conn2, string conn3, string conn4)
76...{
77 conn = Crypt.EncryptAndDecrypt.Encryption( conn );
78 conn2 = Crypt.EncryptAndDecrypt.Encryption( conn2 );
79 conn3 = Crypt.EncryptAndDecrypt.Encryption( conn3 );
80 conn4 = Crypt.EncryptAndDecrypt.Encryption( conn4 );
81 Crypt.EncryptAndDecrypt.WriteIniString("ConnectionCofig", "Server", conn, System.IO.Directory.GetCurrentDirectory()+ "\\DatabaseConnection.ini");
82 Crypt.EncryptAndDecrypt.WriteIniString("ConnectionCofig", "Database", conn2, System.IO.Directory.GetCurrentDirectory()+ "\\DatabaseConnection.ini");
83 Crypt.EncryptAndDecrypt.WriteIniString("ConnectionCofig", "User ID", conn3, System.IO.Directory.GetCurrentDirectory()+ "\\DatabaseConnection.ini");
84 Crypt.EncryptAndDecrypt.WriteIniString("ConnectionCofig", "Password", conn4, System.IO.Directory.GetCurrentDirectory()+ "\\DatabaseConnection.ini");
85 }

86//读连接字符串
87 public static string ReadConn()
88...{
89string conns = Crypt.EncryptAndDecrypt.Decryption(Crypt.EncryptAndDecrypt.GetIniString("ConnectionCofig", "Server", Directory.GetCurrentDirectory ()+ "\\DatabaseConnection.ini"));
90string conns2 = Crypt.EncryptAndDecrypt.Decryption(Crypt.EncryptAndDecrypt.GetIniString("ConnectionCofig", "Database", Directory.GetCurrentDirectory ()+ "\\DatabaseConnection.ini"));
91string conns3 = Crypt.EncryptAndDecrypt.Decryption(Crypt.EncryptAndDecrypt.GetIniString("ConnectionCofig", "User ID", Directory.GetCurrentDirectory ()+ "\\DatabaseConnection.ini"));
92string conns4 = Crypt.EncryptAndDecrypt.Decryption(Crypt.EncryptAndDecrypt.GetIniString("ConnectionCofig", "Password", Directory.GetCurrentDirectory ()+ "\\DatabaseConnection.ini"));
93string str = "Server = " + conns + ";Database = " + conns2 + ";User ID = " + conns3 + ";Password = " + conns4 + ";";
94return str;
95 }

96#endregion

97
98设置和回传参数#region 设置和回传参数
99//设置参数到SqlCommand
100 public static void SetParameters(SqlCommand cmd, ParameterEx[] parameters)
101...{
102if(parameters != null)
103...{
104for(int i = 0;i < parameters.GetLength(0); i++)
105...{
106if ((ParameterDirection)parameters[i].ParaDirection == ParameterDirection.Output)
107...{
108if (parameters[i].ParaSize == null)
109...{
110 cmd.Parameters.Add(new SqlParameter((string)parameters[i].Text, (SqlDbType)parameters[i].DataType)).Direction=(ParameterDirection)parameters[i].ParaDirection;
111 }

112else
113...{
114 cmd.Parameters.Add(new SqlParameter((string)parameters[i].Text, (SqlDbType)parameters[i].DataType,(int)parameters[i].ParaSize)).Direction=(ParameterDirection)parameters[i].ParaDirection;
115 }

116
117 }

118else
119...{
120if (parameters[i].ParaSize == null)
121...{
122

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值