标签:scalar .com .exe close sel else ret finally apt
。
注意:
public class DBHelper { private static SqlConnection conn = new SqlConnection("server=.;database=HR;uid=sa;pwd=;"); /// <summary> /// 封装SqlCommand对象的ExecuteNonQuery方法 /// </summary> /// <param name="strsql">传入以insert|update|delete打头的SQL语句</param> /// <returns>返回执行影响的行数</returns> public static int ExecuteNonQuery(string strsql) { SqlCommand comm = new SqlCommand(strsql, conn); try { conn.Open(); return comm.ExecuteNonQuery(); } catch { throw; } finally { conn.Close(); } } /// <summary> /// 返回数据集DataSet /// </summary> /// <param name="strsql">以Select打头的SQL语句</param> /// <returns>返回DataSet对象</returns> public static DataSet GetDataSet(string strsql) { SqlDataAdapter da = new SqlDataAdapter(strsql, conn); DataSet ds = new DataSet(); try { da.Fill(ds); return ds; } catch { throw; } } /// <summary> /// 返回DataTable对象 /// </summary> /// <param name="strsql">以Select打头的SQL语句</param> /// <returns>返回DataTable对象</returns> public static DataTable GetDataTable(string strsql) { DataSet ds = GetDataSet(strsql); if (ds != null) { return ds.Tables[0]; } else return null; } /// <summary> /// 返回首行首列的单元格的值(封装Command对象的ExecuteScalar方法) /// </summary> /// <param name="strsql">Select打头的SQL语句</param> /// <returns>返回的是Object</returns> public static object GetScalar(string strsql) { SqlCommand comm = new SqlCommand(strsql, conn); try { conn.Open(); return comm.ExecuteScalar(); } catch (Exception) { throw; } finally { conn.Close(); } } }
注意:此时没有对sql注入考虑
标签:scalar .com .exe close sel else ret finally apt
原文地址:http://www.cnblogs.com/bfwbfw/p/6341506.html