Posted on 2009-08-05 16:23
Hero 阅读(172)
评论(0) 编辑 收藏 引用 所属分类:
C#积累
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 using System.Data;
6 using System.Data.SqlClient;
7 using System.IO;
8
9 namespace SQL
10 {
11 class Program
12 {
13 static void Main1( string[] args )
14 {
15 //创建一个新连接字符串
16 SqlConnection thisConnection = new SqlConnection(
17 @"Server=ta690g; Integrated Security=True; Database=AdventureWorks"
18 );
19 thisConnection.Open();
20
21 //创建SQL查询命令行
22 SqlCommand thisCommand = thisConnection.CreateCommand();
23 thisCommand.CommandText = "select * from HumanResources.Department";
24
25 //执行指定的查询
26 SqlDataReader thisReader = thisCommand.ExecuteReader();
27
28 //读出并显示数据
29 FileStream file = new FileStream( "result.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite );
30 StreamWriter wfile = new StreamWriter( file );
31
32 while ( thisReader.Read() )
33 {
34 Console.WriteLine( "{0}\t{1}", thisReader[0], thisReader[1] );
35 wfile.WriteLine( "{0}\t{1}", thisReader[0], thisReader[1] );
36 }
37
38 wfile.Close();
39 file.Close();
40
41 thisReader.Close();
42 thisConnection.Close();
43 }
44
45 static void Main2( string[] args )
46 {
47 //创建连接
48 SqlConnection thisConnection = new SqlConnection(
49 @"Server=ta690g; Integrated Security=True; Database=AdventureWorks"
50 );
51
52 //创建数据适配器
53 SqlDataAdapter thisAdapter = new SqlDataAdapter(
54 "select * from HumanResources.Department", thisConnection
55 );
56
57 //创建数据集合
58 DataSet thisDataSet = new DataSet();
59 thisAdapter.Fill( thisDataSet, "Department" );
60
61 foreach ( DataRow row in thisDataSet.Tables["Department"].Rows )
62 {
63 Console.WriteLine( row[0] + "\t" + row[1] );
64 }
65
66 thisConnection.Close();
67 }
68
69 static void Main( string[] args )
70 {
71 //创建连接
72 SqlConnection thisConnection = new SqlConnection(
73 @"Server=ta690g; Integrated Security=True; Database=AdventureWorks"
74 );
75
76 //创建DataAdapter对象 -- 进行更新操作
77 SqlDataAdapter thisAdapter = new SqlDataAdapter(
78 "select * from HumanResources.Department", thisConnection
79 ) ;
80
81 //创建SQL命令
82 SqlCommandBuilder thisBuilder = new SqlCommandBuilder( thisAdapter ) ;
83
84 //创建数据集合DataSet来包含相关的数据表,rows, columns
85 DataSet thisDataSet = new DataSet() ;
86
87 thisAdapter.Fill( thisDataSet, "Dep" ) ;
88 Console.WriteLine( thisDataSet.Tables["Dep"].Rows.Count );
89 Console.WriteLine( "name = {0}", thisDataSet.Tables["Dep"].Rows[0]["Name"] ) ;
90
91 ////更新元素
92 //thisDataSet.Tables["Dep"].Rows[0]["Name"] = "wangzhaoren";
93 //thisAdapter.Update( thisDataSet, "Dep" );
94
95 ////thisAdapter.Fill( thisDataSet, "Dep" );
96 //Console.WriteLine( "name = {0}", thisDataSet.Tables["Dep"].Rows[0]["Name"] );
97
98 ////插入新元素
99 //Console.WriteLine( thisDataSet.Tables["Dep"].Rows.Count );
100 //DataRow thisRow = thisDataSet.Tables["Dep"].NewRow();
101 //thisRow["Name"] = "lovefang";
102 //thisRow["GroupName"] = "Research and Development";
103 //thisDataSet.Tables["Dep"].Rows.Add( thisRow );
104 //thisAdapter.Update( thisDataSet, "Dep" );
105
106 //Console.WriteLine( thisDataSet.Tables["Dep"].Rows.Count );
107
108 //设置主键
109 DataColumn[] col = new DataColumn[1];
110 col[0] = thisDataSet.Tables["Dep"].Columns["DepartmentID"];
111 thisDataSet.Tables["Dep"].PrimaryKey = col;
112
113 DataRow row = thisDataSet.Tables["Dep"].Rows.Find( "17" );
114 if ( row == null )
115 {
116 Console.WriteLine( "not find" );
117 }
118 else
119 {
120 Console.WriteLine( row[1] );
121 }
122
123
124 thisConnection.Close();
125 }
126 }
127 }
128