比如VB.NET數(shù)據(jù)庫(kù)壓縮的實(shí)現(xiàn)方法,VB.NET操作MySql數(shù)據(jù)庫(kù)的具體操作技巧等等。那么今天大家將會(huì)了解到VB.NET調(diào)用SQL Server存儲(chǔ)過程的相關(guān)應(yīng)用方法。 定義數(shù)據(jù)鏈接部分省略, myConn為鏈接對(duì)象
ProcName為存儲(chǔ)過程名 1. Private Function SqlProc1(ByVal ProcName As String) As Boolean 2. '定義數(shù)據(jù)鏈接部分省略, myConn為鏈接對(duì)象 ProcName為存儲(chǔ)過程名 3. Dim myCommand As New SqlClient.SqlCommand(ProcName, myConn) 4. With myCommand 5. .CommandType = CommandType.StoredProcedure 6. .Parameters.Add("@CodeType", SqlDbType.VarChar, 20).Value = "年級(jí)編碼" 7. Try 8. .ExecuteNonQuery() 9. Return True 10. Catch ex As Exception 11. Return False 12. End Try 13. End Function 2.VB.NET調(diào)用SQL Server存儲(chǔ)過程返回普通值 Private Function SqlProc1(ByVal ProcName As String) As String '定義數(shù)據(jù)鏈接部分省略, myConn為鏈接對(duì)象 Dim myCommand As New SqlClient.SqlCommand(ProcName, myConn) With myCommand .CommandType = CommandType.StoredProcedure .Parameters.Add("@CodeType", SqlDbType.VarChar, 20).Value = "年級(jí)編碼" .Parameters.Add("@NewCode", SqlDbType.VarChar, 20). Try .ExecuteNonQuery() Return .Parameters(1).Value() Catch ex As Exception Return "無編碼生成" End Try End Function 3.VB.NET調(diào)用SQL Server存儲(chǔ)過程返回?cái)?shù)據(jù)集 1. 'VB.NET代碼 2.
Private Function SqlProc2(ByVal ProcName As String, 3. '定義命令對(duì)象,并使用儲(chǔ)存過程 4. Dim myCommand As New SqlClient.SqlCommand 5. myCommand.CommandType = CommandType.StoredProcedure 6. myCommand.CommandText = ProcName 7. myCommand.Connection = myConn 8. '定義一個(gè)數(shù)據(jù)適配器,并設(shè)置參數(shù) 9. Dim myDapter As New SqlClient.SqlDataAdapter(myCommand) 10. myDapter.SelectCommand.Parameters.Add 11. '定義一個(gè)數(shù)據(jù)集對(duì)象,并填充數(shù)據(jù)集 12. Dim myDataSet As New DataSet 13. Try 14. myDapter.Fill(myDataSet) 15. Catch ex As Exception 16. End Try 17. Return myDataSet 18. End Function 存儲(chǔ)過程代碼 1. Create Proc Test @name varchar(20) As 2. Select * From EC_Grade where cGradeName=@name 3. GO 4. ***如果將存儲(chǔ)過程修改部分內(nèi)容,可以做為查詢使用 5. CREATE Proc Test 6. @name varchar(200)='' 7.
--此處應(yīng)該注意200為查詢條件的長(zhǎng)度,可以根據(jù)實(shí)際情況而定; 8. As 9. Declare @sql1 varchar(8000) 10. if @name<>'' 11. Select @sql1='Select * From EC_Grade where '+ @name 12. else 13. Select @sql1='Select * From EC_Grade' 14. exec(@sql1) 15. GO VB.NET調(diào)用SQL Server存儲(chǔ)過程的相關(guān)應(yīng)用方法就為大家介紹到這里。
|
|