Visual Studio - Connection String for SQL Server Database Project - c#

What is the correct way to write a connection string for a SQL Server Database Project within the same solution?
My connection string defaults as
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-PokemonDayCare-20170701121228.mdf;Initial Catalog=aspnet-PokemonDayCare-20170701121228;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
That LocalDB doesn't actually exist, so it creates it once it needs it (I think it does because I'm using single user authentication?).
So I need to make sure it doesn't create another database if I get the connection string wrong.
Here are the properties for my database project (it sits in the same solution as my web app).
How do I create a connection string for the database project?
(sorry if it's stupid or painfully obvious)

In your solution explorer double click properties
Select the Debug Tab
Select Edit Under Target Connection String Setting

Related

SQL connection string for embedded database in Visual Studio solution

I am starting with SQL Server in Visual Studio and have a problem, most probably with my connection string.
I am not installing SQL Server on my computer (there are several reasons for that) but created inside my solution a SQL project. Then, in another project I am attempting to connect to this database using some googled examples, but can't manage to connect. I frequently see that there's a user and password used to connect to the db, but in my case, thus there is no installation but an embedded SQL project, I can't work it out...
My connection string looks like this:
sqlConnectionString = #"Data Source = MyComputerName;initial catalog=DatabaseStudents";
Can anyone let me know if this kind of installation is even possible?
Thanks
This should be in your web.config if you're looking for a simple answer
<add name="YourDBContext"
connectionString="Data Source=(LocalDb)\ServerName;Initial Catalog=DatabaseName;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\DatabaseFile.mdf"
providerName="System.Data.SqlClient" />

How to connect ASP.NET project with SQL Server database table in Visual Studio (C#)

I have created simple database table in Visual Studio 2015 called Users and updated it.
I want to connect it with my project, so after I fill in my text fields and press 'Sign Up' button, it can get saved to database. I am aware that I'll have to use string connections and other stuff to connect it, but how do I first connect this project with web.config ?
I have seen people dragging this table to Registration form code, so it would automatically create a connection with web-config, but it didn't work for me.
How do I reference my database from web.config correctly ?
Right-click on MyDatabase.mdf and in the properties panel you will see the Connection String. Copy that into the web config in the ConnectionStrings node:
<connectionStrings>
<add name="MyDatabase" connectionString="[Paste Connection String Here]"/>
</connectionStrings>

Strange behavior with Entity Framework connection strings

Okay, this is a weird one. I have a solution that contains two projects. Project A references Project B. Project B is, essentially, a class library project that contains an Entity Framework Model, created database first. In the app.config of Project A, I have a few connections strings that point to different databases on one server. My software works fine. Here are my connection strings:
<add name="LRIP 1, 2, 1A" connectionString="metadata=res://*/Kreus2Model.csdl|res://*/Kreus2Model.ssdl|res://*/Kreus2Model.msl;provider=System.Data.SqlClient;provider connection string="data source=KREUS;initial catalog=Kreus2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="ECP 1" connectionString="metadata=res://*/Kreus2Model.csdl|res://*/Kreus2Model.ssdl|res://*/Kreus2Model.msl;provider=System.Data.SqlClient;provider connection string="data source=KREUS;initial catalog=Kreus2ECP1;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="ECP2" connectionString="metadata=res://*/Kreus2Model.csdl|res://*/Kreus2Model.ssdl|res://*/Kreus2Model.msl;provider=System.Data.SqlClient;provider connection string="data source=KREUS;initial catalog=Kreus2ECP2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="TR-12" connectionString="metadata=res://*/Kreus2Model.csdl|res://*/Kreus2Model.ssdl|res://*/Kreus2Model.msl;provider=System.Data.SqlClient;provider connection string="data source=KREUS;initial catalog=Kreus2TR_12;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Now, my coworker is also working on a solution that uses Project B (the data model). He has one connection string in his app.config file:
<add name="Kreus2Context" connectionString="metadata=res://*/Kreus2Model.csdl|res://*/Kreus2Model.ssdl|res://*/Kreus2Model.msl;provider=System.Data.SqlClient;provider connection string="data source=KREUS;initial catalog=Kreus2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
As you can see, the connection string is pointing to the same server (Kreus) that mine are. He wanted to use my connections strings (since I already had them set up) and pasted them into his app.config.
Now, when he runs, he's getting an error:
The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development...
We're both using the same model (the one in Project B), so I'm not sure why my connection strings aren't working for him. There is no difference between the models, we're using it as a reference for the main project. Being relatively new to EF, I'm at a loss as to where to look for solutions. I'm hoping someone can point me in the right direction.
EDIT: For clarity, I should mention that my program lets the user pick from a drop-down and chooses the data table they want to interact with. I pass the chosen name into the constructor for the datacontext:
var dataContext = new Kreus2Context(name); // name is what the user chose for the drop-down
My co-worker added that same functionality (drop box, and constructor for context that takes a string parm for connection string name).
TL;DR: My co-worker and I are both using the same database first EF model. My connections strings cut and pasted into his app.config just do not work, even though we're using the same exact model (it's in a class library project that we both are referencing in our programs).
UPDATE: As pointed out in the answer below, the ultimate source of the problem here was that my co-worker copied the connection strings into the wrong app.config file. He had two projects in his solution, one for the GUI and one for the business logic, that used the Project B (EF model). The connection strings should have been applied to the app.config file in the GUI project, his executing project. Instead, they were copied to the app.config file in his business logic project, which wasn't even being used.
Do they point to the same project? If they both do the connection strings must reside in the controlling executable. (see No connection string named 'MyEntities' could be found in the application config file).
Model first or database first calls on model creating to consume the .edmx file. Code generated by code first will throw the above exception in the on model creating method since code first does not use it.
a scan of the code will show if this method is returning the exception
if this is the case change the connection string to use the code first format

Azure Cloud Service App trying to connect to (localdb) instead of Azure SQL

I have a new App that I am trying to deploy to Azure Cloud Services. Developed locally using VisualStudio 2012, Entity Framework - Code First, and SQL Server Management Studio.
I have an Azure Cloud Services Project, a Web Role Project, a service layer class (C#) library, and a data objects class library.
These all work together beautifully on my local machine. Web calls service, service calls context, context returns (or modifies) objects. Yay!
I then published the web app. Added a web.release.config transform with this statement...
<connectionStrings>
<add name="DefaultConnection"
connectionString="Server=tcp:xmeg1j7x57.database.windows.net,1433;Database=RS.Core.Objects.RSData;User ID=****#xmeg1h7x57;Password=**********;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</connectionStrings>
and no joy. It still seemed to be trying to call my (localdb).
So I ran it locally (having changed the web.config to have the azure production string) and, at a breakpoint, looked at...
WebConfigurationManager.AppSettings["DefaultConnection"]
expecting a string. Discovered an array. Second value was from the web.config, but the first was an auto-generated reference to (localdb). How do I get rid of this reference? Obviously I'm missing something, but hours of research have proved my Google-Fu to be weak.
Two things to check for that have caused this issue for me in the past:
I was calling some of the System.Security methods (specifically, User.IsInRole()) prior to the MembershipProvider being created. For some reason, this tried to check the localdb instance for user information. Once the MembershipProvider was running, I didn't have this issue. I coded around this bug due to time restrictions.
Rename your connection string from "DefaultConnection" to something else, maybe "AzureDbConnection", and change all the references where appropriate.
You might also find it useful to search the entire project for "DefaultConnection" and see if it is being dynamically set, as well as clearing any app.configs of connection string information.
Try adding a </clear> before the "add"?
<connectionStrings>
</clear>
<add name="DefaultConnection"
connectionString="Server=tcp:xmeg1h7x57.database.windows.net,1433;Database=RS.Core.Objects.RoomScopeData;User ID=RSAdmin#xmeg1h7x57;Password=**********;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</connectionStrings>

sqlite is given me headache in my C#2.0 windows application

I'm struggling since this morning about this sqlite thing.after reading some good tutorials i decided to use it as my embedded database in my winform application.
i downloaded SQLite-1.0.61.0-setup that install the system.datal.sqlite for me and downloaded the firefox plugin sqlite manager and another management tool called wxSQLitePlus found here based on this tutorials found here (just being explicit)
Hoping i'm not posting any stupid stuff that will be ignored, here are my 2 challenges.
the first challenge is that i can't reference the database inside a folder let'say database
i have exception "Unable to open the database file".my app.config is as follow
<connectionStrings>
<add name="embcon" connectionString="Data Source=database\iagency.db;Version=3;"/>
but if the iagency.db is inside the root folder there is no problem at all.app.config like
<connectionStrings>
<add name="embcon" connectionString="Data Source=iagency.db;Version=3;"/>
the second chalenge is that object created via tools such as either firefox plugin or wxsqlite+
cannot be queried inside visual studio 2005 that i'm using.if it's a table when queried , i have an excetion the object doesn't exist even though both tools displays the same data.
and inversly table created by queries inside visual studio cannot be viewed by the tools.
so what's the deal?
is it a memory stuff or i'm just missing some points?
can enybody explain what is happening ?Thanks for reading.
Try using the full filepath as the data source (e.g. Data Source=c:\data\iagency.db)
See this post for more: datasource location in connection string
Try that :
<add name="embcon" connectionString="Data Source=|DataDirectory|\iagency.db;Version=3;"/>

Categories