zoukankan      html  css  js  c++  java
  • ADO.NET基础例子

    参考:http://www.cnblogs.com/liuhaorain/archive/2012/02/06/2340409.html

    增,删,改:

     1         public void InsertData(int x, int y)
     2         {
     3             #region using 
     4             string connStr = ConfigurationManager.ConnectionStrings["connStr"].ToString();
     5             using (SqlConnection conn = new SqlConnection(connStr))
     6             {
     7                 string sqlStr = "insert into CalculatorService(num1,num2) values(@x,@y)";
     8 
     9                 SqlCommand cmd = new SqlCommand(sqlStr, conn);
    10 
    11                 SqlParameter[] paras = new SqlParameter[]{
    12                     new SqlParameter("@x",SqlDbType.Int),
    13                     new SqlParameter("@y",SqlDbType.Int)
    14                 };
    15                 paras[0].Value = x;
    16                 paras[1].Value = y;
    17                 foreach (var i in paras)
    18                 {
    19                     cmd.Parameters.Add(i);
    20                 }
    21 
    22                 try
    23                 {
    24                     conn.Open();
    25                     cmd.ExecuteNonQuery();
    26                 }
    27                 catch (Exception ex)
    28                 {
    29                     //
    30                 }
    31             }
    32             #endregion
    33         }

    查:

     1  public List<int> SearchId(int x, int y)
     2         {
     3             string connStr = ConfigurationManager.ConnectionStrings["connStr"].ToString();
     4             using (SqlConnection conn = new SqlConnection(connStr))
     5             {
     6                 string sqlStr = "select * from CalculatorService where Num1=@x and Num2=@y";
     7 
     8                 SqlCommand cmd = new SqlCommand(sqlStr, conn);
     9 
    10                 SqlParameter[] paras = new SqlParameter[]{
    11                     new SqlParameter("@x",SqlDbType.Int),
    12                     new SqlParameter("@y",SqlDbType.Int)
    13                 };
    14                 paras[0].Value = x;
    15                 paras[1].Value = y;
    16                 foreach (var i in paras)
    17                 {
    18                     cmd.Parameters.Add(i);
    19                 }
    20                 List<int> l = new List<int>();
    21                 try
    22                 {
    23                     conn.Open();
    24                     SqlDataReader reader = cmd.ExecuteReader();
    25                     
    26                     if (reader != null && reader.HasRows) {
    27                         while (reader.Read()) {
    28                             for (int i = 0; i < reader.FieldCount; i++) {
    29                                 if (reader.GetName(i) == "id") {
    30                                     l.Add(Convert.ToInt32(reader.GetValue(i)));
    31                                 }
    32                             }
    33                         }
    34                     }
    35                     reader.Close();
    36                     return l;
    37                 }
    38                 catch (Exception ex)
    39                 {
    40                     return l;
    41                 }
    42             }
    43 
    44         }
  • 相关阅读:
    单列模式
    经济数据价格走势图(包括纸黄金),可以查看历史
    UVA10010的类似BFS做法
    转:数据结构专项之Hash函数
    ZOJ1709 DFS和BFS两种搜索方法
    HDU1969(二分搜索)
    HDU1045 回溯
    HDU2899(三分搜索)
    安神口中的水题
    HDU2199(二分搜索无限逼近)
  • 原文地址:https://www.cnblogs.com/cat66/p/7872462.html
Copyright © 2011-2022 走看看