DataReader的详细用法
1//DataReader的详细用法
2 cmd.CommandText = "GetInfo";
3 cmd.CommandType = CommandType.StoredProcedure;
4 SqlParameter param = cmd.Parameters.Add("@Fid",16);
5 param = cmd.Parameters.Add("@Fname",SqlDbType.VarChar,8);
6 param.Direction = ParameterDirection.Output;
7 param = cmd.Parameters.Add("@Fphone",SqlDbType.VarChar,8);
8 param.Direction = ParameterDirection.Output;
9 conn.Open();
10 cmd.ExecuteNonQuery();
11 string Fname = cmd.Parameters["@Fname"].Value.ToString();
12 string Fphone = cmd.Parameters["@Fphone"].Value.ToString();
13 Console.WriteLine(Fname + " " + Fphone);
14 conn.Close();
15 showSplit();
16 }
17
18 //**************************************
19 //*演示读取多个无关记录集
20 //**************************************
21 public void multiResult()
22 {
23 SqlCommand cmd;
24 cmd = conn.CreateCommand();
25 string sqla = "select Fname from friend";
26 string sqlb = "select Fphone from friend";
27 cmd.CommandText = sqla + ";" + sqlb;
28 conn.Open();
29 SqlDataReader reader= cmd.ExecuteReader();
30 int i = 1;
31 do
32 {
33 Console.WriteLine("第" + i.ToString() + "个记录集内容如下:\n");
34 while(reader.Read())
35 {
36 Console.WriteLine(reader[0].ToString() + "\t");
37 }
38 i++;
39 }while(reader.NextResult()); //NextResult()移动到下一个记录集
40 reader.Close();
41 conn.Close();
42 showSplit();
43 }
44
45 //**************************************
46 //*使用DataReader获得数据库模式信息
47 //**************************************
48 public void getSchema()
49 {
50 SqlCommand cmd;
51 cmd = conn.CreateCommand();
52 string sql = "select Fid,Fname,Fphone from friend";
53 cmd.CommandText = sql;
54 conn.Open();
55 SqlDataReader reader = cmd.ExecuteReader();
56 DataTable SchemaTable = reader.GetSchemaTable();
57
58 DataRowCollection SchemaColumns = SchemaTable.Rows;
59 DataColumnCollection SchemaProps = SchemaTable.Columns;
60 foreach(DataRow SchemaColumn in SchemaColumns)
61 {
62 foreach(DataColumn SchemaColumnProp in SchemaProps)
63 {
64 Console.WriteLine(SchemaColumnProp.ColumnName + "=" + SchemaColumn[SchemaColumnProp.ColumnName].ToString());
65 }
66 }
67 reader.Close();
68 conn.Close();
69 showSplit();
70 }
71
72 //**************************************
73 //*从数据库读取二进制数据的代码段
74 //*该代码段只是读取二进制的片断,不是
75 //*整个程序,所以不能执行,你可以把它
76 //*集成到你的WinForm项目里面。
77 //**************************************
78 public void getBinary()
79 {
80 /*
81 System.IO.MemoryStream stream = new System.IO.MemoryStream();
82 System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);
83 int BufferSize = 1024;
84 byte[] Buffer = new Byte[BufferSize];
85 long Offset = 0;
86 long BytesRead = 0;
87 do
88 {
89 BytesRead = reader.GetBytes(2,Offset,Buffer,0,BufferSize);
90 writer.Writer(Buffer,0,(int)BytesRead);
91 writer.Flush();
92 Offset += BytesRead;
93 }
94 while(BytesRead == BufferSize);
95 */
96 }
97
98 //添加输出分隔
99 private void showSplit()
100 {
101 Console.WriteLine("\n********************************************************************\n");
102 }
103
104 public static void Main(string [] args)
105 {
106 SqlReader sqlreader = new SqlReader();
107
108 sqlreader.basicReader();
109
110 sqlreader.hasParamReader();
111
112 sqlreader.hasOledbParamReader();
113
114 sqlreader.outParamShow();
115
116 sqlreader.multiResult();
117
118 sqlreader.getSchema();
119 }
120}
2 cmd.CommandText = "GetInfo";
3 cmd.CommandType = CommandType.StoredProcedure;
4 SqlParameter param = cmd.Parameters.Add("@Fid",16);
5 param = cmd.Parameters.Add("@Fname",SqlDbType.VarChar,8);
6 param.Direction = ParameterDirection.Output;
7 param = cmd.Parameters.Add("@Fphone",SqlDbType.VarChar,8);
8 param.Direction = ParameterDirection.Output;
9 conn.Open();
10 cmd.ExecuteNonQuery();
11 string Fname = cmd.Parameters["@Fname"].Value.ToString();
12 string Fphone = cmd.Parameters["@Fphone"].Value.ToString();
13 Console.WriteLine(Fname + " " + Fphone);
14 conn.Close();
15 showSplit();
16 }
17
18 //**************************************
19 //*演示读取多个无关记录集
20 //**************************************
21 public void multiResult()
22 {
23 SqlCommand cmd;
24 cmd = conn.CreateCommand();
25 string sqla = "select Fname from friend";
26 string sqlb = "select Fphone from friend";
27 cmd.CommandText = sqla + ";" + sqlb;
28 conn.Open();
29 SqlDataReader reader= cmd.ExecuteReader();
30 int i = 1;
31 do
32 {
33 Console.WriteLine("第" + i.ToString() + "个记录集内容如下:\n");
34 while(reader.Read())
35 {
36 Console.WriteLine(reader[0].ToString() + "\t");
37 }
38 i++;
39 }while(reader.NextResult()); //NextResult()移动到下一个记录集
40 reader.Close();
41 conn.Close();
42 showSplit();
43 }
44
45 //**************************************
46 //*使用DataReader获得数据库模式信息
47 //**************************************
48 public void getSchema()
49 {
50 SqlCommand cmd;
51 cmd = conn.CreateCommand();
52 string sql = "select Fid,Fname,Fphone from friend";
53 cmd.CommandText = sql;
54 conn.Open();
55 SqlDataReader reader = cmd.ExecuteReader();
56 DataTable SchemaTable = reader.GetSchemaTable();
57
58 DataRowCollection SchemaColumns = SchemaTable.Rows;
59 DataColumnCollection SchemaProps = SchemaTable.Columns;
60 foreach(DataRow SchemaColumn in SchemaColumns)
61 {
62 foreach(DataColumn SchemaColumnProp in SchemaProps)
63 {
64 Console.WriteLine(SchemaColumnProp.ColumnName + "=" + SchemaColumn[SchemaColumnProp.ColumnName].ToString());
65 }
66 }
67 reader.Close();
68 conn.Close();
69 showSplit();
70 }
71
72 //**************************************
73 //*从数据库读取二进制数据的代码段
74 //*该代码段只是读取二进制的片断,不是
75 //*整个程序,所以不能执行,你可以把它
76 //*集成到你的WinForm项目里面。
77 //**************************************
78 public void getBinary()
79 {
80 /*
81 System.IO.MemoryStream stream = new System.IO.MemoryStream();
82 System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);
83 int BufferSize = 1024;
84 byte[] Buffer = new Byte[BufferSize];
85 long Offset = 0;
86 long BytesRead = 0;
87 do
88 {
89 BytesRead = reader.GetBytes(2,Offset,Buffer,0,BufferSize);
90 writer.Writer(Buffer,0,(int)BytesRead);
91 writer.Flush();
92 Offset += BytesRead;
93 }
94 while(BytesRead == BufferSize);
95 */
96 }
97
98 //添加输出分隔
99 private void showSplit()
100 {
101 Console.WriteLine("\n********************************************************************\n");
102 }
103
104 public static void Main(string [] args)
105 {
106 SqlReader sqlreader = new SqlReader();
107
108 sqlreader.basicReader();
109
110 sqlreader.hasParamReader();
111
112 sqlreader.hasOledbParamReader();
113
114 sqlreader.outParamShow();
115
116 sqlreader.multiResult();
117
118 sqlreader.getSchema();
119 }
120}