adding an exsiting Microsoft sql database to an asp.net project - c#

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>

Related

Automatically Detect Server Name of Machine in App.config

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>

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

Name a database through the connection string (App_Data deleted)

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.

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

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

Where are the logins in my MVC C # program?

Has made the following guide: http://www.asp.net/mvc/tutorials/getting-started-with-mvc3-part1-cs
It automatically creates options for users to register and login. Where are they stored? Can not find it in the database. How do I check if someone is logged in?
It stores the information in a SQL Compact 4.0 database.
By default the ASP.NET MVC X Web Application project template uses the ASP.NET Membership Provider to store and manage the users which according to the article are stored in a SQL Compact 4.0 database. But if I recall correctly there isn't an officially supported membership provider for this database.
Sure you could write your own or use a third party solution such as:
http://sqlcemembership.codeplex.com/
However, if I download the code for the guide and check out the Web.config I notice that the SqlMembershipProvider is used and this is linked to the connection string called "ApplicationServices".
<add name="AspNetSqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="ApplicationServices"
... />
Let's take a look at that connection string:
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated
Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;
User Instance=true"
providerName="System.Data.SqlClient" />
Apparently its stored in a local database file called "aspnetdb.mdf" which is probably located in your web application's App_Data folder.
That is where you can find the user data. But is it actually used in this guide? Appears not, might just be a remnant of the default ASP.NET MVC Web Application project template.
The movie-related data is stored in an SQL Compact 4.0 database. A different connection string is used for it.
<add name="MovieDBContext"
connectionString="Data Source=|DataDirectory|Movies.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
Also a local database file, but this time it is a different provider (SqlServerCe.4.0. instead of SqlClient).

Categories

Resources