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>
Related
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
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.
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>
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
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).