Click here to Skip to main content
15,897,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using sql server management studio as back end and C#.net as the front end. I want to connect the client systems to the server system to access the database.
Posted

All you have to do is specify the connection string. You can find out the current one for your development database by looking in the Server Explorer pane. If you database is not listed, right click "Data connections" and use "Add Connection" to locate the database. Highlighting the database name in the Server Explorer pane will show the connection string in the Properties pane.

For your client systems, the string will be very similar, but may need a different SQL server instance name.
 
Share this answer
 
Comments
Espen Harlinn 1-Oct-11 6:26am    
5'ed!
1.Add the following namespace

using System.Data.SqlClient;  



2.This code connect to the SQL server instance called "EXPERIENCE" and the database "TEST"

C#
try
           {

               SqlConnection oConn= new  SqlConnection();
               oConn.ConnectionString="Data Source=EXPERIENCE;database=Test;user=sa;password=admin1990";
               oConn.Open();


               MessageBox.Show("Connection Opened");
           }
           catch (Exception ex)
           {

               MessageBox.Show(ex.Message); ;
           }
 
Share this answer
 
Comments
Espen Harlinn 1-Oct-11 6:26am    
5'ed
Bala Selvanayagam 1-Oct-11 8:02am    
thx Espen Harlinn
Hi,
NET Framework offers you 2 possibilities to connect your front-end with the back-end (MSSQL or any other relational database, if having the appropriate connector): ADO.NET connected and disconnected environment.
1. Connected environment relates to the following approach. In order to query the database you'll need the following classes SqlConnection, SqlCommand, SqlDataReader.
First you create a connection to the database:
C#
var connection =  new SqlConnection(connectionString); //connection string to your database

Then define the command for your query (one of the INSERT/DELETE/UPDATE/READ).
C#
SqlCommand sqlCommand =  SqlCommand sqlComm = new SqlCommand("SELECT * FROM Albums")
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         CommandTimeout = 60,
                                         Connection = (SqlConnection) connection
                                     };

Before executing the command you'll need to explicitly open the connection.
C#
sqlCmd.Connection.Open();

And execute the command itself:
C#
var reader = sqlCmd.ExecuteReader();

Once executed, you can iterate through all the records that are returned from the database:

C#
while (reader.Read())
{
     var album = reader[FIELD_NAME];
}

Do not forget to explicitly close the connection, as this resource can be considered very expensive one.

C#
sqlCmd.Connection.Close();

The approach that was described above is called connected environment, because all the database operation are executed with an open connection, thus the results of the operation (E.g. INSERT/UPDATE) will be immediately seen, once the operation ends.

2. The second approach is the disconnected environment, its here where you need define the SqlDataAdapter and DataSet objects.
A DataSet is an in-memory data store that can hold numerous tables (the tables and records are store in RAM, so you dont need permanently open connection to the database, in order to update/insert the data into the DataSet. You can define the changes in the DataSet, and before exiting the application, call the Update method on the SqlDataAdapter in order to make all the changes on the datasource in a bulk). DataSets only hold data and do not interact with a data source. The SqlDataAdapter is the class that manages connections with the data source and gives us disconnected behavior. The SqlDataAdapter opens a connection only when required and closes it as soon as it has performed its task. For example, the SqlDataAdapter performs the following tasks when filling a DataSet with data (the Fill method, that fills the data from the datasource. Note that in order to fill the data DataAdapter will need a valid Connection string and a Command):
a) Open connection b) Get data into DataSet c)Close connection
and performs the following actions when updating data source with DataSet changes (the Update method):
a) Open connection b)Write changes from DataSet to data source c)Close connection

In between the Fill and Update operations, data source connections are closed and you are free to read and write data with the DataSet as you need. These are the mechanics of working with disconnected data. Because the applications holds on to connections only when necessary, the application becomes more scalable. More details on this you can find: here[^]
The choice between disconnected and connected environment really depends upon the task that you need to perform as well as the datasource and the architecture itself.
Kind regards
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900