SqlCeException database is opened with a read-only connection - asp.net

We store a template for building SQL Server CE connection strings in our web.config and use string.format to set the data source and temp path values.
In the web.config, the template for the DB connection string is:
"Data Source={0}; Temp Path={1}; Mode=Read Only"
In the web.release.config, we use to build deployment packages, the template for the DB connection string is:
Data Source={0}; Temp Path={1}
Now we only use the SQL Server CE database for read only operations. So, being a smartypants, I thought I would remove the connection string transform in the web.release.config and just always use the read only connection string from the web.config.
This worked fine for the database I was testing. But the same code running with a different SQL Server CE database fails with the following exception:
Exception 'System.Data.SqlServerCe.SqlCeException' with message 'The
database is opened with a read-only connection. Can't perform
post-initialization operations like re-building indexes and upgrading
public tracking. Please re-open with a read-write connection. [
Database name = D:\inetpub\wwwroot\MyApp\App_Data\Storage.sdf ]'
We are using the System.Data.SqlServerCe.4.0 provider
Why in my release build that is deployed to a server does read only access fail for some SQL Server CE databases but not others? It is the same code, just pointing at a different .SDF file. The .SDF files have the same schema, just different data.
Is the driver trying to rebuild indexes because the .SDF file is in some older format? Should we never use a readonly connection in production? Is that why the original code had the transform? why do they all work with a readonly connection on my Windows 7 workstation?

You should always specify a temp path, as moving databases between OS platforms/versions can require index rebuilds.
See my blog post post here: http://erikej.blogspot.dk/2009/08/running-sql-compact-from-cd-rom-read.html for more detailed info.

Related

What is the Data Source syntax SQL Client Connection String to Azure?

I am attempting to configure the connection string in web.confg for an ASP.NET app to connect to Azure DB. I am new to this but am familiar with sql configuration strings to SQL Server. How do I configure the login and data source in the connection string I am building. I haven't ever dealt with the cloud like this. If my db name is DB1 how do I set the Azure path for the data source portion of the connection string. NOTE: I know where to configure it in web.config and know to create a connection object.
Thanks.
In the Azure portal, you can navigate to your database's dashboard and click 'Show connection strings' on the right side. See the first screenshot on the below tutorial:
http://azure.microsoft.com/en-us/documentation/articles/sql-database-dotnet-how-to-use/
To answer your original question, "Data Source" means the same as "Server", it is just your server name, i.e. blah.database.windows.net. All of the connection string keywords (including their synonyms) are documented here: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring%28v=vs.110%29.aspx

The underlying provider failed on open entity framework

I am unsing entity framework in my web application. Earlier everything was working fine. But then I deleted database from Sql Server. After that whenever I run the application, I get the following exception :
The underlying provider failed on open entity framework
Isn't it true that if we delete the database from Sql Server and again run the application, database is again created?
Can you show some details about connection string. I basically want to check the authentication you are using.
Demo for setting a typical authentication : http://msdn.microsoft.com/en-us/library/ff649314.aspx
Another possibility can be with connection pooling. Try explicitly closing the connection(if there are any open connections)
db.Database.Connection.Close();
Entity framework will recreate your database on application start assuming the following:
You have permission to create databases in SQL
A connection string is specified correctly (if it was working before this should be fine)
You are using CodeFirst or ModelFirst strategies for databases (ie if you generated an EDMX off an existing database it wont recreate it for you)
I had a similar error and it was due to the connection problem.
Usually you need to open your *.EDPS file under your entity (EDML) and check your connection string and make sure it has got a correct setting especially your Default Oracle home.

Moving SQL Server from local computer to server

I kind of new to SQL Server, I always used access db for my sites.
I created a SQL Server on my local computer and now I want to take this db and transfer it to the server. In access all I had to do is, take the mdb file and put it on the server and change the connection string. How can I transfer the SQL Server db to the server?
Is there any file to put on the server ?
Also the connection string isn't a folder but a local computer like this:
Data Source=my-PC;Initial Catalog=storeSQL1;User ID='my-PC\com';Password='';Trusted_Connection=YES;
Who can provide me this connection string for the server (the hosting company) ?
The easiest way would probably be to create a backup of the database on your local machine, then restore that backup on the new server.
Roadmap is:
Do simple backup-restore to move user databases to target server.
Create script on source server, that can recover permissions and login-users pairing
Restore the CLR and TRUSTWORTHY security for databases, that using unsafe assemblies, simpliest way is (in proper DB):
exec sp_changedbowner 'sa' --sa just for example
ALTER DATABASE dbname SET TRUSTWORTHY ON
Enjoy
Depending on your version of SQL Server here is a good article that outlines all the ways to move a SQL Server Database.
http://blogs.msdn.com/b/sreekarm/archive/2009/09/11/move-a-database-from-one-server-to-another-server-in-sql-server-2008.aspx
As for getting the connection string yes the hosting company would provide you with that. Where is the database hosted, you could check their knowledge base articles or if it's an in house data base I'm sure a dba could provide you with that information. It won't change much from what you have but it will change.
I'm not sure what tools your using, but to start you need to do a dump or backup of your current database on your machine. After you do that then you can do and import which should create all the tables and import any data you have.
After the data exists on the server then as far as the connection string, you just need to say the Data Source is the server ip address or host name and change your User ID and Pass to match that server.
If you need more details on any part of this process, post what tools your using and what your environment looks like and I would be more than happy to assist you.
In my opinion the best way to do that is to detach the db from one server(pc), copy the files to the second one and then attach them on the second server/pc.
To detach:
USE master;
GO
EXEC sp_detach_db #dbname = N'AdventureWorks2008R2';
GO
To attach:
USE master;
GO
CREATE DATABASE MyAdventureWorks
ON (FILENAME = 'C:\MySQLServer\AdventureWorks2008R2_Data.mdf'),
(FILENAME = 'C:\MySQLServer\AdventureWorks2008R2_Log.ldf')
FOR ATTACH;
GO

Cannot open database 'dbname' requested by the login. The login failed

Firstly, I know that there are a lot of similar topics, but none seem to solve my problem.
I have inherited a project where the front-end is built in ASP.NET and the CMS is built in classic ASP. I'm in the process of setting up a local development environment where I've copied the database from the live server and am configuring the website to connect to my new local DB.
The ASP.NET part is connecting with the connection string Data Source=.\\SQLEXPRESS; Database=DBNAME; Trusted_Connection=True; but I had to mess about with Application Pools and Permissions.
I'm having a problem with getting the classic ASP part of the website to connect to the SQL database. The error I am recieving is:
Microsoft OLE DB Provider for ODBC Drivers error '80004005'
[Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open database "DBNAME" requested by the login. The login failed.
/cms/connections/Conn.asp, line 18
The code in that file is:
12. Dim Conn_STRING, URL, mailHost
13. Conn_STRING = "Driver={SQL Server}; Server=.\SQLEXPRESS; Database=DBNAME; Trusted_Connection=True;"
15. Dim conn
16. set conn = Server.CreateObject("ADODB.Connection")
17. conn.ConnectionString = Conn_STRING
18. conn.Open
I know it's a permission problem or some sort, but I can't get to the bottom of it. I'll appreciate any help at all :)
hmmmm, well, the asp.net part is using SQLExpress, connected via an attached file. DBNAME in the first example is going to refer to a db file name in the actual asp.net project. It's been quite a long time since I last used classic ASP, but I'd be STUNNED if it supports this. You need to attach the db file to an actual sql server and use it that way.
server=mycomputername\sqlexpress; database=DBNAME; user id=username; password=password
You'll need to open your database in sql express mngmt studio
http://www.microsoft.com/download/en/details.aspx?id=7593
Go into your security settings for that user and set the default database to "dbname" (whatver your real dbname is of course)
Ensure your app pool is set to run under the user that has permissions to that database. It may just be easier to enable mixed mode authentication and create a sql user to connect to the database rather than messing around with windows authentication on an app pool.
You wil lof course need to change your connection string then to support the sql authentication if you go that route.
http://msdn.microsoft.com/en-us/library/ms143705%28v=sql.90%29.aspx

How To Query A Database That's Being Used By Asp.Net

I have a Sql Server 2008 Express database file that's currently being used by an ASP.NET application, and I'm not sure how to query the database without taking the website down.
I'm unable to copy the database files (.mdf and .ldf files) to another directory, since they're in use by the web server. Also, if I attach the databases to an instance of the sql server (using the 'Create Database [DB name] on (filename = '[DB filename.mdf]') for attach;' command at the sqlcmd prompt), then the application pool user becomes unable to access the database (i.e. the webpages start producing http 500 errors. I think this might have to do with the username for the application pool becoming somehow divorced from the login credentials in the sql server database).
Any suggestions? I realize this is probably a newbie question, since it seems like a rather fundamental task. However, due to my inexperience, I really don't know what the answer is, and I'm pretty stumped at this point, since I've tried a couple of different things.
Thanks!
Andrew
if I attach the databases to an instance of the sql server (using the 'Create Database [DB name] on (filename = '[DB filename.mdf]') for attach;' command at the sqlcmd prompt),
Don't do this to a live database - it's attempting to be setup an MDF to be written to by two different databases...
Use Backup/Restore
As you've found, Attach/ReAttach requires the database to be offline - use the Backup/Restore functionality:
MSDN: Using SSMS to Backup the Database
MSDN: Using SSMs to Restore the Backup
Be aware that the backup/restore doesn't maintain logins (& jobs if you have any associated with the database) - you'll have to recreate & sync if using an account other than those with uber access.
Maybe Linked Server would work?
Another alternative would be to setup another SQL Server Express/etc instance on a different box, and use the Linked Server functionality to create a connection to the live/prod data. Use a different account than the one used for the ASP application...

Resources