Trouble connecting to SQL Server Express database - asp.net

In my web application installed on IIS 7, I need to connect to a SQL Server Express database file (used for testing purposes only.) I use the following connection string in a web.config file:
<configuration>
<connectionStrings>
<add name="MyDbConnection" connectionString="data source=.\SQLEXPRESS;Integrated Security=true;AttachDBFilename='C:\Users\UserName\CSharp\WebApp004_TestDB\App_Data\Database1.mdf';User Instance=true;Connection Timeout=15" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
I also set up NTFS permissions for the C:\Users\UserName\CSharp\WebApp004_TestDB\App_Data folder as such:
IIS_IUSRS = FullControl
IUSR = FullControl
NT SERVICE\MSSQL$SQLEXPRESS = FullControl
IIS AppPool\[MyAppPoolName] = FullControl
That still didn't help. When I try to connect to that database via my C# code I get an exception:
An attempt to attach an auto-named database for file
C:\Users\UserName\CSharp\WebApp004_TestDB\App_Data\Database1.mdf
failed. A database with the same name exists, or specified file cannot
be opened, or it is located on UNC share.
Note that the same code works on another machine with the actual SQL Server installed (with a different connection string where I used DB user name and password.)
Any idea what am I missing here?

Have a look at this Connection Strings

Related

SQL Server not recognised after hosting the website

I have hosted a website on somee.com and I'm getting the error which says that it is not able to connect to the sql server.
The web.config has the connectionstring as
<connectionStrings>
<add name="RegConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Can anybody let me know the changes I have to make to get this working?
P.S. I have uploaded the database in App_Data folder.
Your connection string still pointing to your local SQL server and even it is local database of your system.
You have to modify your connection strig before publishing the site to the external server/ hosted server.
Change the whole connection string and put below properties within it.
Data Source=value; Initial Catalog=value; User ID = value; Password = value;
Here Data Source will be your SQL Server IP address or URL.
Here Initial Catalog will be your Database name.
You have to put User ID and Password which you are using to your SQL Server (Not Local)
Lose this AttachDbFilename=|DataDirectory|\Database.mdf I have never used it
Try these name value pairs
Data Source=value; Initial Catalog=value; User ID = value; Password = value;

ASP.NET Wepapplication with on-disk database - how?

How can I use an .mdf file (called SQL Server Database in VS2012) for Entity Framework?
I get this error message:
Invalid value for key 'attachdbfilename'.
This is my connectionstring:
<add name="DbContainer" connectionString="metadata=res://*/Database.Db.csdl|res://*/Database.Db.ssdl|res://*/Database.Db.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDb)\v11.0;initial catalog=DataContext;Integrated Security=True;AttachDBFilename=|DataDirectory|\DataContext.mdf;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />.
In Global.asax.cs, Application_Start I set the correct path for |DataDirectory| like this:
string newPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data");
AppDomain.CurrentDomain.SetData("DataDirectory", newPath);
Do any of you know to make this work?
Btw. it works when running my web application locally (i.e. debug), and does not work when publishing to my online domain.
If it works locally but not online, the problem is from data source part of your connection string. And if you want to attach your db at runtime, not by management studio, remove the initial catalig part. So change it to something like the following:
<add name="DbContainer"
connectionString="metadata=res://*/Database.Db.csdl|res://*/Database.Db.ssdl
|res://*/Database.Db.msl;provider=System.Data.SqlClient;
provider connection string="data source=RemoteServerInstance;
Integrated Security=True;AttachDBFilename=|DataDirectory|\DataContext.mdf;
MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />

SQL Server Express 2008 connection string

I am trying to configure my connection using SQL Server Express 2008.
Can anyone help me please. My server name is as follows: test\expressql2008 and my database is TestDatabase
My connectionstring is :
<add key="hibernate.connection.connection_string"
value="Server=localhost\EXPRESSQL2008;Database=TestMyproject;User=Me;Pwd=test;" />
It seems that the connections isn't working and I get the error:
NHibernate.ADOException : cannot open connection
Here's all connectionstrings needed
http://connectionstrings.com/
<add name="SD_ConnectionString" connectionString="Data Source=[SQLEXPRESS NAME];Initial Catalog=TestDatabase;User ID=[USERNAME];Password=[PASSWORD]" providerName="System.Data.SqlClient"/>
string con = ConfigurationManager.ConnectionStrings["SD_ConnectionString"].ToString()
Can you provide more detail about this error there could be number of reasons
1- Please check ASP.NET user has access to your database if you are using trusted connection.
2- It could be better if you create a SQL USER and assign it permission of DBOWNER to your database and with your connection use that user information.
con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
in web.config

MY ASPNETDB.MDF would not work in production server? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
A network-related or instance-specific error occurred while establishing a connection to SQL Server.
Today i have create two pages login.aspx using LOgin control and register.aspx using Create user wizard ...
when i use connection string of my production server in my local computer then site works only if my database ASPNETDB.MDF is present in APP_Data Folder in my local computer..
if i rename ASPNETDB.MDF or Delete ASPNETDB.MDF from my local compuet App_Data the the following error occurs ...
An attempt to attach an auto-named database for file C:\Users\Ashish Dobriyal\Documents\Visual Studio 2008\WebSites\VOLVOO\App_Data\ASPNETDB.MDF failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
but
After i publish my webpages ..... and database to my production server .... it produces an error ....
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
What was the problem actually ... ??
MY PRODUCTION SERVER CONNECTION STRING :
<connectionStrings>
<remove name="ConnectionString"/>
<add name="ConnectionString" connectionString="workstation id=volvobusesindia.mssql.somee.com;packet size=4096;user id=username;pwd=password;data source=dobriyal.mssql.somee.com;persist security info=False;initial catalog=dobriyal" providerName="System.Data.SqlClient"/>
</connectionStrings>
MY LOCAL SERVER CONNECTION STRING :
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
when i use connection string of my
production server in my local computer
then site works only if my database
ASPNETDB.MDF is present in APP_Data
Folder in my local computer..
This is because you need to add access permissions to the directory that you going to put the database. The permissions must be for the user that runs the asp.net pool that you using.
Alternative try this string, for both production and local server and just be sure that your database have the correct permissions for been access from the asp.net running pool account.
This is not depended from what you write on the string but from how you correctly setup the database. Its working 100% so if not play for you search the database permissions, both file and database settings.
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=localhost;Initial Catalog=dobriyal;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>

Unable to connect to remote SQL Server 2005

I have created my asp.net website using database ASPNETDB.MDF but when I try to host my site on server ...
The SQL Server 2005 hosting provider issue me the
database name dobriyalji
server ip : 69.112.222.220
database username : XXXX
database password : XXXX
I have imported all of my ASPNETDB.MDF tables to my server database dobriyalji ...
And I empty the Appdata.mdf and aspnetdb.log file from App_Data Folder ...
When I try to run my site the following error occurs ...
An attempt to attach an auto-named database for file
Users\Ashish Dobriyal\Documents\Visual Studio
2008\WebSites\VOLVO\App_Data\ASPNETDB.MDF
failed. A database with the same name exists, or specified file cannot
be opened, or it is located on UNC share.
MY LOCALHOST CONNECTION STRING :
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
MY REMOTE HOST CONNECTION STRING :
<connectionStrings>
<add name="vbi_india2kConnectionString" connectionString="Data Source=69.112.222.220;Initial Catalog=dobriyalji;Persist Security Info=True;User ID=XXXX;Password=XXXX" providerName="System.Data.SqlClient"/>
</connectionStrings>
You have to use publish database wizard to deploy local database to your production database server.
Do you have the physical mdf / ldf files from your development machine. Assuming you do as the error "specified file cannot be opened" is quite clear. So are you on a shared SQL Server with other sites; if so is there a database on the sql server called aspnetdb? You may have to change the name of your database as you attach it in order to get it onto the sql server.
EXEC sp_attach_db #dbname = N'myASPNetDB',
#filename1 = N'c:\mypathdata\myaspnet.mdf',
#filename2 = N'c:\mypathlogs\myaspnet_log.ldf'

Resources