是的,您确实可以这样做...创建一个新类并使其成为 SQLConnection 类型的对象,或者它是。
它的目的应该是创建和维护与数据库的连接并执行各种任务(这部分是可选的,因为您可以直接传入查询)。
从网上得到这个
class DBConnect
{
private MySqlConnection connection;
private string server;
private string database;
private string uid;
private string password;
//Constructor
public DBConnect()
{
Initialize();
}
//Initialize values
private void Initialize(String[] connectionInfo)
{
server = connectionInfo[0];
database = connectionInfo[1];
uid = connectionInfo[2];
password = connectionInfo[3];
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
//open connection to database
private bool OpenConnection()
{
}
//Close connection
private bool CloseConnection()
{
}
//Insert statement
public void Insert()
{
}
//Update statement
public void Update()
{
}
//Delete statement
public void Delete()
{
}
//Select statement
public List <string> [] Select()
{
}
//Count statement
public int Count()
{
}
//Backup
public void Backup()
{
}
//Restore
public void Restore()
{
}
}
所以当你有不同的数据库时,你可以这样:
DBConnect srv001 = new DBConnect({server1,db1,uid1,pw1});
DBConnect srv002 = new DBConnect({server2,db2,uid2,pw2});
所以现在你有 2 个并发连接到 2 个不同的数据库