ASP.NET MVC C# Connection String metadata creation - c#

Sorry, I have a feeling this is a really basic question, but my connection string to my data source is invalid. I did a bit of trouble shooting and think that I am missing the metadata part of the connectionstring - but I'm not sure where it is/how to create it.
How is how I think it should look
<add name="PDCWebEntities" connectionString="metadata=METADATASTRING;Data Source=.;Initial Catalog=PDC;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
And here is where I think the information that needs to be in the metadata string is held:
It should be noted that the app.config file where the connectionstring is is in the PokemonDayCareSimple.Web project
Is it possible to make the metadata part of the string from this information?

You don't need metadata for connectionString.
<add name="DefaultConnection" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\me\source\repos\MarketplaceMVC\MarketplaceMVC.Web\App_Data\MarketplaceIdentityDb.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
Instead of "AttachDbFilename" you can use also "Initial Catalog".
In your Context Class you need to add a constructor:
public ApplicationContext() : base("DefaultConnection")
{
}

Related

Add new connection in connectionString

I have the next connectionstring file:
<?xml version="1.0"?>
<connectionStrings>
<add name="DefaultConnection"
connectionString="Application Name=CalculatorTest;Server=PC01;Initial catalog=MyDB;Integrated Security=true" />
</connectionStrings>
I want to add a new connection, for example "MyDBTest", staying:
<?xml version="1.0"?>
<connectionStrings>
<add name="DefaultConnection"
connectionString="Application Name=CalculatorTest;Server=PC01;Initial catalog=MyDB;Integrated Security=true" />
<add name="NewDefaultConnection"
connectionString="Application Name=CalculatorTest;Server=PC01;Initial catalog=MyDBTest;Integrated Security=true" />
</connectionStrings>
How do I do it from C# Mvc 5?
Right click on your project and add an Entity Data Object, then enter the name you want it to be called; chose code first from database, then enter the server address and you can also select which tables from the db you want.
This will create the connection string for you. I would however recommend doing this in a separate class library so it’s easier to maintain. Note: if you do use it in a separate class library, you will need to look in the Web.config and copy it over to your main application web.config. Also you will want to add the reference of it in your project references.

Call connection string into another connection string in web.config

I have two connections in the web.config basically they are calling the same database. I want to manage this in a better manner because change in one config also needs to change the second connection string as well.
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;persist security info=True;user id=test;password=test123;database=db-AUTH" providerName="System.Data.SqlClient" />
<add name="dbEntities" connectionString="metadata=res://*/InsuranceFinderModel.csdl|res://*/InsuranceFinderModel.ssdl|res://*/InsuranceFinderModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=db-AUTH;persist security info=True;user id=test;password=test123;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
My question is, is there any way i can call the connection string into the other connection string.
For example. default connection connection string into db entities something like
<add name="dbEntities" connectionString="metadata=res://*/InsuranceFinderModel.csdl|res://*/InsuranceFinderModel.ssdl|res://*/InsuranceFinderModel.msl;provider=System.Data.SqlClient;provider connection string= DefaultConnection" providerName="System.Data.EntityClient" />
Any suggestions would be appreciated thanks.
You are not obliged to use connection string defined inside app.config(web.config) file for entity connection. You can change entity connection string at runtime. Read this article for that: http://www.c-sharpcorner.com/UploadFile/dacca2/pass-connection-string-in-run-time-to-entity-framework/ .
Also you can get another connnection string and separete every part of connection string( DataBase,DataSource and etc.) using StringConnectionBuilder class https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.initialcatalog.aspx.
The solution is: Get DefaultConnection string and change entity connection at runtime.

How do I modify my web.config file to support SQL Server 2016?

I'm totally new to C#/.Net development. I've read extensive documentation about the web.config file on msdn and still am unsure what to change.
If answered would you please provide an example of what I should do? I can provide more info if necessary.
You don't have to provide information about SQL Server, You just need to give correct connection string, which points to your database.
Something like:
<connectionStrings>
<add name="myConnectionString"
connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>
<connectionStrings>
<add name="ConnStringDb1" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
This is one way if you are just using some local database.
Otherwise you can set it up like this:
<connectionStrings >
<add
name="myConnectionString"
connectionString="Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;"
providerName="System.Data.SqlClient"/>
And then in your code you should write:
using System.Web.Configuration;
And then at last you can set up your connection variable something like this:
SqlConnection con = new SqlConnection( WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
Are you using the entity framework? Because then it can be generated automatically.

Connection string stored in web.config file doesn't open SQL Server connection

I am trying to learn to use VS2015 and I have seen a number of similar postings on the internet relating to this problem and none of them have gotten me any closer to a solution.
I am trying to write into a SQL Server database and have tested both my database and connection string and confirm they are both working well.
With that out of the way I went into web.config and created a definition for said connection string:
<connectionStrings>
<add name="Name"
connectionString="Data Source=<desktopname>\\SQLEXPRESS01;Integrated Security=SSPI;Initial Catalog=Database1"/>
</connectionStrings>
As far as I can tell this works fine...
However, when I declare the string as follows:
MyConnectionString = ConfigurationManager.ConnectionStrings["Name"].ConnectionString;
Somehow what this returns is not understandable by:
SqlConnection iData = new SqlConnection(myConnectionString);
As when I try and "open" the connection it fails... Despite the exact same connection string works when declared like this within the C# code:
SqlConnection iData = new SqlConnection("Data Source=<desktopname>\\SQLEXPRESS01;Integrated Security=SSPI;Initial Catalog=Database1");
Does anyone have any ideas?
The problem lies with how the information is handled from web.config to the string to the SqlConnection as far as I can tell.
Notably everything runs perfectly unless I use the string as the connection string...
Thanks in advance!
In C# the backslash needs to be doubled, because the compiler treats the backslash as an escape character. And then the \\ compiles to \ in the actual string used at runtime.
In XML that is not the case, it uses a different parser, so in Web.Config you should use a single \:
<add name="Name" connectionString="Data Source=<desktopname>\SQLEXPRESS01;Integrated Security=SSPI;Initial Catalog=Database1"/>
On top of that you may also need to add providerName="System.Data.SqlClient":
<add
name="Name"
connectionString="Data Source=<desktopname>\SQLEXPRESS01;Integrated Security=SSPI;Initial Catalog=Database1"
providerName="System.Data.SqlClient"
/>
Reference: https://msdn.microsoft.com/en-us/library/bf7sd233(v=vs.85).aspx
Try to add : providerName="System.Data.SqlClient" in your "< add name"
<add name ="Name" connectionString="Data Source=<desktopname>\\SQLEXPRESS01;Integrated Security=SSPI;Initial Catalog=Database1" providerName="System.Data.SqlClient"/>

How to set a proper connection string for EF DB First?

I have an ArgumentException : Keyword not supported: 'metadata'.
I tried many things, read many posts. I dont know what to do...
This is my CS :
<add name="CDPContext"
connectionString="metadata=res://*/CDP_Model.csdl|
res://*/CDP_Model.ssdl|
res://*/CDP_Model.msl;
provider=System.Data.SqlClient
provider connection string='Data Source=(LocalDb)\v11.0;
AttachDbFilename=\CDP.mdf;
Initial Catalog=CDP;
Integrated Security=True;
MultipleActiveResultSets=True;
App=EntityFramework'"
providerName="System.Data.SqlClient" />
Thanks.
the connection string you provided above is for model first.
to use connection string for code first you could write your connection string as below
<add name="CDPContext" connectionString="Data Source=(LocalDb)\v11.0;
AttachDbFilename=\CDP.mdf;
Initial Catalog=CDP;integrated security=True;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
and also go to you context, if there is a throw exception in it, just remove it.
providerName="System.Data.SqlClient" is the wrong provider for DB First
instead use providerName="System.Data.EntityClient" to access the db using the EDM...

Categories