C#連接數據庫

  1.  SqlConnection conn = new SqlConnection(@"data source=20110105-1517;initial catalog=消防預警系統;User ID=sa;password=28"); 
  2.  
  3. 數據的更新替換 update Table_1 set 姓名='張曉玲' where 姓名='牛羊' 
  4. 多行替換update Table_1  set 姓名='牛永傑' ,年齡='100' where 姓名='牛永斌' and 工資=100000 
  5. 排序  select 年齡 from Table_1 order by  年齡 desc 
  6. Begin end 結合使用 
  7.  
  8. 很多的SQL語句中要使用end結束(when @str=90 then '你的成績是優秀' 
  9. end 
  10. print  @str1) 
  11. using System; 
  12. using System.Data; 
  13. using System.Data.SqlClient; 
  14. namespace ConsoleApplication3 
  15.     class Program 
  16.     { 
  17.         static void Main(string[] args) 
  18.         { 
  19.             SqlConnection conn = new SqlConnection(@"server=.;integrated security=true;database=db_business"); 
  20.             //SqlConnection conn = new SqlConnection(@"server=.\db_business;integrated security=true;"); 
  21.             string sql = @"select count (*) from  職工 "
  22.             SqlCommand cmd = new SqlCommand(sql,conn); 
  23.           
  24.             try 
  25.             { 
  26.                 conn.Open(); 
  27.                 Console.WriteLine("打開成功"); 
  28.  
  29.                 Console.WriteLine("結果:{0}",cmd.ExecuteScalar()); 
  30.             } 
  31.             catch (SqlException e) 
  32.             { 
  33.                 Console.WriteLine("錯誤是:" + e); 
  34.             } 
  35.             finally 
  36.             { 
  37.                 conn.Close(); 
  38.                 Console.WriteLine("連接關閉!"); 
  39.             } 
  40.             Console.Read(); 
  41.  
  42.         } 
  43.     } 
  44.  
  45.             int[] arr = { 1, 2, 3,58,110 };  
  46.             foreach (int i in arr)  
  47.             {  
  48.                 System.Console.WriteLine(i); 
  49.             } 
  50.  
  51. 數據集,數據適配器都是構造函數重載的, 
  52.     string sql = @"select * from  Table_1 "
  53.                 SqlDataAdapter da = new SqlDataAdapter (sql,conn); 
  54.                 DataSet ds = new DataSet(); 
  55.                 da.Fill(ds,"Table_1"); 
  56.                 DataTable dt = ds.Tables["Table_1"]; 
  57.                 foreach(DataRow row in dt.Rows) 
  58.                 { 
  59.                     foreach(DataColumn col in dt.Columns) 
  60.                         Console.WriteLine(row[col]); 
  61.                         Console.WriteLine("".PadLeft(20,'=')); 
  62.  
  63.                 } 
  64.      
  65. 獲取dataGridView1控件中的值 
  66.      for (int i = 0; i < dataGridView1.Columns.Count;i++ ) 
  67.             { 
  68.                 string str = ""
  69.                 if (i == 0) 
  70.                 { 
  71.                     str += dataGridView1.Rows[e.RowIndex].Cells[i].Value.ToString(); 
  72.                     textBox1.Text = str; 
  73.                 } 
  74.                 else 
  75.                 { 
  76.                     str += dataGridView1.Rows[e.RowIndex].Cells[i].Value.ToString(); 
  77.                     textBox2.Text = str; 
  78.                 } 
  79.             } 
  80.  ///使用數據適配器操作數據集,並不是直接對數據庫的操作, 
  81. //使用sqldataadapt 實現數據的添加 
  82.             SqlCommandBuilder sb = new SqlCommandBuilder(da); 
  83.             DataRow row = ds.Tables[0].NewRow(); 
  84.             row["ID"] = "D7"
  85.             row["AreaName"] = "測a試º?區?3"
  86.             ds.Tables[0].Rows.Add(row); 
  87.             da.Update(ds); 
  88.             dataGridView1.DataSource = ds.Tables[0]; 
  89.             conn.Close (); 
  90. //使用sqldataadapt 刪除數據 
  91.           SqlCommandBuilder sqlcb = new SqlCommandBuilder(da); 
  92.             for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
  93.             {    
  94.                 if(ds.Tables[0].Rows[i][1].ToString()=="測a試º?區?2"
  95.                 { 
  96.                     ds.Tables[0].Rows[i].Delete(); 
  97.                 } 
  98.             } 
  99.             da.Update(ds); 
  100. //使用sqldataadapt 修改數據 
  101.           SqlCommandBuilder sqlcb = new SqlCommandBuilder(da); 
  102.             for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
  103.             { 
  104.                 if(ds.Tables[0].Rows[i][1].ToString()=="測a試º?區?3"
  105.                 { 
  106.                     ds.Tables[0].Rows[i][1]="測a試º?區?1"
  107.                 } 
  108.             } 
  109.             da.Update(ds); 
  110.     

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章