SQLServer SessionState - iis-7

I am using InProc mode of SessionState, but my app is keep restarting because of the recycling of application pool so I am loosing session of currently logged in users. I want to change to save it in database. I edited my webconfig like this
<sessionState mode="SQLServer" timeout="20" allowCustomSqlDatabase="true"
sqlConnectionString="Data Source=Server;integrated security=True;Initial Catalog=SerialTracker;"
cookieless="false" />
Do I have to create some tables for session state or new database ? My hosting is shared so I can not acces to admin console or anything else.

There is a step by step HowTo available here. Seems you will be needing to run a SQL file named InstallSqlState.sql on the database.

Yes, you have to use the InstallSqlState.sql script that you can find - according to the machine - in %System Drive Letter%/Microsoft .NET/Framework/%v.????%
Example: c:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallSqlState.sql
Consider also that the script create a AspState DB who will not keep, by default, session data: these will be stored in tempdb table.
If these is not good for you there's the way to save data inside the AspState DB, take a look here.

As of 2015, you don't need to run a script, the Universal Provider package uses Entity Framework to create the table it needs.
I used NuGet/Package Manager to install the 'Microsoft ASP.NET Universal Providers' package (currently v 2.0.0) which also installs the Core:
Install-Package Microsoft.AspNet.Providers
It inserts some (commented) items in your web.config file. I followed the instructions in the comment to change 'InProc' to 'Custom' and set it up with the connection string to my database. On first run it creates a 'Sessions' table in my database and stores the session data in there.
I'm not sure what it would do if you already had a 'Sessions' table in your project.
ps - I removed the 'profile', 'membership', and 'rolemanager' tags it inserted, as I only need to store the session stuff, not the rest of asp identity.

Related

Why Jet OLEDB driver caches my MDB file?

I have a Cassini application that uses Access file. Updated installer overwrites both application and database. However, application still sees the old data even after system restart. (Also copied MDB file manually to make sure it's replaced but it doesn't help.)
The only way for the application to see new MDB data is to install it to a new folder.
Connection string is standard (I think):
Connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
HttpContext.Current.Server.MapPath("App_Data\\" +
ConfigurationManager.AppSettings.Get("dbfile")) + ";Jet OLEDB:Database Password=xxxx";
Any idea what's going on and how to avoid the caching issue?
Update (to answer some questions in comments):
We can reproduce this issue only on Windows 7.
MDB is never updated by the Cassini application. We create database and send it to users. - We did make sure that file was successfully replaced by installer. Actually the same thing happens if we replace it manually.
The problem remains after Windows reboot.
Go to solution Explorer
Select mdb file
Go to properties, there is one property
Do not copy set it to Copy if newer
Set the
Cache Authorization property of connection object to false which is by default true.
http://msdn.microsoft.com/en-us/library/aa140022(v=office.10).aspx
Have you tried using the "Microsoft.ACE.OLEDB.12.0" provider? I believe it comes pre-installed with Windows 7.
The new Windows 7/Server 2008 security model presents virtualised copies of files under some conditions for each user.
Are you possibly hitting this design feature that is presenting the stale snapshots to the users after the installation completes? Is it possible the application uses different credentials to the user installing the database?
There is a description of the functionality at User Account Control Data Redirection. The recommended solution is to use a more appropriate folder for the data

Problem connecting to database - user instance and Entity Framework issue

I've created an entity model file (.edmx) based on an .mdf file in my asp.net application, placed in my App_Data Folder.
first of all my connection string, created by the entity framework wizard :
<connectionStrings>
<add name="Sales_DBEntities"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='data source=.\SQLEXPRESS;attachdbfilename="c:\users\ext\documents\visual studio 2010\Projects\WebProject_A\WebProject_A\App_Data\Sales_DB.mdf";integrated security=True;USER INSTANCE=TRUE;multipleactiveresultsets=True;App=EntityFramework'"
providerName="System.Data.EntityClient" />
</connectionStrings>
The above is placed in an app.config file created by the wizard.
I've copied the same connection string to the web.config file as well (I'm not sure if this is necessary).
I've come across two problems when attempting to later run the same application on a my machine (I've created the application on a different machine).
The first was regarding the user instance:
Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed
The first thing I don't really comprehend is what a user instance actually defines.
According to MSDN :
A user instance is a separate instance of the SQL Server Express Database Engine that is generated by a parent instance
Is my local database considered a user instance ?
I would really like some clarification on the matter because I came across posts suggesting
to mark it as False, and the reason was not clear to me .
another post on this matter
From there I came across another solution that explains that for this problem you need to
enable user instance in the SQL Server on your machine.
The Fix
I've done what was explained in that post
but now I've come across a new problem :
An attempt to attach an auto-named database for file (Full Path to my app_data\my.mdf file) A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
I've already attempted to delete all the files under
C:\Documents and Settings\(your user account name)\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data\SQLEXPRESS
and rebooting like suggested in other posts, but still I can't find a clear solution for this problem.
I know this is a long post but any help would be appreciated
thanks in advance.
Read this very good overview of what user instances really are.
In a nutshell:
SQL Server allows you to have multiple instances of itself on a single computer - the default instance can be accessed using just the machine name (or IP address) of the host machine - all other instances need to be specified by an instance name (like the SQLExpress on your machine)
for development purposes, you can also create a user instance - each user gets their own separate instance of SQL Server (works in the Express edition only) and a database is attached by its file name (path\Sales_DB.mdf) to that user instance. This instance is started up on demand, then runs and is shut down when no longer needed
While this works great for development, it's not really intended for later on - certainly not for production use.
In a production environment, you want to attach your database files to the actual SQL Server instance they should run on - typically using SQL Server Management Studio. When you do this, you no longer refer to that database via its file name, but instead you use the database name that was given to the database when being attached to the server. In this case, SQL Server will handle all details of operations - and that SQL Server instance is up and running all the time (doesn't need to be started "on demand" all the time), and you can use things like permissions and logins to control access to the database.
Your connection string would then look something like this:
<connectionStrings>
<add name="Sales_DBEntities"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='server=YourServerNameHere;database=Sales_DB;integrated security=True;App=EntityFramework'"
providerName="System.Data.EntityClient" />
</connectionStrings>
When you have attached a database to a production (or testing) SQL Server instance, you do not need to know what the files are that make up the database, and you don't need to specify those files - SQL Server will take care of that. You just refer to the database via its database name.
The solution is always simpler then anticipated:
double click the model.edmx file -> the designer will open.
right click on the designer -> Update model from database .
new connection -> Choose Database file (.mdf) under type .
Browse -> go to your App_Data folder, and choose the DB.
copy the connection string created in App.config to web.config.
bye.

How do I copy my database

I am working on visual studio 2010 and i have a database in Server Explorer that i can see that is not in the App_data in the solution explorer.. I need to copy it somewhere to upload it to the host..
I think the database is suitable for SQL server2005 , cause that is the only sql server version installed on my computer with the vs2010.
Here are my connection string:
<add name="YourGuruDB" connectionString="Data Source=DIMA-00AA1DA557;Initial Catalog=model;Integrated Security=True" />
You can back up the database by right click on db and choose back up. Restore it on your host /web server.
I would prefer to export you database schema and mandatory data as sql scripts using SSMS (SQL Server Management Studio) then run the script on the host.
Here is a great tutorial about it http://blog.sqlauthority.com/2007/08/21/sql-server-2005-create-script-to-copy-database-schema-and-all-the-objects-stored-procedure-functions-triggers-tables-views-constraints-and-all-other-database-objects/
EDIT: if you are using VS2010 there is a new feature in the deployment called "Package/Publish SQL" allow you to package your SQL DB. Here is a tutorial how to use it http://rachelappel.com/deployment/database-deployment-with-the-vs-2010-package-publish-database-tool/
Erm. your connection string says the database in use is model. That's a special database in SQL Server that's used a template for new databases (that's why you can't detach/copy). You're going to have all sorts of problems if you stick with that name.
But DO NOT rename it. You're going to have to create a new database - which will have everything you've put in model in it. Then remove everything you've added to model.
You should now be able to detach your new database and move it elsewhere.
Select you database in SQL Managment Studio.Then Tasks\Detach.
After this you can copy the database files to another distination. To make you database on-line again, just do Tasks\Attach.
P.S It's only the one among other approaches to accomplish your task.

How to Rollback Asp.Net Membership Configuration(Tables&Procedures)

I'm developing a project that works on remote server database tables. But there were no membership, only one admin panel with 1 admin user. So I tried to use Asp.Net membership for this, but when I did that according to tutorials(on C:\WINDOWS\Microsoft.NET\Framework\version\aspnet_regsql.exe"), I guess there was a conflict so my project didn't work. I found "Windows authentication" line in my web.config, commented out, fixed it.
Then I decided to use a master password inside the code, with text boxes. This is really simple application, no high-level security needed. So I did it so. But when I opened the database, I realized that Asp.Net created its own tables and stored procedures. Now I want to rollback my Asp.Net (Membership) configuration. I can manually delete the tables and procedures but I fear of doing something wrong. What should I do?
Thanks in advance.
Run aspnet_regsql from .NET command prompt and it will start a wizard. You can either configure or remove the tables.. The wizard gives you option for both.. Select the one you need and your tables etc will be deleted from the database...
If you're not using asp.net membership, then it will do no harm to manually delete them.

Where to find state.sql for sqlserver mode sessionsates?

I am looking for the state.sql file provided by the .NET framework to run sessionstates in sqlserver mode. Where can I find the sql file to create the required tables and stored procs. I tried in the FRAMEWORKS/VERSION2.0 folder but I am unable to find it.
It's not state.sql anymore, but InstallSqlState.sql or InstallPersistSqlSate.sql.
Choose InstallSqlState.sql, if you would like to store your session data into tempdb (and thus to lose all sessions on every SQL Server restart). Or choose InstallPersistSqlState.sql if you would like to store them persistently.
This article has a great overview of both modes (temporary and persistent db), and the script is linked to from there also...

Resources