怎么样在vs2005调用存储过程并用Datareader读出?
前些日一位兄弟问我vs2003下如何用sql2000的存储过程,并用DataReader读出苦于一时没有2003只好用2005写了一下,没想到改了一下还真是好使,而且进行了一下数据库的负载测试,效果还是不错的
DimconnAsNewSqlConnection()
conn.ConnectionString=ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
conn.Open()
DimcmdAsNewSqlCommand()
cmd.Connection=conn
cmd.CommandType=CommandType.StoredProcedure
cmd.CommandText="User"
Dimc1AsNewSqlParameter("@id",SqlDbType.BigInt)
c1.Value=Val(session)
cmd.Parameters.Add(c1)
DimreaderAsSqlDataReader
DimpreviousConnectionStateAsConnectionState=conn.State
Try
Ifconn.State=ConnectionState.ClosedThen
conn.Open()
EndIf
reader=cmd.ExecuteReader()
Usingreader
Whilereader.Read
plovelike=reader.Item("lovelike").ToString
EndWhile
EndUsing
Finally
IfpreviousConnectionState=ConnectionState.ClosedThen
conn.Close()
EndIf
EndTry
conn.Close()
conn.ConnectionString=ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
conn.Open()
DimcmdAsNewSqlCommand()
cmd.Connection=conn
cmd.CommandType=CommandType.StoredProcedure
cmd.CommandText="User"
Dimc1AsNewSqlParameter("@id",SqlDbType.BigInt)
c1.Value=Val(session)
cmd.Parameters.Add(c1)
DimreaderAsSqlDataReader
DimpreviousConnectionStateAsConnectionState=conn.State
Try
Ifconn.State=ConnectionState.ClosedThen
conn.Open()
EndIf
reader=cmd.ExecuteReader()
Usingreader
Whilereader.Read
plovelike=reader.Item("lovelike").ToString
EndWhile
EndUsing
Finally
IfpreviousConnectionState=ConnectionState.ClosedThen
conn.Close()
EndIf
EndTry
conn.Close()
本文地址:http://www.45fan.com/dnjc/70354.html