WPF中SqlHelper

 

  
  
  
  
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. using System.Configuration; 
  6. using System.Data; 
  7. using System.Data.SqlClient; 
  8. namespace 数据绑定 
  9.     class SqlHelper 
  10.     { 
  11.         private static string strconn= ConfigurationManager.ConnectionStrings["dbstrConn"].ConnectionString; 
  12.         public static int ExecuteNonQuery(string sql) 
  13.         { 
  14.             using (SqlConnection conn = new SqlConnection(strconn)) 
  15.             { 
  16.                 conn.Open(); 
  17.                 using (SqlCommand cmd = conn.CreateCommand()) 
  18.                 { 
  19.                     cmd.CommandText = sql; 
  20.                     return cmd.ExecuteNonQuery(); 
  21.                 } 
  22.                  
  23.             } 
  24.         } 
  25.         public static object ExecuteScalar(string sql) 
  26.         { 
  27.             using (SqlConnection conn = new SqlConnection(strconn)) 
  28.             { 
  29.                 conn.Open(); 
  30.                 using (SqlCommand cmd = conn.CreateCommand()) 
  31.                 { 
  32.                     cmd.CommandText = sql; 
  33.                     return cmd.ExecuteScalar(); 
  34.                 } 
  35.  
  36.             } 
  37.         } 
  38.         //查询少量数据 
  39.         public static DataTable ExecuteTable(string sql) 
  40.         { 
  41.             using (SqlConnection conn = new SqlConnection(strconn)) 
  42.             { 
  43.                 conn.Open(); 
  44.                 using (SqlCommand cmd = conn.CreateCommand()) 
  45.                 { 
  46.                     cmd.CommandText = sql; 
  47.                     SqlDataAdapter adapter = new SqlDataAdapter(cmd); 
  48.                     DataSet dataset = new DataSet(); 
  49.                     adapter.Fill(dataset); 
  50.                     return dataset.Tables[0];    
  51.                 } 
  52.  
  53.             } 
  54.         } 
  55.     } 

你可能感兴趣的:(SQLHelper)