I would like to know where to name my database inside a connection string, like this one:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Initial Catalog=MyDatabase;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
For what I've reading, the name should be placed next to |DataDirectory|, but the fact is that I've deleted the App_Data folder, because I don't want to have a folder with such a name in my project, so the file .mdf won't be create, and I neither want to be.
What I want is that when going to the Server Explorer view inside Visual Studio Express 2013 for Web I could see the database with the name I've selected above, whichever it is.
ADDITIONAL:
I would like to know what are the pros/cons of not having an App_Data folder.
Thank you for your help.
There is no pros/cons. Visual Studio creates temporary Database inside App_Data so that you can test it locally. I prefer hosting Database outside of project (it is just my person preference).
Once the application is ready for production. We need to host the database in separate SQL Server. For example,
<add name="ConnectionString"
connectionString="Server=tcp:database.windows.net,1433;Database=MyDatabase;User ID=TheUsername;Password=ThePassword;Trusted_Connection=False;Encrypt=True;" />
Here are SQL Server connection strings.
Related
I am developing a Desktop Application in C#. An I want to use MSSQL Server Database in it through Entity Framework. I want to know that how can I make app-config file automatically detect server name in Connection String tag. As when I will deploy my application through a setup. It will install on another machine. It may have different server name. So I want my app-config to automatically detect the server name available on that machine. I knew there was a way but I have forgotten it. Please help me here. Thanks
You don't necessarily have to. You can refer to a local database like this:
.\SQLEXPRESS
The whole connection string:
<connectionStrings>
<add name="ActiveConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=NameOfTheDatabase;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
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
I am quite new in programming, I managed to make a programme in Visual Studio 2013, with an SQL DB. I published the project well, and even copied the DB to the bin/debug file. All is well but my programme cannot connect to the DB, is there a step am missing during publishing?
Keep Database in App_Data folder and follow following step than you can use following approach:
<add name="MyConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Book.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
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>
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.