//***********************************************************
//*名稱
//*功能
//*說明
//*作者
//*BOLG
//***********************************************************
using System;
using System
using System
using System
class SqlReader
{
const string connStr =
SqlConnection conn;
public SqlReader()
{
conn = new SqlConnection(connStr);
}
//**************************************
//*演示DataReader的兩種取值方法
//**************************************
public void basicReader()
{
string sql=
SqlCommand cmd;
cmd = conn
cmd
conn
SqlDataReader reader = cmd
while(reader
{
Console
}
showSplit();
reader
conn
}
//**************************************
//*演示帶參數查詢的操作
//**************************************
public void hasParamReader()
{
SqlCommand cmd;
cmd = conn
string sql =
cmd
SqlParameter param = new SqlParameter(
param
cmd
conn
//當關閉reader的時候同時關閉數據庫連接
SqlDataReader reader = cmd
while(reader
{
Console
}
showSplit();
//無需關閉conn
reader
}
//**************************************
//*演示帶參數查詢的操作
//**************************************
public void hasOledbParamReader()
{
SqlCommand cmd;
cmd = conn
string sql =
string oledbConnStr =
OleDbConnection oleConn = new OleDbConnection(oledbConnStr);
OleDbCommand oleCmd = new OleDbCommand(sql
oleCmd
oleConn
OleDbDataReader oleReader = oleCmd
while(oleReader
{
Console
}
showSplit();
oleReader
oleConn
}
//**************************************
//*演示存儲過程的輸出參數
//**************************************
public void outParamShow()
{
SqlCommand cmd;
cmd = conn
cmd
cmd
SqlParameter param = cmd
param = cmd
param
param = cmd
param
conn
cmd
string Fname = cmd
string Fphone = cmd
Console
conn
showSplit();
}
//**************************************
//*演示讀取多個無關記錄集
//**************************************
public void multiResult()
{
SqlCommand cmd;
cmd = conn
string sqla =
string sqlb =
cmd
conn
SqlDataReader reader= cmd
int i =
do
{
Console
while(reader
{
Console
}
i++;
}while(reader
reader
conn
showSplit();
}
//**************************************
//*使用DataReader獲得數據庫模式信息
//**************************************
public void getSchema()
{
SqlCommand cmd;
cmd = conn
string sql =
cmd
conn
SqlDataReader reader = cmd
DataTable SchemaTable = reader
DataRowCollection SchemaColumns = SchemaTable
DataColumnCollection SchemaProps = SchemaTable
foreach(DataRow SchemaColumn in SchemaColumns)
{
foreach(DataColumn SchemaColumnProp in SchemaProps)
{
Console
}
}
reader
conn
showSplit();
}
//**************************************
//*從數據庫讀取二進制數據的代碼段
//*該代碼段只是讀取二進制的片斷
//*整個程序
//*集成到你的WinForm項目裡面
//**************************************
public void getBinary()
{
/*
System
System
int BufferSize =
byte[] Buffer = new Byte[BufferSize];
long Offset =
long BytesRead =
do
{
BytesRead = reader
writer
writer
Offset += BytesRead;
}
while(BytesRead == BufferSize);
*/
}
//添加輸出分隔
private void showSplit()
{
Console
}
public static void Main(string [] args)
{
SqlReader sqlreader = new SqlReader();
sqlreader
sqlreader
sqlreader
sqlreader
sqlreader
sqlreader
}
}
From:http://tw.wingwit.com/Article/program/net/201311/11352.html