将客户端系统连接到服务器系统以访问数据库. [英] connect the client systems to the server system to access the database.

查看:90
本文介绍了将客户端系统连接到服务器系统以访问数据库.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用sql server management studio作为后端,并使用C#.net作为前端.我想将客户端系统连接到服务器系统以访问数据库.

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.

推荐答案

您要做的就是指定连接字符串.您可以通过在服务器资源管理器"窗格中查找来为您的开发数据库找到当前的数据库.如果数据库未列出,请右键单击数据连接",然后使用添加连接"找到数据库.在服务器资源管理器"窗格中突出显示数据库名称将在属性"窗格中显示连接字符串.

对于您的客户端系统,该字符串将非常相似,但是可能需要不同的SQL Server实例名称.
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.


1.添加以下名称空间

1.Add the following namespace

using System.Data.SqlClient;  




2.此代码连接到名为"EXPERIENCE"的SQL Server实例和"TEST"数据库




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

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); ;
           }



NET Framework为您提供了两种将前端与后端(MSSQL或任何其他关系数据库,如果具有适当的连接器)连接的可能性:ADO.NET已连接和已断开连接的环境.
1. 连通环境与以下方法有关.为了查询数据库,您将需要以下类SqlConnection, SqlCommand, SqlDataReader.
首先,您创建与数据库的连接:
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:
var connection =  new SqlConnection(connectionString); //connection string to your database


然后为您的查询定义命令(INSERT/DELETE/UPDATE/READ之一).


Then define the command for your query (one of the INSERT/DELETE/UPDATE/READ).

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.

sqlCmd.Connection.Open();


并执行命令本身:


And execute the command itself:

var reader = sqlCmd.ExecuteReader();


一旦执行,就可以遍历数据库返回的所有记录:


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

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.

sqlCmd.Connection.Close();


上面描述的方法称为连接环境,因为所有数据库操作都是通过打开的连接执行的,因此一旦操作结束,将立即看到操作的结果(例如INSERT/UPDATE).

2.第二种方法是断开连接环境,在这里需要定义SqlDataAdapterDataSet对象.
DataSet是一种内存中数据存储,可以容纳多个表(表和记录存储在RAM中,因此您无需永久打开与数据库的连接即可将数据更新/插入到DataSet您可以在DataSet中定义更改,然后在退出应用程序之前,调用SqlDataAdapter 上的Update 方法,以便对数据源进行所有更改(批量). DataSets 仅保存数据,并且不与数据源进行交互. SqlDataAdapter 是管理与数据源的连接并为我们提供断开连接行为的类. SqlDataAdapter 仅在需要时打开连接,并在执行任务后立即将其关闭.例如,SqlDataAdapter 在用数据填充DataSet 时执行以下任务(Fill 方法从数据源填充数据.请注意,为了填充数据DataAdapter 需要有效的连接字符串和Command):
a)打开连接b)将数据放入DataSet c)关闭连接
并在使用DataSet 更改(Update 方法)更新数据源时执行以下操作:
a)打开连接b)将更改从DataSet写入数据源c)关闭连接

Fill Update 操作之间,数据源连接已关闭,您可以根据需要自由使用DataSet 读写数据.这些是处理断开数据的机制.由于应用程序仅在必要时才保持连接,因此该应用程序具有更高的可伸缩性.您可以在此找到更多详细信息:此处 [


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


这篇关于将客户端系统连接到服务器系统以访问数据库.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆