I have been trying to learn how to use sql express with C# and I am having difficulty connecting to a database that already has data in it. When I connect to the sql server my program adds a new database instead of reading the one that is currently in the sql server.
Here is my connection string which I think is where the issue is.
<add name="GolfLeague1" providerName="System.Data.SqlClient"
connectionString="Data Source=(localdb)\v11.0;Initial Catalog=aspnet-WebUI-20140205175325;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-WebUI-20140205175325.mdf" />
This gives me a new database called GolfLeague1(WebUI). Which I can read and write to, but it is not what I would like to do.
I have tried the string with and without a "Database=".
What part of the big picture am I missing here?
You have specified 'AttachDbFileName' property when you already have specified the database name. This part is not required here.
You can use the below mentioned connection string.
<add name="GolfLeague1" providerName="System.Data.SqlClient"
connectionString="Data Source=(localdb)\v11.0;Initial Catalog=aspnet-WebUI-20140205175325;Integrated Security=SSPI;" />
Related
I am writing Integration tests for a WPF application and I'm using a local database that is placed inside the solution for integration testing and this database has data inside it.
When I read data from this database after connecting it to SQL Server, it returns valid data, but once I go back to the application, add its connection string and try to get data, the code returns an empty set. I'm on a tight deadline and I don't know why this is happening.
Any help will be greatly appreciated.
Simple code is:
var xsystem = context.Species.ToList();
My connection string is:
<add name="MaxDatabase"
connectionString="Data Source=(LocalDb)\ProjectsV13;Initial Catalog=Catalog=MaxLocalEmbeded;Integrated Security=SSPI"
providerName="System.Data.SqlClient" />
Just replace your connection string with this below code.
Also remove "catalog=catalog" from your connection.
<add name="MaxDatabase" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=MaxLocalEmbeded;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MaxLocalEmbeded.mdf" providerName="System.Data.SqlClient" />
I am at a loss as to how to fix this issue.
When running my MVC application and trying to access the user database I get the dreaded SQL Network Interfaces, error: 50.
I can see all the data in the Server Explorer in VS, but I can never get the application to see it.
I have spent sometime reading this;
SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance 9
I have renamed and created new databases.
I have added/changed the entry in the applicationHost.config file.
I have changed the connection string every which way from Sunday.
I just cant figure it out.
Here is my connection string in the Webconfig file.
<add name="UserConnection" connectionString="Data Source=(LocalDB)\V11.0;AttachDbFilename=|DataDirectory|\ShopUsers.mdf;Initial Catalog=ShopUsers;Integrated Security=True" providerName="System.Data.SqlClient" />
I even downloaded and ran the ContosoUniversity sample code to see if it works, and it does.
Here is that connection string, (that works fine)
<add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ContosoUniversity1;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
It seems there is something amiss between IIS and the db Server.
Any new ideas would be greatly appreciated.
Oh ya, I did have this working just fine on my old PC.
Seems like you have the database file created already (I suspect you do because of the following segment: AttachDbFilename=|DataDirectory|\ShopUsers.mdf;). If so, then ensure you have it in the same folder beneath your project as the original one on your old PC, usually the |DataDirectory| stands for App_Data (look the answer to this question).
If, you need a brand new database however, you should have tried the following connection string:
<add name="UserConnection" connectionString="Data Source=(localdb)\v11.0\ShopUsers;Initial Catalog=ShopUsers;Integrated Security=True" providerName="System.Data.SqlClient" />
This will create the database for you.
I am looking for a suggestion, how can I connect database without writing the connection string in the native application.
I am using this but looking for another so that, when I rename database, the software will connect automatically.
static string conStr = #"server=myServerName;Integrated Security=true;Connection Timeout=5;Database=myDatabaseName;";
You can put the connection string in the configuration file.
<connectionStrings>
<add name="MyDatabase" connectionString="server=myServerName;Integrated Security=true;Connection Timeout=5;Database=myDatabaseName;" providerName="System.Data.SqlClient" />
</connectionStrings>
Then to retrieve the string you want you use:
ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString
Asking for it to connect automatically even if you rename the database isn't possible. Think about it, a single server can host hundreds of databases. How would it know which one to connect to?
I am learning basics of MVC. And forgive me If I am not able to ask question properly because I am not aware of various technical terms. Anyways I am trying build one simple page where I will have two tables courses and Instructors. I am able to do most of the stuff and it seems to be working. Also I do have a solution given by my faculty of the same problem. When I am comparing my solution and faculty solution then in the web.config file of the actual solution given by my faculty I see something like
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MvcCRUDwithSQL-20140217025002.mdf;Initial Catalog=aspnet-MvcCRUDwithSQL-20140217025002;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="CourseContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=CourseContext-20140217145250; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|CourseContext-20140217145250.mdf"
providerName="System.Data.SqlClient" />
</connectionStrings>
Where as in my solution that I just have one add name tag. I don't know why the another tag did not get created automatically. Or do I need to add that tag manually.
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication9-20150313042831.mdf;Initial Catalog=aspnet-WebApplication9-20150313042831;Integrated Security=True" providerName="System.Data.SqlClient" />
Have I done something wrong that the connection string related to CourseContext did not get generated automatically. Or we need to add for CourseContext manually? Please help me. Since most of the files and its content are generated automatically I must have done something wrong that connection string pertaining to CourseContext did not get generated.
Use the steps below to generate the connection string via visual studio server explorer
Click on server explorer
Connect to database
Choose server name from dropdown
Choose your authentication type
Select your database from dropdown
Test connection
Copy the connection string from the properties and paste in your web.config
Also take a note of #Guffa answer, he's got a very good point
You don't need one connection for each table, you only need one connection for each database. A database can contain many tables (the project I'm working on right now has 9 tables, and that is a really small project).
The Visual Studio template starts out with a default connection string, but no database. It looks like it's that connection string that is still left in the solution.
Im working on a MVC Application and so far ive been using localDB since that was which was included in the tutorial. I want to switch the application now to my SQL Server but im not sure how to.
I get that I have to change the connection String. But not in what way exactly since the SQL Server has a username and password which my localDB doesnt.
Another question regarding this is, do I have to create the tables myself in the beginning on the SQL Server or will they be generated by the entity framework like in my localDB?
Current connection string for localDB:
<add name="AcquisitionDBContext"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Acquisitions.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
Im pretty new to this so any help is very much appreciated
if you are going to use MS SQL then it wil look something like this:
<add name="AcquisitionDBContext" connectionString="Data Source=hostname\sql_instance_name;Initial Catalog=databaseName;Persist Security Info=True;User ID=username;Password=password;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
Where "hostname\sql_instance_name" is the servername(or ip)/the sql instance name eg. localhost\sqlexpress
ConnectionStrings.com is the best reference for connection-string syntax. It shows you the options to use when you have a username+password. In this case:
Server=myServerAddress; Database=myDataBase; User Id=myUsername; Password=myPassword;
Entity Framework will not create your tables for you unless you tell it to by calling something like this:
Database.SetInitializer( new DropCreateDatabaseIfModelChanges<YourDbContextHere>() );
(Where Database is the System.Data.Entity.Database type).
Try it:
<connectionStrings>
<add name="<<Name of Connection String>>" connectionString="Data Source=<<Put here path to your SQL SErver>>;Initial Catalog=<<Database name>> ;Integrated Security=SSPI;Trusted_Connection=true;" providerName="System.Data.SqlClient" />
</connectionStrings>
Here's how connection string should look like (tweak to your needs)
<add name="DefaultConnection" connectionString="Data Source=server_name,port; Initial Catalog=your_catalogue_name; Integrated Security=False; User ID=userName;Password=Passwd; MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>
Before first run I would recommend using migrations.
Go to PackageManagerConsole (if you don't have it in menu, install NuGet first via Extension and Updates)
Then inside console type Enable-Migrations (if you didn't already).
Next type Add-Migration your_migration_name and then Update-Database to let migrations create tables for you.
If you change something in your model files later, you can again type Add-Migration your_migration_name and Update-Database to update database with those changes.