views:

142

answers:

4

I have been working in a business writing advanced software apps, and obviously im provided with access to our SQL server and all the connection strings needed.This is fine for my job now - but what if i wanted to do this for a new (very small) business... If i wanted to purchase a small database server and set up a piece of software that talks to the databases on this server, how would i go about a) Talking and connecting to the server in code (c#) and b)What would i need regarding things like internet/phone connections etc to make this possible.

Edit: the reason it would need a server is because it would need to be accessed from 2 or 3 different computers in different locations?

+1  A: 

Hi

In case your application is small (by small I mean the usage of resources like CPU and memory) then your SQL Server can reside on the same box.

Else you need to have a separate server box for your database and connect to that from your application. In this case, preferably your database box and application box would be on the local area network.

Check this link for having a connection to SQL Server from C# code - http://www.codeproject.com/KB/database/sql_in_csharp.aspx

cheers

Andriyev
good internet website link. its helpful.
djangofan
+1  A: 

You should probably expose your database with an xml web services layer, so that your architecture will be scalable. The general idea is host your sql server and webservices, using Native SQL Server XML Web Services you can make this available to your remote clients. When in your clients you simply add a service reference in Visual Studio and your data will now be available in your client app.

Hope this helps some.

Cheers

mirezus
+2  A: 

Actually there are quite a few ways to create a database connection, but I would say one of the easiest ways is to utilize the methods and classes found in System.Data.SQLClient. A basic connection would look something like the following:

using System.Data.SQLClient;

namespace YourNamespace
{
    public class DatabaseConnect
    {
         public DataType getData()
         {
            DataType dataObj = new DataType();

            SqlConnection testConn = new SqlConnection("connection string here");
            SqlCommand testCommand = new SqlCommand("select * from dataTable", testConn);
            testConn.Open()
             using (SqlDataReader reader = testCommand.ExecuteReader())
            {
                while (reader.Read())
                {
                   //Get data from reader and set into DataType object
                }
            }

            return dataObj;
         }
    }
}

Keep in mind, this is a very, very simple version of a connection for reading data, but it should give you an idea of what you need to do. Make sure to use a "using" or "try/catch" statement to ensure that the connection is closed and resources are freed after each use (whether it successfully gets data or not).

As for your other question about what equipment you may require. In the beginning I would suggest just creating the database on your local machine and running tests from there. Once you are confident with trading data back and forth, feel free to move the database to another box or an online server. Any internet connection type should suffice, though I can't vouch for dial-up, haven't used it in years.

One final note, if you do happen to decide to move to an online server system, make sure that the service you use allows for outside connections. Certain services use shared server systems, and force users to use their internal database interfaces to manage and write to the database.

--- EDIT ---

As for the server system itself, build up a separate box on your local network that you can see, and load up the database software of your choice. Since you are using C#, it would probably be easiest to go with Microsoft SQL Server 2005 / 2008. The installation is rather straightforward, and it will prompt you to automatically create your first database while installing.

After installation it will be up to you to add in the tables, stored procedures, custom functions, etc... Once your base structure is created, go ahead and use the above code to make some simple connections. Since you are familiar with the above practices then I'm sure you know that all you really need to do is target the server machine and database in the connection string to be on your way.

Zensar
The above code is very familiar to me, i do data access a lot at work to their local servers. I am more interested in your second point about connecting to an online server or to another box that is not local to this machine! i have no idea where to start with this, but as i said the above kind of data access is second nature to me!
xoxo
Oh ok! I misunderstood there for a second. Many people I know use some sort of database facade, so they never really learn to code the initial connections.
Zensar
Building from what you are saying about putting a seperate machine on the local network...what about if the machines are in different locations in the country and want to access a shared database??
xoxo
A: 

You may find the connectionstrings website useful - worth bookmarking.

Paul Owens