- 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);