.NET Forum / ASP.NET / General / November 2007
Connection issues between .Net framwok and SQl Express 2005
|
|
Thread rating:  |
RGF - 19 Nov 2007 18:29 GMT Background:
I wrote a simple .bat file which executes the following sqlcmd call:
"sqlcmd -S BLUEBOX\SQLEXPRESS -d C:\App_Data\ASPNETDB.MDF -U MyUserCount -P MyUserPassword -Q "Exec SendMessage" -o C:\Vocalico \SendMailQuePump\Undelievered.txt"
the above command works fine, as long as no other application is accessing the db.
Also, using VS2005 I wrote a simple web based calendar scheduling application which I am testing by launching it via Visual Studio 2005 (using the built in ASP.Net Development Server), the web application seems to work correctly as well.
The Problem Found:
When I run the VS2005 web application the sqlcmd fails to login (note that the web application and the sqlcmd access the same db (C:\App_Data \ASPNETDB.MDF)). The following is the error I get form SQL 2005 Express when running the sqlcmd while the web application is running:
Msg 5120, Level 16, State 101, Server BLUEBOX\SQLEXPRESS, Line 1 Unable to open the physical file "C:\App_Data\aspnetdb.mdf". Operating system error 32: "32(The process cannot access the file because it is being used by another process.)". Msg 5120, Level 16, State 101, Server BLUEBOX\SQLEXPRESS, Line 1 Unable to open the physical file "C:\App_Data\aspnetdb_log.ldf". Operating system error 32: "32(The process cannot access the file because it is being used by another process.)". File activation failure. The physical file name "C:\App_Data \aspnetdb_log.ldf" may be incorrect. Msg 4060, Level 11, State 1, Server BLUEBOX\SQLEXPRESS, Line 1 Cannot open database "C:\App_Data\ASPNETDB.MDF" requested by the login. The login failed. Msg 18456, Level 14, State 1, Server BLUEBOX\SQLEXPRESS, Line 1 Login failed for user 'MyUserCount'.
Questions:
Shouldn't SQL 2005 Express allow more than one user to connect and authenticate to the db by default? Are there any settings I need to modify in Visual Studio 2005 or SQL Express 2005 in order to allow my web app and the sqlcmd command to co-exist and execute without interfering with each other?
My Setup:
VS 2005 .Net Framework 2.0 XP Professional SP2 SQL 2005 Express web.config connection string looks like this: <add name="ASPNETDBConnectionString" connectionString="Data Source=. \SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=SSPI;User Instance=True" providerName="System.Data.SqlClient"/
I have been going around in circles on this issue, any help would be greatly appreciated!
Sincerely, -ralph
Cowboy (Gregory A. Beamer) - 19 Nov 2007 19:04 GMT It does allow more than one user.
The problem you are experiencing is the installation of the membership database is a destructive "update" that requires an exclusive lock. If you want to prove this, try setting up a desktop app and a web app with the same connection string and play with both at the same time.
Solution: Recycle the web application.
 Signature Gregory A. Beamer MVP, MCP: +I, SE, SD, DBA
*************************************************
| Think outside the box! *************************************************
> Background: > [quoted text clipped - 59 lines] > Sincerely, > -ralph RGF - 19 Nov 2007 19:28 GMT On Nov 19, 2:04 pm, "Cowboy \(Gregory A. Beamer\)" <NoSpamMgbwo...@comcast.netNoSpamM> wrote:
> It does allow more than one user. > [quoted text clipped - 75 lines] > > Sincerely, > > -ralph Thanks Gregory,
Is there a way to make the the membership database a non destructive "update"? so that it does not require an exclusive lock.
Also with respect to your suggested solution <Recycle the web application.>, could you suggest the best implementation to recycle the web app without breaking the user authentication?
-ralph
Cowboy (Gregory A. Beamer) - 19 Nov 2007 20:32 GMT > Thanks Gregory, > [quoted text clipped - 4 lines] > application.>, could you suggest the best implementation to recycle > the web app without breaking the user authentication? The only way I can think of is to script out the tables using the scripting option in the SQL Manager. You can then run the DDL script (object creation) while the site is running. This is true as long as you are not attempting to use login functionality.
My favorite way of recycling an app is with iisreset. Not a good idea on a production site, but it works quickly. If you store state in SQL Server, your users will not be booted, unless they hit the site right at the recycle. I would opt for scripting, if possible, first.
 Signature Gregory A. Beamer MVP, MCP: +I, SE, SD, DBA
*************************************************
| Think outside the box! *************************************************
Norman Yuan - 19 Nov 2007 20:42 GMT Since you use SQL Server Express USER INSTANCE (hope you know what does that mean. Unfortunately, ASP.NET management wizard create ASPNETDB automatically as USER INSTANCE on existing SQL Server Express, assuming the developer knows USER INSTANCE. The reality is most newbie have no idea on USER INSTANCE at all and most ASP.NET book do not explain that), your call to the "sqlcmd" (i do not know why you need to run that) only runs when the calling process is under the same user account as the ASP.NET application, because USER INSTANCE only allow single user access (not neccesarily single connection, though), which is the user account used to run your ASP.NET application, by default, it is ASPNET or Network Service, or any other account you may have impersonated to.
If your application will be deployed to a host service provider's web server, the host provider would most likely not support SQL Server Express at all, so no USER INSTANCE will be available. If you know USER INSTANCE well, and have no difficulty understanding how to change ASPNETDB to a full SQL Server, that would be fine. If you don't, better spend some time on SQL Server, and its Express USER INSTANCE. You'd discover there is most likely no need to use USER INSTANCE for a web application/website.
> Background: > [quoted text clipped - 59 lines] > Sincerely, > -ralph RGF - 20 Nov 2007 00:05 GMT > Since you use SQL Server Express USER INSTANCE (hope you know what does that > mean. Unfortunately, ASP.NET management wizard create ASPNETDB automatically [quoted text clipped - 79 lines] > > Sincerely, > > -ralph Norman, If you've been working in the tech field long enough you should know by now that as technology evolves you become a newbie again, yup I am a newbie to the VS2005 and SQL 2005 Express beasts. I did not encounter user instance issues in previous projects I did (older gen. of MS technology).
You are correct in that, I used the helpful wizard, I mean, that is what is there for.. and as far as I can remember, it did not give me an option as to the USER INSTANCE option. Just to clarify, the account used (impersonation) to run the ASP.Net page is different than the account used by the sqlcmd call, I would presume that the separate accounts would spawn their own access and process threads .. If I understood your comment correctly "USER INSTANCE only allow single user access" this would mean that only one account (or one user) would be able to access and lock everyone out from a site that was created using the connecting wizard in VS2005, right? that would strike me as odd, but I will to some googling on "USER INSTANCE"...thanks for the lead.
Also, with regards to the usage of sqlcmd. I resolved to use "sqlcmd" because as you are probably are aware SQL 2005 Express does not come with SSIS, and DTS (SQL 2000) refused to run against my instance of SQL 2005 Express. So in order to schedule a SQL task to run periodically, it is valid to batch out a sqlcmd call, it beats executing the a store proc manually...:)
Norman Yuan - 20 Nov 2007 14:18 GMT OK, it is, as I expected, because of using SQL Server Express USER INSTANCE.
To solve your problem, the simplest way is to not use USER INSTANCE. You simply attach the ASPNETDB.mdf to SQL Server permenantly, and change the connection slightly (i.e. remove "AttachDBFile=..." and "User Instance=True" from the ConnectionString, and use SSMS to attach the ASPNETDB.mdf to the SQL Server/Express, if it has been detached previously, automatically or not.
Study on SQL Server Epxress USER INSTANCE is always good if you are to use it (but as I said in previous post, it does not have much chance in web application).
>> Since you use SQL Server Express USER INSTANCE (hope you know what does >> that [quoted text clipped - 115 lines] > periodically, it is valid to batch out a sqlcmd call, it beats > executing the a store proc manually...:) RGF - 20 Nov 2007 18:39 GMT > OK, it is, as I expected, because of using SQL Server Express USER INSTANCE. > [quoted text clipped - 132 lines] > > periodically, it is valid to batch out a sqlcmd call, it beats > > executing the a store proc manually...:) Thanks for your feedback Norman..
I read a bit more regarding "User Instance", the notion that it only works on SQL 2005 Express and it is not supported on other versions of SQL 2005, is enough of an incentive not to use UI. I am hopeful the tool I am developing grows beyond the 4GB cap placed on SQL 2K5 Express, at which point I will need to get a full SQL 2K5 version and I rather not have to revisit the connection strings because of the upgrade.
The DB has been permanently attached to SQL Express server for a while, as long as VS2005 is not running I don't experience any collisions when accessing the DB using SSMS.
With regards to your suggested connection string solution, the existing web.config connection string looks like:
<add name="ASPNETDBConnectionString" connectionString="Data Source=. \SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=SSPI;User Instance=True" providerName="System.Data.SqlClient"/
With your suggested changes the mod string looks as follows:
<add name="ASPNETDBConnectionString" connectionString="Data Source=. \SQLEXPRESS;Integrated Security=SSPI" providerName="System.Data.SqlClient"/>
I noticed that there would be no reference to the database to be defaulted to, thus when I run the tool I get the error listed below, which basically is stating that it does not know where where table "UserJobs" is found:
Invalid object name 'UserJobs'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'UserJobs'.
Source Error:
Line 6929: } Line 6930: DataSet2.UserJobsDataTable dataTable = new DataSet2.UserJobsDataTable(); Line 6931: this.Adapter.Fill(dataTable); Line 6932: return dataTable; Line 6933: }
So I tried adding an initial catalog argument as follows:
<add name="ASPNETDBConnectionString" connectionString="Data Source=. \SQLEXPRESS;Initial Catalog=ASPNETDB.MDF;Integrated Security=SSPI" providerName="System.Data.SqlClient"/>
but it fails still (Err; Cannot open database "ASPNETDB.MDF" requested by the login.),
What would be your suggestion for referencing the db (not attaching) to be used by the connection string?
Regards, -ralph
Cowboy (Gregory A. Beamer) - 21 Nov 2007 04:12 GMT Look up sp_attachdb in the books online with SQL Server Express. That will show you how to attach the database to the server rather than have it as a user instance. The connection string will then look something like:
server=.\SQLExpress;database={YourdatabaseName};Integrated Security=SSPI
You will no longer need the database MDF file name in the connection string.
 Signature Gregory A. Beamer MVP, MCP: +I, SE, SD, DBA
*************************************************
| Think outside the box! *************************************************
>> OK, it is, as I expected, because of using SQL Server Express USER >> INSTANCE. [quoted text clipped - 213 lines] > Regards, > -ralph RGF - 21 Nov 2007 05:06 GMT On Nov 20, 11:12 pm, "Cowboy \(Gregory A. Beamer\)" <NoSpamMgbwo...@comcast.netNoSpamM> wrote:
> Look up sp_attachdb in the books online with SQL Server Express. That will > show you how to attach the database to the server rather than have it as a [quoted text clipped - 226 lines] > > read more >> Will do, thanks again Gregory! I will postback on the results...
RGF - 29 Nov 2007 17:02 GMT On Nov 20, 11:12 pm, "Cowboy \(Gregory A. Beamer\)" <NoSpamMgbwo...@comcast.netNoSpamM> wrote:
> Look up sp_attachdb in the books online with SQL Server Express. That will > show you how to attach the database to the server rather than have it as a [quoted text clipped - 226 lines] > > read more >> I was out of town, sorry for not getting back to you folks sooner..
I was able to attach the db to the server and implement the connection string as you suggested and I am now able to connect to the web application, and run the external sqlcmd commands without db access concurrency issues....Many thanks for you (and Norm's) pointers... your advice kept me from going boinkers..:)
I did encounter a side effect which I wanted to share with you;
While in VS2005's Server Explorer I detached and deleted the older connection to ASPNETDB.MDF (the one using AttachedDbFileName version) so that the older connection will be there no more. I took advantage of the detachment from VS2005 and copied the DB (.MDF & .LDF files) to a bigger drive to allow it to grow (so copied from H:\Inetpub\wwwroot \Vocalico\App_Data\ASPNETDB.MDF, copied to C:\Database\ASPNETDB.MDF)
I went to SSMS Express attached the ASPNETDB.MDF (found at C:\Database \ASPNETDB.MDF) to SQL Server 2005 Express using the store proc, everything went ok!
In VS2005's Server Explorer I created the new data connection to the DB that is attached to SQL 2005 Server Express, the connection string looks as follows:
Data Source=.\SQLEXPRESS;Initial Catalog=ASPNETDB;Integrated Security=True
The connection worked so far! I generated a few transactions and the data was correctly commited to the DB attahced to SQL Server (physically located at C:\Database\ASPNETDB.MDF).
What seems odd is that, I noticed that when launching the Web app, VS2005 automatically recreated the older ASPNETDB.MDF connection pointing to H:\Inetpub\wwwroot\Vocalico\App_Data\ASPNETDB.MDF, thus, from VS2005's Server Explorer I have two connection instances of ASPNETDB.MDF, that is, 1) The older connection at H:\Inetpub\wwwroot \Vocalico\App_Data\ASPNETDB.MDF and 2) The newer connection to the SQL Sever DB.
I ran my web application in order to test the end to end scenario, everything works correctly, and the are no concurrency issues when running external sqlcmd commands. On close inspection seems that ASP.Net is using the older .MDF file (H:\Inetpub\wwwroot\Vocalico \App_Data\ASPNETDB.MDF) to add and retrieve the Membership and Roles information exclusively, and is using the SQL Server attached db to store the transactions performed by an authenticated user... While everything works ok, I would seem rather odd to have to instances of the db to support a single app.
While using SQL 2005 Server Express, Is there a way to tell ASP.Net (VS2005) to use one db (the db attached to SQL Server) for all transactions? and not to auto recreate the connection to the older H: \Inetpub\wwwroot\Vocalico\App_Data\ASPNETDB.MDF?
Regards
Norman Yuan - 29 Nov 2007 18:13 GMT You have re-configure the website to use SQL Server Express as ASPNETDB. here is excert from someone else reply to similar question that might help.
<Quote>
As for configuring ASP.NET 2.0 membership to connect SQL Server 2000, you need to do the following things:
** use aspnet_regsql.exe tool to setup a database in sqlserver 2000 db
** add a new membership provider in your ASP.NET 2.0 application which use a connectionstring pointed to your SQL Server 2000 database
Here are two web articles provided detailed steps:
#Configuring ASP.NET 2.0 Application Services to use SQL Server 2000 or SQL Server 2005 http://weblogs.asp.net/scottgu/archive/2005/08/25/423703.aspx
#Using ASP.NET 2.0 Membership API with SQL Server 2000 http://www.vikramlakhotia.com/Using_ASPNET_20_Membership_API_with_SQL_Server _2000.aspx
</Quote>
> On Nov 20, 11:12 pm, "Cowboy \(Gregory A. Beamer\)" > <NoSpamMgbwo...@comcast.netNoSpamM> wrote: [quoted text clipped - 321 lines] > > Regards RGF - 30 Nov 2007 06:50 GMT > You have re-configure the website to use SQL Server Express as ASPNETDB. > here is excert from someone else reply to similar question that might help. [quoted text clipped - 242 lines] > > read more >> Norman, thanks for the fast response...the links you forwarded provided helped!!
I ran aspnet_regsql.exe as per the steps provided in the links you forwarded, I also modified the "LocalSqlServer" connection string inside the machine.config file, and everything (roles / membership and no collision issues ) worked like a charm!!
I really appreciate your (and Gregory's) guidance, clear advice and your patience on this issue, you guys rock!
Cheers, Ralph
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|