ProviderName for Sql Server .SDF file to use in app.config - c#

I've created a DB file in a project Add -> New Item -> Local Database and created a table for it.
Now I would like to create a connection string for it in app.config.
The problem is that all example code that I find uses code only to create a connection, and not app.config. Hence I can't figure out which providerName I should use.
So what is it?
<add name="DemoDb" providerName="XXXXXX" connectionString="Data Source=ExampleDb.sdf;Persist Security Info=False;"/>

System.Data.SqlServerCE.4.0 or System.Data.SqlServerCe.3.5 depending framework version

Related

How to create a connection string using C# to SQL Server database in Visual Studio 2015

I have a database for user details in my SQL Server and I have already written a C# login application. I need to connect this application to my database to get user data. Before that, I think I should connect my database to Visual Studio using a connection string.
Can anyone please tell me a way of creating a connection string to connect my database to the application?
If you don't have an app.config file just add it by right click your project > add > new item > search box type application then select Application Configuration File then click Add button below. then open the App.config file and paste the code below.
It will be has a name of App.config in your project.
<configuration>
<connectionStrings>
<clear />
<add name="conn" providerName="MySql.Data.MySqlClient" connectionString="data source=localhost;initial catalog=YourDBName;user=YourUsername;password=YourPassword; default command timeout=120;;" />
</connectionStrings>
</configuration>
See this reference to run the code.
Good Luck!
You can use EntityFramework database first convention. The easiest way is to add ADO.NET Entity Data Model from visual studio Project -> Add New Item -> Data and then wizard helps you to connect to your database and creates required objects for tables in code

Changing Lightswitch database connection at run time

I am looking to make it possible for my application to change it's connection string at runtime. I have a connection string in the Web.Config file, but when I update the config file and restart the application, the connection string does not update. Also, I do not seem to be able to use the configuration manager from within my application. Is changing the connection string in the Web.Config file the best way to go about making this a dynamic connection?
<add name="486f1ab5-d3c4-4fc5-805b-0afbcf0fa46b" connectionString="Data Source=.\MyServer;Initial Catalog=Mydatabase;Integrated Security=True" />
Here is a blog post that goes over what you need to do:
LightSwitch Dynamic Connection Strings Now Supported

Code-First Migrations for multiple databases?

I have the following connection string:
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
<add name="MyContext" connectionString="metadata=res://*;provider=System.Data.SqlClient;provider connection string='data source=SQLSERVERDB;initial catalog=TestDB_CodeFirst;user id=***;password=***;MultipleActiveResultSets=True;App=EntityFramework'" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
When I try to enable migrations I first get a warning:
Cannot determine a valid start-up project. Using project 'MyApp.Model' instead.
Your configuration file and working directory may not be set as expected.
Use the -StartUpProjectName parameter to set one explicitly.
Then I get this exception:
Argument 'xmlReader' is not valid. A minimum of one .ssdl artifact must be supplied.
Is the connection string wrong and why should I need ssdl if I'm using Code First?
NOTE
My context is in MyApp.Model project where my Migrations folder should be located.
I don't have connection strings in my main startup project because connection strings are retrieved from a second database and the user can select one of them when logging in to the application.
I have just one connection string shown above in my MyApp.Model project which points to my development database.
Also, my second question is:
If I use CF migrations, will all databases be migrated each time a user selects a different database for the first time?
EDIT
I changed the connection as mentioned below, and I get the following exception:
The item with identity 'table1' already exists in the metadata collection.
Parameter name: item
It must be noted that I reverse-engineered an existing database. So I don't know what possibly went wrong!
I've also deleted the Migrations folder and checked the database but there is no migration_history table created.
You are trying to use a connectionString designed to work with Database First / Model First. You can tell because your providerName is System.Data.EntityClient instead of System.Data.SqlClient.
Your connection string should look like this:
<connectionStrings>
<add name="MyContext"
connectionString="Data Source=SQLSERVERDB; Initial Catalog=TestDB_CodeFirst;user id=***;password=***;"
providerName="System.Data.SqlClient" />
</connectionStrings
Although I would suggest using Integrated Security instead of a user/password. Just personal preference, though.

adding an exsiting Microsoft sql database to an asp.net project

I have been doing C# asp.net exercises for a while and ussually I would add an Access Database to the application Data folder of my project and then with my OleDbConnection string connect to the database and interact with the tables.
I however want to now add an existing Microsoft sql 2008 express edition database to my asp.net project(visual studio 2012) but am struggling to do this as it is not a case of simply adding the databse to the application data folder and making a connection using the connection string necessary.
What would the walkthrough or step by step procedure be for doing this?
What would the walkthrough or step by step procedure be for doing this?
Install SQL Server (Full Edition) if not already installed (could be installed on the same machine as the web application or on a separate machine).
Create a database inside this SQL Server.
Create the tables inside the newly created database.
Specify the connection string to this SQL server.
You could of course use an embedded database. For example VS 2012 comes with a LocalDB which stores the files inside the App_Data folder. When you create a new ASP.NET MVC 4 application using the internet template it will set everything up for you. But that's not a production ready database. Basically it sets a connection string pointing to the App_Data folder:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcApplication1-20130107093649;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MvcApplication1-20130107093649.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
and setting entity framework to use this provider:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
So when you are ready to deploy your application to production and have a running SQL Server instance all you need to do is change the connection string to point to this SQL instance.
Here's a nice article on MSDN with various connection strings that you could use based on the target database.
You don't have to work with files if you have an existing MSSQL database. Simply add your connection string and you are ready to use. You have 2 options to include connection string, you can include it in web.config or you can include it to your cs file directly.
If you choose to include it to your cs file you should use SqlConnection class. In web.config, it should look something like this:
<connectionStrings>
<add name="constr" connectionString="Data Source=yourservername;Initial Catalog=yourdatabasename;User ID=youruserid;Password=yourpass" providerName="System.Data.SqlClient"/>
</connectionStrings>

How to specify a custom folder path in which EF code-first approach create a database?

By default, EF code first will create a database in
C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA
Is it possible to change this default path to my own path?
Try following connection string.
<add name="default" connectionString="Data Source=.\sqlexpress;Initial catalog=TestDb;AttachDbFilename=c:\MyDatabase.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
Note that folder where you want to put your database files must be created manualy (SQL server will not do it for you).
Hope this helps.
As far as I can tell, the only way you can change the destination folder is if you are using SQL Compact Edition (SQLCE) and the SqlCeConnectionFactory factory.
It takes the destination path from the connection string (or uses |DataDirectory| by default), which is currently only supported with SQLCE.
Best I could find was this link http://blogs.msdn.com/b/adonet/archive/2010/09/02/ef-feature-ctp4-dbcontext-and-databases.aspx which applies to the CTP4 (not the latest version).
I couldn't find any way to change the output path even with web/app.config changes.

Categories

Resources