Connecting to SQL Server Database using a Hosted Web Application - c#

I have created a .net web application using c# that inserts, updates, and deletes data from an SQL server database. I have tested it using my localhost and it works fine. Now I would like to publish this application on a hosted site.
My question is, does the database need to be on the same server/host that the application is in for it to connect?
Does anyone have any tips on how to implement this?

No you do not have to run the Database on the same server as the application. For scalability it can be best practice to separate the two, one dedicated server for your DB and one server to host all your websites.
In your application you specify the connection to your Database. On your Database server you can also then set restrictions on the IP to access this data if you really wanted for further security.

If your Question is 'How to connect database present on another server', then you have to Add IP address of it in the Web.config file as:
<connectionStrings>
<add name="myConnectionString" connectionString="data source=myserver\SQLEXPRESS;initial catalog=myDatabase;uid=myUserName;password=myPassword;" />
</connectionStrings>
data source can be IP ADDRESS or PC NAME, where Database is stored.

does the database need to be on the same server/host that the application is in for it to connect?
It's upto you to put the Database on the same machine or a different machine.You can choose following scenarios
If it's a simple website and you only have one Machine,then you can install Database on the same server(or if'ts already present) .In this case the connection string may or may not have to change depending on what type of connection string you use(e.g if your sql server instance name is different,connection string will change)
Your website can get complex or process large amount of data,it makes sense to use a dedicated database server .This case connection string will change
Does anyone have any tips on how to implement this?
Easiest way to deal with this is using web.config transformation.All you have to do is create a new Web.Release.Config
Please refer this answer for more details How do I use Web.Config transform on my connection strings?

Related

moving from local database to remote. What do I need?

I've got some desktop experience, but am (brand) new to web programming. I've built a well-received C# WPF desktop app that stores data in a local (on user's desktop) SqlLite DB. I'd like to transition the app to remote data storage, probably with a MS SQL Server DB, hosted by a web-host service provider. One database there would hold all the various users data, access controlled by their own username/password.
In fact I've already done that as an experiment, and it functions. My concern is security: at the moment my in-code connection string just uses my db account/password. I'm not such a newb to know that's not a good idea. There must be a standard way to move that private information out of the code and into a sort of relay between the app code and the db. But I don't know the terminology, or what to ask for, despite a day of googling. So:
(1) User requests data save, say
(2) App sends SQL statement and user credentials to relay.
(3) Relay checks credentials against db records (using my db credentials, but that's ok, they're at least not stored in the apps's source code)
(4) Assuming ok, forward sql statement to db.
Is (something like) this a thing? What's it called? Or is there some other standard way to achieve the goal of keep my connection string completely out of the code? Where do I begin reading about how to implement it? How would I know if my web-host would support such a thing?
From the point of view of web-app operations, your connection string, from your dotnet app to your RDBMS server, is considered a secret. That means it's data you retrieve from a configuration file, and is never checked in to your git or other source control system. That connection string contains your RDBMS username and password, along with stuff like the name of your database and the server where it runs.
(If you did check in an RDBMS connection string to source control for any machine other than localhost, change that password. Do it now. Cybercreeps troll github looking for connection strings to steal and use for nefarious purposes. )
Dotnet web apps have a configuration file. It's an XML file called web.config. Connection strings go into that file in an XML stanza looking like this
<configuration>
<connectionStrings>
<add name="Name"
providerName="System.Data.ProviderName"
<!-- When deploying to production,
replace this connection string with one
to connect the production data base. -->
connectionString="Valid Connection String;" />
</connectionStrings>
</configuration>
Here's some info about retrieving that kind of connection string from your dotnet program..
I've had good luck putting a globally useless locally useful localhost connection string in that file, with some xml comments explaining that it needs to be edited when putting the web app on a public server. My example shows such comments.
It's also possible to edit a connection string with the web server's IIS Manager app. This setup -- either web.config or IIS Manager -- has good security.
The scheme you outlined is more complex than you need unless, heaven forbid, every one of your customers has a different connection string.

Search for SQL Server database installed on machine

I'm deploying a wpf application and I'd like to know how is the best way to connect to a database.
For example after I install my application I need to set a connection string to connect with a SQL Server Express.
I read a thread that told it's possible to find SQL Servers installed on a machine, so after I find it, how do I connect it programmatically?
An another thing I thought is, there is a file which I can read an write a connection string as web.config in asp.net?
I know that there is an app.config file but after I deploy it I guess it became inaccessible!
Nothing wrong with putting SQL Server, especially Express, on the same machine if it's a smallish application. There's also the 'localdb' feature.
Anyway, this bit of code will find all instances of SQL Server on your network. You can then build a window to display the results, let the user pick a server, and construct a connectionstring from the results. Note that firewalls etc on the server can get in the way of this process - typically you get the server name returned but no other info. You can fix this by adding exceptions to the firewall.
private DataTable FindServers()
{
System.Data.Sql.SqlDataSourceEnumerator instance = System.Data.Sql.SqlDataSourceEnumerator.Instance;
DataTable dt = instance.GetDataSources();
return dt;
}
These days database layer should be separated from application by DataService or MicroService. Which would be the data access layer.
If you dont need that level of separation and still thinking about application that connects to database directly, I would suggest to use configuration to store connection strings as you probably like to cover scenario where database is not located on same server as WPF application.
The configuration file for non-web application is called app.configand is exactly the same as web.config for web applications.
Problem with auto-finding installed sql server instaces is simple: what if there are two instances installed on machine where you start your app? How the app will know which to use?

How can I run C# app which contains local SQL Server database on another computer?

I have created a C# program with a SQL Server database. It works fine on my computer but on my friend's PC it doesn't (my friend doesn't have SQL Sever 2008). Is it possible to make it without any installation? And if it can, how can it be done?
This is my connection string:
connectionString="Data Source=\v11.0;AttachDbFilename=|DataDirectory|\MainDatabase.mdf;Integrated Security=True"
SQL Server is for server databases. You can change your project to use SQL Server CE (SQL Server Compact Edition) which is a single-file local database. It is very similar to the "true" SQL Server so it may be the easiest solution. Your code probably wont change except for the connection string.
Use the IP address of your local machine in which the database is present;
1433 is the default port number. Then modify the connection string accordingly:
connectionString="Data Source=190.190.200.100,1433;
Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;"
First time you open port 1433 in firewall if you don't need you can turn off firewall.
go to Run=>cmd=>Ipconfig find network's card activity. Show like
Like my computer have just wireless and my andress in private lan is:192.168.100.165
And now you change connectstring in web.config.
connectionString="Data Source=192.168.100.165;
Initial Catalog=yourDataBase;User ID=yourUsername;Password=yourPassword;"
If you don't know "yourUsername" and "yourpassword" please refer link
and create username and password in MsSQL
https://msdn.microsoft.com/en-us/library/aa337562.aspx
And if you want to connect database from internet you need to do open port your's router
If you want to run your program without the SQL server installed you have to use a service based sql database.see this image
you can add a local database through Visual Studio (project--> Add new item)
My solution will cater if you need a SQL database on your friend's computer instead of hosting from your own.
The cheapest way to handle a database locally on any device using a proper database is to convert to SQLite. This is a local device alternative that is more lightweight and doesn't require any user to install a SQL server at all.
There are also alternatives like writing information to a hidden file (usually in binary if you don't want your application to be hacked).
In short, only consider a SQL database IF you are hosting the database, otherwise use alternatives.
As far as I know, you have one of 2 options. You either have a server (a pc set up to be a server and containing this database) or you could take the easier option and host your database on the cloud. Many websites offer a free service to host your database for a limited amount of time, or limited storage. If you have Azure subscription that would definitely be the way to go.
If your connection string is like below
connectionString="Data Source=\v11.0;AttachDbFilename=|DataDirectory|\MainDatabase.mdf;Integrated Security=True"
then you should install sql localdb 2012.msi on your friend's PC.
And make sure the .mdf file is located the same as on your computer.

make a single PC application work on the network

I have a working WPF application that uses EntityFramework 6.0 to connect to a SQL Server database. Below is how my source code is structured.
TestPacks.Data contains the model of the database and logic to interact with the database. While TestPacks.Entities contains the mapped entities.
Now, I need to make this Application work on the local network so that multiple users can see the same data simultanously.
Here is how I plan to go ahead.
a) install the database on a server machine
b) change the below connection string to reference the database from the server instead of the local machine.
<connectionStrings>
<add name="TestPacksContext" connectionString="metadata=res://*/TestPacksModel.csdl|res://*/TestPacksModel.ssdl|res://*/TestPacksModel.msl;provider=System.Data.SqlClient;provider connection string="data source=WASIM-DELL;initial catalog=TPM;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
Is there anything else I will require to do for my application to work correctly? My requirements says that about 10-25 people (max) might work on the application over the network.
Please suggest.
You need to do concurrency check, Refer this sites Entity Framework Update check if the record has changed "http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application"

App.config.exe connectionstring tricks

I'm trying to develop multi client in C# with SQL Server 2008 data Base, after made setup file and install it to client I cannot access DataBase in server. I used LINQ to Connect DataBase and have App.config xml file in my project which ConnectionString Declare there and I after installing the application I change that XML file's ConnectionString to right way(Server name and sql instance name), but this change doesn't have any impact, whats the problem maybe have my solution?
If any one has experience about developing multi client application share with me.
and also How can I change scope in project properties from Application to User, It's may be solve my problem
Thank you
In this project we use LINQ to connect Database to server, and we can get right Database address dynamically from User Interface and use it in constructor of DataContext:
public DataClasses1DataContext(string connection)
And then it don't care in embed connection string, its use only that connectionString in their constructor.
If the error mentions "provider: Named Pipes Provider", then your application is trying to connect via named pipes to your SQL Server, is that what you want? You can prefix the server name with tcp: to force TCP/IP usage instead i.e. Server=tcp:ServerName if you wish to use TCP.
If you want to use named pipes it is possible that support for it is turned off on the server, I believe it is disabled by default. You would need to enable it via the SQL Server configuration manager program on the server, in the network configuration, protocols section.
hmmm, hard to say without the connection strings or error message, but some ideas....
When you change the app.config, restart app to verify changes.
Try pinging the server from the where the app is being installed.
Check firewall settings. shared network.
Use your new connection string on your development machine on the same network if local.

Categories

Resources