• 带参数的SqlHelper


     1 namespace WpfApplication3
     2 {
     3     class 简化SqlHelper
     4     {
     5         private static string ConnStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
     6 
     7         public static int ExecuteNonQuery(string sql, params SqlParameter[] parameter)
     8         { 
     9             using(SqlConnection conn=new SqlConnection (ConnStr))
    10             {
    11                 conn.Open();
    12                 using(SqlCommand cmd=conn.CreateCommand())
    13                 {
    14                     cmd.CommandText = sql;
    15                     cmd.Parameters.AddRange(parameter);
    16                     return cmd.ExecuteNonQuery();
    17                 }
    18             }
    19         }
    20         public static object ExecuteScalar(string sql, params SqlParameter[] parameter)
    21         { 
    22             using(SqlConnection conn=new SqlConnection (ConnStr))
    23             {
    24                 conn.Open();
    25                 using(SqlCommand cmd=conn.CreateCommand())
    26                 {
    27                     cmd.CommandText = sql;
    28                     cmd.Parameters.AddRange(parameter);
    29                     return cmd.ExecuteScalar();
    30                 }
    31             }
    32         }
    33         public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameter)
    34         { 
    35             using(SqlConnection conn=new SqlConnection (ConnStr))
    36             {
    37                 conn.Open();
    38                 using(SqlCommand cmd=conn.CreateCommand())
    39                 {
    40                     cmd.CommandText = sql;
    41                     cmd.Parameters.AddRange(parameter);
    42                     SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    43                     DataSet dataset = new DataSet();
    44                     adapter.Fill(dataset);
    45                     return dataset.Tables[0];
    46                 }
    47             }
    48         }
    49     }
    50 }
    View Code
    1        private void button1_Click(object sender, RoutedEventArgs e)
    2         {
    3            int a=(int) 简化SqlHelper.ExecuteScalar("select count(*) from Table_1 where othersnum=@num ",new SqlParameter("@num",5));
    4             MessageBox.Show(a.ToString());
    5         }
    View Code
  • 相关阅读:
    从代码书写理解指针,很重要
    指针参数 引用参数
    [转载]c/c++ 操作sqlite
    [转载]C#.NET DataTable扩展 ToList/Get/OrderBy/Where/ToPage
    [转载]宏定义
    【转载】scanf,sscanf高级用法
    C开发机顶盒实战应用代码片段
    指针精华口诀,短小精悍
    C# linq泛型 字典集合 排序
    c开发机顶盒实战应用代码片段2
  • 原文地址:https://www.cnblogs.com/chuizhuizhigan/p/3301049.html
Copyright © 2020-2023  润新知