JavaEE精讲-MyBatis框架实战-SQL复习


 
1 <connectionStrings>2 <add name="connStr" connectionString="server=.;uid=sa;pwd=****;database=****;Charset=utf8"/>3 </connectionStrings> 

JavaEE精讲-MyBatis框架实战-SQL复习

文章插图
JavaEE精讲-MyBatis框架实战-SQL复习

文章插图
1 using System; 2 using System.Collections.Generic; 3 using System.Configuration; 4 using System.Data; 5 using System.Data.SqlClient; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 10 namespace ConsoleApplication111 {12 class SqlHelper13{14//ADO.net中的SqlHelper15 16 public static readonly string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;17 18public static SqlConnection CreateConnection()19{20SqlConnection conn = new SqlConnection(connStr);21conn.Open();22return conn;23}24 25public static int ExecuteNonQuery(SqlConnection conn, string sql, params SqlParameter[] parameters)26{27using (SqlCommand cmd = conn.CreateCommand())28{29cmd.CommandText = sql;30cmd.Parameters.AddRange(parameters);31return cmd.ExecuteNonQuery();32}33}34 35public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)36{37using (SqlConnection conn = CreateConnection())38{39return ExecuteNonQuery(conn, sql, parameters);40}41}42 43public static object ExecuteScalar(SqlConnection conn, string sql, params SqlParameter[] parameters)44{45using (SqlCommand cmd = conn.CreateCommand())46{47cmd.CommandText = sql;48cmd.Parameters.AddRange(parameters);49return cmd.ExecuteScalar();50}51}52 53public static object ExecuteScalar(string sql, params SqlParameter[] parameters)54{55using (SqlConnection conn = CreateConnection())56{57return ExecuteScalar(conn, sql, parameters);58}59}60 61public static DataTable ExecuteQuery(SqlConnection conn, string sql, params SqlParameter[] parameters)62{63DataTable table = new DataTable();64using (SqlCommand cmd = conn.CreateCommand())65{66cmd.CommandText = sql;67cmd.Parameters.AddRange(parameters);68using (SqlDataReader reader = cmd.ExecuteReader())69{70table.Load(reader);71return table;72}73}74 75}76 77public static DataTable ExecuteQuery(string sql, params SqlParameter[] parameters)78{79using (SqlConnection conn = CreateConnection())80{81return ExecuteQuery(conn, sql, parameters);82}83}84}85 }SqlHelper【JavaEE精讲-MyBatis框架实战-SQL复习】 
复习SqlHelper
标签:ble   turn   log   技术分享   closed   reader   serve   utf8   public