常用的查询的方法
返回一个Table的方法!
public static DataTable GetDataTable(string sql) { DataTable dt = new DataTable(); if (!string.IsNullOrEmpty(sql)) { SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbString"].ConnectionString); try { conn.Open(); SqlDataAdapter da = new SqlDataAdapter(sql, conn); da.Fill(dt); } catch (Exception ex) { throw ex; } finally { conn.Close(); } } return dt; }
返回受影响的行数!
public static int ExecuteNonQuery(string sql, SqlParameter[] sqlParams) { int count = 0; SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbString"].ConnectionString); if (!string.IsNullOrEmpty(sql) && sqlParams != null && sqlParams.Length > 0) { try { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddRange(sqlParams); count = cmd.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } finally { conn.Close(); } } return count; }