Vb.net 从global.asax文件调用存储过程
您好,我已经开发了以下代码来将数据集保存在Vb.net 从global.asax文件调用存储过程,vb.net,stored-procedures,global-asax,Vb.net,Stored Procedures,Global Asax,您好,我已经开发了以下代码来将数据集保存在Global.asax文件中。要在webpage.aspx.vb文件中使用它,现在它工作正常 以类似的方式,如何调用Global.asax中的存储过程? 请帮忙,提前谢谢 Shared _cache As Cache = Nothing Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) _cache = Context.Cache RefreshCac
Global.asax
文件中。要在webpage.aspx.vb
文件中使用它,现在它工作正常
以类似的方式,如何调用Global.asax
中的存储过程?请帮忙,提前谢谢
Shared _cache As Cache = Nothing
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
_cache = Context.Cache
RefreshCache(Nothing, Nothing, 0)
End Sub
Private Shared Sub RefreshCache(ByVal key As String, ByVal item As Object, ByVal reason As CacheItemRemovedReason)
Dim adapter As New SqlDataAdapter("SELECT * FROM dbo.table where logid = 1", "server=server;database=database;uid=userid;pwd=password")
Dim ds As New DataSet()
adapter.Fill(ds, "Quotations")
Dim onRemove As CacheItemRemovedCallback
onRemove = New CacheItemRemovedCallback(AddressOf RefreshCache)
_cache.Insert("Quotes", ds, New CacheDependency("C:\AspNetSql\Quotes.Quotations"), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.[Default], onRemove)
End Sub
得到了以下代码的解决方案
<%@ Application Language="VB" %>
<%@ Import Namespace="System.DirectoryServices.AccountManagement" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Common" %>
<%@ Import Namespace="System.Runtime.Remoting.Contexts" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web.Caching" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Web.Caching.Cache" %>
<script RunAt="server">
Shared _cache As Cache = Nothing
Protected Shared db As Database
Private cnPubs As SqlConnection
Private daPubs As SqlDataAdapter
Private cmdSelPubInfo As SqlCommand
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
_cache = Context.Cache
RefreshCache(Nothing, Nothing, 0)
End Sub
Public Sub New(ByVal databaseName As String)
db = New Database(databaseName)
End Sub
Private Sub RefreshCache(ByVal key As String, ByVal item As Object, ByVal reason As CacheItemRemovedReason)
cnPubs = New SqlConnection("server=Servername;database=databasename;uid=userid;pwd=password")
'select command
cmdSelPubInfo = New SqlCommand
cmdSelPubInfo.Connection = cnPubs
cmdSelPubInfo.CommandType = CommandType.StoredProcedure
cmdSelPubInfo.CommandText = "storedprocedurename"
cmdSelPubInfo.Parameters.Add(New SqlParameter("@pStartDate", "01/01/2011"))
cmdSelPubInfo.Parameters.Add(New SqlParameter("@pEndDate", "01/01/2012"))
cmdSelPubInfo.Parameters.Add(New SqlParameter("@pErrorMessage", ""))
'DataApapter
daPubs = New SqlDataAdapter
daPubs.SelectCommand = cmdSelPubInfo
Dim dsPubs = New DataSet
'Dim ds As New DataSet()
daPubs.Fill(dsPubs)
Dim onRemove As CacheItemRemovedCallback
onRemove = New CacheItemRemovedCallback(AddressOf RefreshCache)
'' Add the DataSet to the application cache.
_cache.Insert("Quotes", dsPubs, New CacheDependency("C:\AspNetSql\Quotes.Quotations"), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.[Default], onRemove)
End Sub
</script>
共享_缓存为缓存=无
受保护的共享数据库作为数据库
专用cnPubs作为SqlConnection
专用daPubs作为SqlDataAdapter
私有cmdSelPubInfo作为SqlCommand
子应用程序_启动(ByVal发送方作为对象,ByVal e作为事件参数)
_cache=Context.cache
刷新缓存(无,无,0)
端接头
Public Sub New(ByVal databaseName作为字符串)
db=新数据库(数据库名称)
端接头
私有子刷新缓存(ByVal键作为字符串,ByVal项作为对象,ByVal原因作为CacheItemRemovedReason)
cnPubs=newsqlconnection(“server=Servername;database=databasename;uid=userid;pwd=password”)
'选择命令
cmdSelPubInfo=New-SqlCommand
cmdSelPubInfo.Connection=cnPubs
cmdSelPubInfo.CommandType=CommandType.StoredProcess
cmdSelPubInfo.CommandText=“storedprocedurename”
cmdSelPubInfo.Parameters.Add(新的SqlParameter(“@pStartDate”,“01/01/2011”))
cmdSelPubInfo.Parameters.Add(新的SqlParameter(“@pEndDate”,“01/01/2012”))
cmdSelPubInfo.Parameters.Add(新的SqlParameter(“@perroMessage”,”))
“DataApapter
daPubs=新的SqlDataAdapter
daPubs.SelectCommand=cmdSelPubInfo
Dim dsPubs=新数据集
'Dim ds作为新数据集()
daPubs.Fill(dsPubs)
Dim onRemove作为CacheItemRemovedCallback
onRemove=New CacheItemRemovedCallback(刷新缓存的地址)
“”将数据集添加到应用程序缓存。
_cache.Insert(“Quotes”、dspub、新CacheDependency(“C:\AspNetSql\Quotes.Quotes”)、cache.NoAbsoluteExpiration、cache.NoSlidingExpiration、cacheitemriority。[默认值],onRemove)
端接头
调用它的方式与在任何代码隐藏文件中调用它的方式相同。你到底有什么问题?我昨天就回答了。。。我理解你们对我说的话,但我的global.asax文件包含vb.net代码,它不是在一个类中编写的,以继承任何其他基类,无论如何,谢谢你们,我已经用你们的建议解决了这个问题。以前,我想得太多,无法实现下面的答案代码,现在我明白了。