如何将程序连接到另一台PC的数据库?

时间:2021-07-17 20:44:47

I'm making a program with c# , and after finishing it I'll setup the program in more than one pc , I want to connect the program in all PCs to one database in another pc using sql server . how can I do it ?

我正在使用c#制作一个程序,在完成它之后,我将在多台PC上设置程序,我想使用sql server将所有PC中的程序连接到另一台PC中的一个数据库。我该怎么做 ?

2 个解决方案

#1


4  

Configure the connection string in your program to point at the database server.

在程序中配置连接字符串以指向数据库服务器。

An example from http://www.connectionstrings.com:

来自http://www.connectionstrings.com的一个例子:

Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

Data Source = myServerAddress; Initial Catalog = myDataBase; User Id = myUsername; Password = myPassword;

#2


1  

One example would be setting up an MySQL-Database at on of you PCs. You can get it from the MySQL Website. After that you need to connect to it in any way. To connect to that database you may use the .NET Connector. Using it you can access a remote databasein a fashion like this:

一个例子是在您的PC上建立MySQL数据库。你可以从MySQL网站上获得它。之后,您需要以任何方式连接到它。要连接到该数据库,您可以使用.NET Connector。使用它,您可以像这样的方式访问远程数据库:

using MySql.Data.MySqlClient;
...

string myConnectionString = "SERVER=localhost;" +
                            "DATABASE=mydatabase;" +
                            "UID=user;" +
                            "PASSWORD=mypassword;";

MySqlConnection connection = new MySqlConnection(myConnectionString);
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT * FROM mytable";
MySqlDataReader Reader;
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{
    string row = "";
    for (int i = 0; i < Reader.FieldCount; i++)
        row += Reader.GetValue(i).ToString() + ", ";
    Console.WriteLine(row);
}
connection.Close();
...

In a similiar way you may

你可以用一种类似的方式

#1


4  

Configure the connection string in your program to point at the database server.

在程序中配置连接字符串以指向数据库服务器。

An example from http://www.connectionstrings.com:

来自http://www.connectionstrings.com的一个例子:

Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

Data Source = myServerAddress; Initial Catalog = myDataBase; User Id = myUsername; Password = myPassword;

#2


1  

One example would be setting up an MySQL-Database at on of you PCs. You can get it from the MySQL Website. After that you need to connect to it in any way. To connect to that database you may use the .NET Connector. Using it you can access a remote databasein a fashion like this:

一个例子是在您的PC上建立MySQL数据库。你可以从MySQL网站上获得它。之后,您需要以任何方式连接到它。要连接到该数据库,您可以使用.NET Connector。使用它,您可以像这样的方式访问远程数据库:

using MySql.Data.MySqlClient;
...

string myConnectionString = "SERVER=localhost;" +
                            "DATABASE=mydatabase;" +
                            "UID=user;" +
                            "PASSWORD=mypassword;";

MySqlConnection connection = new MySqlConnection(myConnectionString);
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT * FROM mytable";
MySqlDataReader Reader;
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{
    string row = "";
    for (int i = 0; i < Reader.FieldCount; i++)
        row += Reader.GetValue(i).ToString() + ", ";
    Console.WriteLine(row);
}
connection.Close();
...

In a similiar way you may

你可以用一种类似的方式