CLR存储过程Word文档格式.docx
- 文档编号:20179160
- 上传时间:2023-01-17
- 格式:DOCX
- 页数:13
- 大小:19.70KB
CLR存储过程Word文档格式.docx
《CLR存储过程Word文档格式.docx》由会员分享,可在线阅读,更多相关《CLR存储过程Word文档格式.docx(13页珍藏版)》请在冰豆网上搜索。
attributetorepresentanoutputparameter,asinthefollowing:
CopyCode
ImportsSystem.Runtime.InteropServices
…
PublicSharedSubPriceSum(<
ByRefvalueAsSqlInt32)
Thefollowingshowsastoredprocedurereturninginformationthroughanoutputparameter:
C#
usingSystem;
usingSystem.Data.SqlTypes;
usingSystem.Data.SqlClient;
usingMicrosoft.SqlServer.Server;
publicclassStoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
publicstaticvoidPriceSum(outSqlInt32value)
{
using(SqlConnectionconnection=newSqlConnection("
contextconnection=true"
))
value=0;
connection.Open();
SqlCommandcommand=newSqlCommand("
SELECTPriceFROMProducts"
connection);
SqlDataReaderreader=command.ExecuteReader();
using(reader)
while(reader.Read())
value+=reader.GetSqlInt32(0);
}
}
}
VisualBasic
ImportsSystem
ImportsSystem.Data
ImportsSystem.Data.Sql
ImportsSystem.Data.SqlTypes
ImportsMicrosoft.SqlServer.Server
ImportsSystem.Data.SqlClient
'
ThePartialmodifierisonlyrequiredononeclassdefinitionperproject.
PartialPublicClassStoredProcedures
'
<
summary>
Executesaqueryanditeratesovertheresultstoperformasummation.
/summary>
Microsoft.SqlServer.Server.SqlProcedure>
_
PublicSharedSubPriceSum(<
UsingconnectionAsNewSqlConnection("
)
value=0
Connection.Open()
DimcommandAsNewSqlCommand("
connection)
DimreaderAsSqlDataReader
reader=command.ExecuteReader()
Usingreader
Whilereader.Read()
value+=reader.GetSqlInt32(0)
EndWhile
EndUsing
EndUsing
EndSub
EndClass
ReturningTabularResultsandMessages
ReturningtabularresultsandmessagestotheclientisdonethroughtheSqlPipeobject,whichisobtainedbyusingthePipepropertyoftheSqlContextclass.TheSqlPipeobjecthasaSendmethod.BycallingtheSendmethod,youcantransmitdatathroughthepipetothecallingapplication.
TheseareseveraloverloadsoftheSqlPipe.Sendmethod,includingonethatsendsaSqlDataReaderandanotherthatsimplysendsatextstring.
ReturningMessages
UseSqlPipe.Send(string)tosendmessagestotheclientapplication.Thetextofthemessageislimitedto8000characters.Ifthemessageexceeds8000characters,itwillbetruncated.
ReturningTabularResults
Tosendtheresultsofaquerydirectlytotheclient,useoneoftheoverloadsoftheExecutemethodontheSqlPipeobject.Thisisthemostefficientwaytoreturnresultstotheclient,sincethedataistransferredtothenetworkbufferswithoutbeingcopiedintomanagedmemory.Forexample:
[C#]
usingSystem.Data;
///<
///Executeacommandandsendtheresultstotheclientdirectly.
publicstaticvoidExecuteToClient()
select@@version"
SqlContext.Pipe.ExecuteAndSend(command);
[VisualBasic]
Executeacommandandsendtheresultstotheclientdirectly.
PublicSharedSubExecuteToClient()
connection.Open()
SELECT@@VERSION"
SqlContext.Pipe.ExecuteAndSend(command)
Tosendtheresultsofaquerythatwasexecutedpreviouslythroughthein-processprovider(ortopre-processthedatausingacustomimplementationofSqlDataReader),usetheoverloadoftheSendmethodthattakesaSqlDataReader.Thismethodisslightlyslowerthanthedirectmethoddescribedpreviously,butitoffersgreaterflexibilitytomanipulatethedatabeforeitissenttotheclient.
///Executeacommandandsendtheresultigreadertotheclient
publicstaticvoidSendReaderToClient()
SqlDataReaderr=command.ExecuteReader();
SqlContext.Pipe.Send(r);
PublicSharedSubSendReaderToClient()
SqlContext.Pipe.Send(reader)
Tocreateadynamicresultset,populateitandsendittotheclient,youcancreaterecordsfromthecurrentconnectionandsendthemusingSqlPipe.Send.
///Createaresultsetontheflyandsendittotheclient.
publicstaticvoidSendTransientResultSet()
//Createarecordobjectthatrepresentsanindividualrow,includingit'
smetadata.
SqlDataRecordrecord=newSqlDataRecord(newSqlMetaData("
stringcol"
SqlDbType.NVarChar,128));
//Populatetherecord.
record.SetSqlString(0,"
HelloWorld!
"
);
//Sendtherecordtotheclient.
SqlContext.Pipe.Send(record);
Createaresultsetontheflyandsendittotheclient.
PublicSharedSubSendTransientResultSet()
Createarecordobjectthatrepresentsanindividualrow,includingit'
DimrecordAsNewSqlDataRecord(NewSqlMetaData("
SqlDbType.NVarChar,128))
Populatetherecord.
Sendtherecordtotheclient.
SqlContext.Pipe.Send(record)
EndClass
Hereisanexampleofsendingatabul
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CLR 存储 过程