• SQLHelper


     1 namespace WpfApplication2
     2 {
     3    public class SqlHelper
     4     {
     5       private static string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
     6 
     7         public static int ExecuteNoQuery(string sql)
     8         { 
     9             using(SqlConnection conn=new SqlConnection (connStr))
    10             {
    11                 conn.Open();
    12                 using(SqlCommand cmd=conn.CreateCommand())
    13                 {
    14                     cmd.CommandText = sql;
    15                     return cmd.ExecuteNonQuery();
    16                 }
    17             }
    18         }
    19         public static object ExecuteScalar(string sql)
    20         { 
    21             using(SqlConnection conn=new SqlConnection (connStr))
    22             {
    23                 conn.Open();
    24                 using(SqlCommand cmd=conn.CreateCommand())
    25                 {
    26                     cmd.CommandText = sql;
    27                     return cmd.ExecuteScalar();
    28                 }
    29             }
    30         }
    31        //用来执行查询结果较少的sql
    32         public static DataSet ExecuteDataSet(string sql)
    33         { 
    34             using(SqlConnection conn=new SqlConnection (connStr))
    35             {
    36                 conn.Open();
    37                 using(SqlCommand cmd=conn.CreateCommand())
    38                 {
    39                     cmd.CommandText = sql;
    40                     SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    41                     DataSet dataset = new DataSet();
    42                     adapter.Fill(dataset);
    43                     return dataset;
    44                 }
    45             }
    46         }
    47     }
    48 }
    View Code
     1     private void button2_Click(object sender, RoutedEventArgs e)
     2         {
     3             //int a = SqlHelper.ExecuteNoQuery("insert into Table_1 values(5,'bbb',66,'2013-08-08','2013-09-09')");
     4             //int a=(int)SqlHelper.ExecuteScalar("select count(*) from Table_1");
     5             //MessageBox.Show(a.ToString());
     6             DataSet ds = SqlHelper.ExecuteDataSet("select waterwork from Table_1 where id=1 ");
     7             foreach (DataRow row in ds.Tables[0].Rows)
     8             {
     9                 string wn = (string)row["waterwork"];
    10                 MessageBox.Show(wn);
    11             }
    12         }
    View Code
  • 相关阅读:
    Unreal中的python开发
    2022.5.16
    2022.5.18
    2022.5.4
    2022.5.2
    2022.5.6
    2022.5.14
    2022.4.30
    2022.5.10
    2022.5.8
  • 原文地址:https://www.cnblogs.com/chuizhuizhigan/p/3299223.html
Copyright © 2020-2023  润新知