1using System;
2using System.Data;
3using System.Data.SqlClient;
4using System.Configuration;
5
6namespace Briefcase
7{
8 public static class SqlHelper
9 {
10 private static string strConn;
11
12 static SqlHelper()
13 {
14 strConn = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
15 }
16
17 public static int ExecuteNonQuery(string sql)
18 {
19 return ExecuteNonQuery(sql, null);
20 }
21
22 public static int ExecuteNonQuery(string sql, SqlParameter[] p)
23 {
24 SqlConnection cnn = new SqlConnection(strConn);
25 cnn.Open();
26 SqlCommand cmd = new SqlCommand(sql,cnn);
27 cmd.CommandType = CommandType.StoredProcedure;
28 if (p != null)
29 {
30 for (int i = 0; i < p.Length; i++)
31 {
32 cmd.Parameters.Add(p[i]);
33 }
34 }
35 int retval = cmd.ExecuteNonQuery();
36 cnn.Close();
37 return retval;
38 }
39
40 public static object ExecuteScalar(string sql)
41 {
42 return ExecuteScalar(sql, null);
43 }
44
45 public static object ExecuteScalar(string sql, SqlParameter[] p)
46 {
47 SqlConnection cnn = new SqlConnection(strConn);
48 cnn.Open();
49 SqlCommand cmd = new SqlCommand(sql, cnn);
50 cmd.CommandType = CommandType.StoredProcedure;
51 if (p != null)
52 {
53 for (int i = 0; i < p.Length; i++)
54 {
55 cmd.Parameters.Add(p[i]);
56 }
57 }
58 object retval = cmd.ExecuteScalar();
59 cnn.Close();
60 return retval;
61 }
62
63 public static SqlDataReader ExecuteReader(string sql)
64 {
65 return ExecuteReader(sql, null);
66 }
67
68 public static SqlDataReader ExecuteReader(string sql, SqlParameter[] p)
69 {
70 SqlConnection cnn = new SqlConnection(strConn);
71 cnn.Open();
72 SqlCommand cmd = new SqlCommand(sql, cnn);
73 cmd.CommandType = CommandType.StoredProcedure;
74 if (p != null)
75 {
76 for (int i = 0; i < p.Length; i++)
77 {
78 cmd.Parameters.Add(p[i]);
79 }
80 }
81 SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
82 return reader;
83 }
84
85 public static DataSet ExecuteDataSet(string sql)
86 {
87 return ExecuteDataSet(sql, null);
88 }
89
90 public static DataSet ExecuteDataSet(string sql, SqlParameter[] p)
91 {
92 SqlConnection cnn = new SqlConnection(strConn);
93 cnn.Open();
94 SqlCommand cmd = new SqlCommand(sql, cnn);
95 cmd.CommandType = CommandType.StoredProcedure;
96 if (p != null)
97 {
98 for (int i = 0; i < p.Length; i++)
99 {
100 cmd.Parameters.Add(p[i]);
101 }
102 }
103 SqlDataAdapter da = new SqlDataAdapter();
104 da.SelectCommand = cmd;
105 DataSet ds = new DataSet();
106 da.Fill(ds);
107 cnn.Close();
108 return ds;
109 }
110
111 }
112}
113
2using System.Data;
3using System.Data.SqlClient;
4using System.Configuration;
5
6namespace Briefcase
7{
8 public static class SqlHelper
9 {
10 private static string strConn;
11
12 static SqlHelper()
13 {
14 strConn = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
15 }
16
17 public static int ExecuteNonQuery(string sql)
18 {
19 return ExecuteNonQuery(sql, null);
20 }
21
22 public static int ExecuteNonQuery(string sql, SqlParameter[] p)
23 {
24 SqlConnection cnn = new SqlConnection(strConn);
25 cnn.Open();
26 SqlCommand cmd = new SqlCommand(sql,cnn);
27 cmd.CommandType = CommandType.StoredProcedure;
28 if (p != null)
29 {
30 for (int i = 0; i < p.Length; i++)
31 {
32 cmd.Parameters.Add(p[i]);
33 }
34 }
35 int retval = cmd.ExecuteNonQuery();
36 cnn.Close();
37 return retval;
38 }
39
40 public static object ExecuteScalar(string sql)
41 {
42 return ExecuteScalar(sql, null);
43 }
44
45 public static object ExecuteScalar(string sql, SqlParameter[] p)
46 {
47 SqlConnection cnn = new SqlConnection(strConn);
48 cnn.Open();
49 SqlCommand cmd = new SqlCommand(sql, cnn);
50 cmd.CommandType = CommandType.StoredProcedure;
51 if (p != null)
52 {
53 for (int i = 0; i < p.Length; i++)
54 {
55 cmd.Parameters.Add(p[i]);
56 }
57 }
58 object retval = cmd.ExecuteScalar();
59 cnn.Close();
60 return retval;
61 }
62
63 public static SqlDataReader ExecuteReader(string sql)
64 {
65 return ExecuteReader(sql, null);
66 }
67
68 public static SqlDataReader ExecuteReader(string sql, SqlParameter[] p)
69 {
70 SqlConnection cnn = new SqlConnection(strConn);
71 cnn.Open();
72 SqlCommand cmd = new SqlCommand(sql, cnn);
73 cmd.CommandType = CommandType.StoredProcedure;
74 if (p != null)
75 {
76 for (int i = 0; i < p.Length; i++)
77 {
78 cmd.Parameters.Add(p[i]);
79 }
80 }
81 SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
82 return reader;
83 }
84
85 public static DataSet ExecuteDataSet(string sql)
86 {
87 return ExecuteDataSet(sql, null);
88 }
89
90 public static DataSet ExecuteDataSet(string sql, SqlParameter[] p)
91 {
92 SqlConnection cnn = new SqlConnection(strConn);
93 cnn.Open();
94 SqlCommand cmd = new SqlCommand(sql, cnn);
95 cmd.CommandType = CommandType.StoredProcedure;
96 if (p != null)
97 {
98 for (int i = 0; i < p.Length; i++)
99 {
100 cmd.Parameters.Add(p[i]);
101 }
102 }
103 SqlDataAdapter da = new SqlDataAdapter();
104 da.SelectCommand = cmd;
105 DataSet ds = new DataSet();
106 da.Fill(ds);
107 cnn.Close();
108 return ds;
109 }
110
111 }
112}
113