使用“SQL Server”模式在两个不同的asp.net解决方案之间共享会话值

时间:2023-01-16 09:53:04

I was just testing. I created a text box and a button in first asp.net application and on the button event i stored the value of text box in a session that got stored in database.

我只是在测试。我在第一个asp.net应用程序中创建了一个文本框和一个按钮,在按钮事件中,我将文本框的值存储在存储在数据库中的会话中。

Now i created another asp.net application with just a text box. Now I want to call the value of the text box in the first asp.net application.

现在我用一个文本框创建了另一个asp.net应用程序。现在我想在第一个asp.net应用程序中调用文本框的值。

How will I do it?

我该怎么办?

I have already read all the theoretical application. If someone could write down the code for it, it would be great help.

我已经阅读了所有的理论应用。如果有人可以为它写下代码,那将会很有帮助。

I have used

我用过

<mode="SQLServer"
sqlConnectionString="data source=.;integrated security=true">

in web.config.

在web.config中。

I have also successfully created the ASPState database in SQL Server 2008 R2.

我还在SQL Server 2008 R2中成功创建了ASPState数据库。

protected void Button1_Click(object sender, EventArgs e)
{
Session["mydb"] = TextBox1.Text;
}

I am storing value of textbox like this. Its getting stored in the database under table "ASPStateTempSessions" in encrypted form in column SessionId.

我正在存储这样的文本框的值。它在SessionId列中以加密形式存储在表“ASPStateTempSessions”下的数据库中。

Now I am not getting how to call this value stored in database in the text box in my second web application.

现在我没有得到如何在我的第二个Web应用程序的文本框中调用存储在数据库中的值。

I hope you understand my problem. Please help me!!

我希望你理解我的问题。请帮帮我!!

1 个解决方案

#1


1  

I have found a solution:-

我找到了一个解决方案: -

I just changed the procedure i.e.

我只是改变了程序,即

USE ASPState
GO

ALTER PROCEDURE dbo.TempGetAppID
    @appName tAppName,
    @appId int OUTPUT
AS

    -- start change

    -- Use the application name specified in the connection for the appname if specified
    -- This allows us to share session between sites just by making sure they have the
    -- the same application name in the connection string.
    DECLARE @connStrAppName nvarchar(50)
    SET @connStrAppName = APP_NAME()

    -- .NET SQLClient Data Provider is the default application name for .NET apps
    IF (@connStrAppName <> '.NET SQLClient Data Provider')
        SET @appName = @connStrAppName

    -- end change

SET @appName = LOWER(@appName)
SET @appId = NULL

SELECT @appId = AppId
FROM [ASPState].dbo.ASPStateTempApplications
WHERE AppName = @appName

IF @appId IS NULL BEGIN
BEGIN TRAN 

SELECT @appId = AppId
FROM [ASPState].dbo.ASPStateTempApplications WITH (TABLOCKX)
WHERE AppName = @appName

IF @appId IS NULL
BEGIN
EXEC GetHashCode @appName, @appId OUTPUT

INSERT [ASPState].dbo.ASPStateTempApplications
VALUES
(@appId, @appName)

IF @@ERROR = 2627 
BEGIN
DECLARE @dupApp tAppName

SELECT @dupApp = RTRIM(AppName)
FROM [ASPState].dbo.ASPStateTempApplications 
WHERE AppId = @appId

RAISERROR('SQL session state fatal error: hash-code collision between applications ''%s'' and ''%s''. Please rename the 1st application to resolve the problem.', 
18, 1, @appName, @dupApp)
END
END

COMMIT
END

RETURN 0 
GO

and the web.config:-

和web.config: -

<sessionState mode="SQLServer" sqlConnectionString="Data Source=.;Integrated Security=True;Application Name=TEST" cookieless="false" timeout="20"></sessionState>
   <httpRuntime targetFramework="4.5"/>

You have to add Application Name and that have to be the same for all the application for which you want to share the same session.

您必须添加应用程序名称,并且必须与要共享同一会话的所有应用程序相同。

Thanks.

谢谢。

#1


1  

I have found a solution:-

我找到了一个解决方案: -

I just changed the procedure i.e.

我只是改变了程序,即

USE ASPState
GO

ALTER PROCEDURE dbo.TempGetAppID
    @appName tAppName,
    @appId int OUTPUT
AS

    -- start change

    -- Use the application name specified in the connection for the appname if specified
    -- This allows us to share session between sites just by making sure they have the
    -- the same application name in the connection string.
    DECLARE @connStrAppName nvarchar(50)
    SET @connStrAppName = APP_NAME()

    -- .NET SQLClient Data Provider is the default application name for .NET apps
    IF (@connStrAppName <> '.NET SQLClient Data Provider')
        SET @appName = @connStrAppName

    -- end change

SET @appName = LOWER(@appName)
SET @appId = NULL

SELECT @appId = AppId
FROM [ASPState].dbo.ASPStateTempApplications
WHERE AppName = @appName

IF @appId IS NULL BEGIN
BEGIN TRAN 

SELECT @appId = AppId
FROM [ASPState].dbo.ASPStateTempApplications WITH (TABLOCKX)
WHERE AppName = @appName

IF @appId IS NULL
BEGIN
EXEC GetHashCode @appName, @appId OUTPUT

INSERT [ASPState].dbo.ASPStateTempApplications
VALUES
(@appId, @appName)

IF @@ERROR = 2627 
BEGIN
DECLARE @dupApp tAppName

SELECT @dupApp = RTRIM(AppName)
FROM [ASPState].dbo.ASPStateTempApplications 
WHERE AppId = @appId

RAISERROR('SQL session state fatal error: hash-code collision between applications ''%s'' and ''%s''. Please rename the 1st application to resolve the problem.', 
18, 1, @appName, @dupApp)
END
END

COMMIT
END

RETURN 0 
GO

and the web.config:-

和web.config: -

<sessionState mode="SQLServer" sqlConnectionString="Data Source=.;Integrated Security=True;Application Name=TEST" cookieless="false" timeout="20"></sessionState>
   <httpRuntime targetFramework="4.5"/>

You have to add Application Name and that have to be the same for all the application for which you want to share the same session.

您必须添加应用程序名称,并且必须与要共享同一会话的所有应用程序相同。

Thanks.

谢谢。