- SqlConnection conn = new SqlConnection(@"data source=20110105-1517;initial catalog=消防預警系統;User ID=sa;password=28");
-
- 數據的更新替換 update Table_1 set 姓名='張曉玲' where 姓名='牛羊'
- 多行替換update Table_1 set 姓名='牛永傑' ,年齡='100' where 姓名='牛永斌' and 工資=100000
- 排序 select 年齡 from Table_1 order by 年齡 desc
- Begin end 結合使用
-
- 不少的SQL語句中要使用end結束(when @str=90 then '你的成績是優秀'
- end
- print @str1)
- using System;
- using System.Data;
- using System.Data.SqlClient;
- namespace ConsoleApplication3
- {
- class Program
- {
- static void Main(string[] args)
- {
- SqlConnection conn = new SqlConnection(@"server=.;integrated security=true;database=db_business");
-
- string sql = @"select count (*) from 職工 ";
- SqlCommand cmd = new SqlCommand(sql,conn);
-
- try
- {
- conn.Open();
- Console.WriteLine("打開成功");
-
- Console.WriteLine("結果:{0}",cmd.ExecuteScalar());
- }
- catch (SqlException e)
- {
- Console.WriteLine("錯誤是:" + e);
- }
- finally
- {
- conn.Close();
- Console.WriteLine("鏈接關閉!");
- }
- Console.Read();
-
- }
- }
- }
-
- int[] arr = { 1, 2, 3,58,110 };
- foreach (int i in arr)
- {
- System.Console.WriteLine(i);
- }
-
- 數據集,數據適配器都是構造函數重載的,
- string sql = @"select * from Table_1 ";
- SqlDataAdapter da = new SqlDataAdapter (sql,conn);
- DataSet ds = new DataSet();
- da.Fill(ds,"Table_1");
- DataTable dt = ds.Tables["Table_1"];
- foreach(DataRow row in dt.Rows)
- {
- foreach(DataColumn col in dt.Columns)
- Console.WriteLine(row[col]);
- Console.WriteLine("".PadLeft(20,'='));
-
- }
-
- 獲取dataGridView1控件中的值
- for (int i = 0; i < dataGridView1.Columns.Count;i++ )
- {
- string str = "";
- if (i == 0)
- {
- str += dataGridView1.Rows[e.RowIndex].Cells[i].Value.ToString();
- textBox1.Text = str;
- }
- else
- {
- str += dataGridView1.Rows[e.RowIndex].Cells[i].Value.ToString();
- textBox2.Text = str;
- }
- }
-
-
- SqlCommandBuilder sb = new SqlCommandBuilder(da);
- DataRow row = ds.Tables[0].NewRow();
- row["ID"] = "D7";
- row["AreaName"] = "測a試º?區?3";
- ds.Tables[0].Rows.Add(row);
- da.Update(ds);
- dataGridView1.DataSource = ds.Tables[0];
- conn.Close ();
-
- SqlCommandBuilder sqlcb = new SqlCommandBuilder(da);
- for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
- {
- if(ds.Tables[0].Rows[i][1].ToString()=="測a試º?區?2")
- {
- ds.Tables[0].Rows[i].Delete();
- }
- }
- da.Update(ds);
-
- SqlCommandBuilder sqlcb = new SqlCommandBuilder(da);
- for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
- {
- if(ds.Tables[0].Rows[i][1].ToString()=="測a試º?區?3")
- {
- ds.Tables[0].Rows[i][1]="測a試º?區?1";
- }
- }
- da.Update(ds);