十款高效简化移动开发过程的工具-软件-网界CNW.com.cn!

技术分享
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Data;
  4 using System.Data.SqlClient;
  5 using System.Text;
  6 using System.Web.Services.Description;
  7 namespace SQL注入式攻击
  8 {
  9 publicclassDBHelper
 10 {
 11 privatestaticstringDBConnectString="server=.;database=meixin;uid=sa;pwd=sa;";
 12 privatestaticSqlConnection conn;
 13 privatestaticSqlDataAdapter da;
 14 privatestaticSqlCommand cmd;
 15 privatestaticDBHelper dBHelper;
 16 publicDBHelper()
 17 {
 18 conn =newSqlConnection(DBConnectString);
 19 }
 20 /// <summary>
 21 /// 实例化DBHelper对象
 22 /// </summary>
 23 /// <returns></returns>
 24 publicstaticDBHelperInstance()
 25 {
 26 if(dBHelper ==null)
 27 {
 28 dBHelper =newDBHelper();
 29 }
 30 return dBHelper;
 31 }
 32 /// <summary>
 33 /// 打开数据库连接
 34 /// </summary>
 35 voidDBOpen()
 36 {
 37 if(conn.State==ConnectionState.Closed)
 38 {
 39 conn.Open();
 40 }
 41 }
 42 /// <summary>
 43 /// 关闭数据库连接
 44 /// </summary>
 45 voidDBClose()
 46 {
 47 if(conn.State==ConnectionState.Open)
 48 {
 49 conn.Close();
 50 }
 51 }
 52 /// <summary>
 53 /// 执行SQL语句获取数据集
 54 /// </summary>
 55 /// <param name="sql">sql语句</param>
 56 /// <returns>DataTable数据集</returns>
 57 publicDataTableGetDataTableBySql(string sql)
 58 {
 59 DBOpen();
 60 DataTable dt =newDataTable();
 61 da =newSqlDataAdapter(sql, conn);
 62 da.Fill(dt);
 63 return dt;
 64 }
 65 /// <summary>
 66 /// 执行SQL语句
 67 /// </summary>
 68 /// <param name="sql">SQL语句</param>
 69 /// <returns>是否执行成功</returns>
 70 public bool ExcuteSql(string sql)
 71 {
 72 DBOpen();
 73 cmd =newSqlCommand(sql, conn);
 74 try
 75 {
 76 cmd.ExecuteNonQuery();
 77 returntrue;
 78 }
 79 catch
 80 {
 81 returnfalse;
 82 }
 83 finally
 84 {
 85 DBClose();
 86 }
 87 }
 88 /// <summary>
 89 /// 执行存储过程
 90 /// </summary>
 91 /// <param name="proName">存储过程名称</param>
 92 /// <param name="paras">存储过程参数</param>
 93 /// <returns>是否执行成功</returns>
 94 public bool ExcuteProcedure(string proName,SqlParameter[] paras)
 95 {
 96 DBOpen();
 97 cmd =newSqlCommand(proName, conn);
 98 cmd.CommandType=CommandType.StoredProcedure;
 99 for(int i =0; i < paras.Length; i++)
100 {
101 cmd.Parameters.Add(paras[i]);
102 }
103 try
104 {
105 cmd.ExecuteNonQuery();
106 returntrue;
107 }
108 catch
109 {
110 returnfalse;
111 }
112 finally
113 {
114 DBClose();
115 }
116 }
117 /// <summary>
118 /// 执行存储过程获得数据集
119 /// </summary>
120 /// <param name="proName">存储过程名</param>
121 /// <param name="paras">存储过程参数</param>
122 /// <returns>DataTable数据集</returns>
123 publicDataTableGetDataTableByProcedure(string proName,SqlParameter[] paras)
124 {
125 DBOpen();
126 cmd =newSqlCommand(proName, conn);
127 cmd.CommandType=CommandType.StoredProcedure;
128 da =newSqlDataAdapter(cmd);
129 DataTable dt =newDataTable();
130 for(int i =0; i < paras.Length; i++)
131 {
132 cmd.Parameters.Add(paras[i]);
133 }
134 try
135 {
136 da.Fill(dt);
137 return dt;
138 }
139 catch
140 {
141 returnnull;
142 }
143 finally
144 {
145 DBClose();
146 }
147 }
148 }
149 }
View Code

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。