I'm trying to get my local SQL database connecting to my ASP.Net project and nothing I try is working.
The database is named cinemaDatabase and its using windows authentication.
In web.config I have
<connectionStrings>
<add name="myDatabase" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=cinemaDatabase;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
Then in a cshtml page I have
#using WebMatrix.Data;
#{
var db = Database.Open("myDatabase");
}
I keep getting this error
InvalidOperationException: Connection string "myDatabase" was not
found. OpenNamedConnection
What am I doing wrong here? Thanks in advance.
Edit -
Got it working, had to make a normal SQL server instead of the local MSSQLLocalDB one and change the data source in web.config to that.
Then used this code on my page and it connects now.
#using WebMatrix.Data;
#using System.Data.SqlClient;
#{
var connectionString = "Data Source=DESKTOP-58NGNQP;Initial Catalog=webDev;Integrated Security=True";
var providerName = "System.Data.SqlClient";
var db = Database.OpenConnectionString(connectionString, providerName);
}
I believe the problem is with your connection string. Unfortunately, I don't have a running example right this moment with an attached .mdf file, but I believe something like this will do the job for you, I grabbed this string from MSDN's article on connection strings
<add name="ConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />
Couple of important facts here:
Data Source=(LocalDB)\v11.0 - This specifies the start-on-demand localdb instance to use. Note that the version, v11.0 in this string, varies from installation to installation, and you should find the correct version for your string. (You should be able to find it easier below)
AttachDbFilename=|DataDirectory|\DatabaseFilename.mdf - this tells LocalDb to load that database file. Alternatively, localdb persists database files in your user home directory, or AppData directory somewhere.
Integrated Security=true - A connection string needs an authentication scheme. Two possible options are Integrated Security, which uses your windows credentials to attempt access to your database, which works well with many things, LocalDb being one of them. Another option is to use sql authentication and a sql name and password to connect.
Related
I've seen this error answered for other people, but I can't seem to get it to work. I'm trying to connect to a SQL Server Database project locally.
I'm getting this error thrown on Open() when I do the follow:
SqlConnection conn = null;
conn = new SqlConnection(connection)
conn.Open()
Where connection is a connection string passed from my webconfig. I've found examples online and tried these three connection strings and I get the same error every time:
<add name="TICKETING_CONNECTION" connectionString="Server=(local); DataBase=Ticketing_DB; Integrated Security=SSPI"/>
<add name="CONN"
connectionString="Data Source=localhost; Integrated Security=SSPI;Initial Catalog=Ticketing_DB"
providerName="System.Data.SqlClient" />
<add name="CONN2"
connectionString="Data Source=(local);Initial Catalog=Ticketing_DB;"
providerName="System.Data.SqlClient"
/>
Could the problem be stemming from the path of where the database is located on my machine? I've tried copying the c:\ path but didn't have any luck. I've seen some examples online use a .mdf file, but my project does not have this file either.
This is my first time trying to connect to a database project, so any help or advice would be greatly appreciated
If you are using Visual Studio try going to the Server Explorer and Right click on Data Connections and Add new connection by selecting DataSource as Microsoft SQL Server and provider as .net framework provider for SQL Server.
Under the server name check for the SQL Server if it is listed with your machine name.
Then try to provide the database details and user name and password for SQL Authentication or select windows for windows authentication. Then do a test connection. If it is fine on click of OK it will add the data connection.
Right click the properties of data connection which is newly added and navigate to properties to extract the connection string. Compare it with yours. This can help you troubleshoot the issue.
I am doing Microsoft's MVC getting started tutorial:
Getting Started with Entity Framework 6 Code First using MVC 5.
This includes the creation of a code first database.
It all works fine, but I am not able to find my database.
This is my ConnectionString:
<add name="MovieDBContext" connectionString="Data Source=.\SQLEXPRESS; Integrated Security=True" providerName="System.Data.SqlClient"/>
If I debug my index Method the connection is as follow:
Data Source=.\SQLEXPRESS; Integrated Security=True
But there is no Database in my SQLEXPRESS Instance, I have checked it with SQL Server Management Studio.
I also cant find anything if I search my filesystem for *.mdf.
App_Data in my Project is Empty...
But all CRUD operations are working fine, there has to be something.
The only way I can see that table is to connect to .\SQLEXPRESS via the Visual Studio Server Explorer. But where is this physically located? Why cant I see the table if I connect to .\SQLEXPRESS via SQL Server Management Studio?
Any idea?
You didn't specify the Initial Catalog in your connection string so probably you are using the Master database.
You need to specify the Initial catalog like this:
<add name="MovieDBContext"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=yourDBName;Integrated Security=True"
providerName="System.Data.SqlClient"/>
I started an ASP.NET Web Application project in Visual Studio Community 2013 from the Empty template and ticked the Web API checkbox. I've installed SQL Server 2014 Express separately, and I've connected to my database under Server Explorer. Not sure if any of that's relevant.
I'm trying to open a connection to my database using a connection string in Web.config:
<configuration>
<connectionStrings>
<clear />
<add name="DbConnection" connectionString="..."/>
</connectionStrings>
</configuration>
I've attempted to access it a couple ways:
var connStr = System.Configuration.ConfigurationManager.ConnectionStrings["DbConnection"].ToString();
var connStr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString;
When I run a UnitTest, Everything I try pukes out a NullReferenceException.
Taking a look at what's in ConfigurationManager.ConnectionStrings, all I find is machine.config's LocalSqlServer:
Why does it seem like Web.config isn't loading when I'm testing?
var connStr = System.Configuration.ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString;
Don't use .ToString(). Use the .ConnectionString property. In addition, your web.config should be at the root of your site.
Unit Tests have their own separate configuration. You need an app.config file, though the contents can be the same as you posted in your question.
I'm having a problem switching a project I am working on over to another system. It's an ASP.Net web application and web form wrapped in a single solution, with a SQL .mdf DB file in the project as well. I'd like to be able to zip up the whole thing and move it around, but when trying to debug the solution on the other machine I get an error about the local DB not existing. My connection string in the Web.Config file looks like this:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-name-of-database.mdf;Initial Catalog=aspnet-name-of-database;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
I know I must be making an obvious mistake, but I can't figure out what it is. Any advice, please? Thanks.
Edit: The specific error is:
"The specified LocalDB instance does not exist."
Run the following from your package manager console:
SqlLocalDb info
If the executable isn’t found, chances are you do not have LocalDb installed in your new machine. You can use the steps mentioned in the below link to debug as to why the connection is not successful to your localDB:
http://odetocode.com/blogs/scott/archive/2012/08/15/a-troubleshooting-guide-for-entity-framework-connections-amp-migrations.aspx
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True
Try setting the Datasource as below.
Data Source=.\;
If you are able to connect to your database, then the set the ServerName as DataSource in your connection string.
Make sure you have SSMS installed in your new system.
Download SQL Server Express 2014
I've Create MVC5 APP with Local DB like the following link(step 1 to 5)
http://www.asp.net/mvc/tutorials/mvc-5/database-first-development/creating-the-web-application
And the application is running as expected and now I need to change the DB from the
sql Lite to azure DB ,I have created the same DB in azure (same data structure)and now what I want that the application use this DB ,There is simple way to do that ?
Im using VS2013 for web.
Just change the "source" and "initial catalog" parameters of the connection string in the web.config file.
Also, SQL Azure doesn't work with integrated security so you need to remove the "integrated security" parameter from the connection string and add the parameters "User ID" and "Password" with the credential of a user that you created in SQL Azure.
Open Visual Studio In Solution Explorer
find the file web.config
Look for the section connectionStrings
You should find something similar to:
<add name="testEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\test.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
You need to:
Replace (LocalDB)\v11.0 with the name of your server
Remove "attachdbfilename=|DataDirectory|\test.mdf;integrated security=True;"
Add "Initial catalog=[name of your database in SQL Azure];user id=[name of your user id on SQL Azure];password=[your password]