标签:data conf comm from name ref var cut nec
参考: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 }
标签:data conf comm from name ref var cut nec
原文地址:http://www.cnblogs.com/cat66/p/7872462.html