C# 在我的windows应用程序中从SQL Server 2008 R2访问多个数据库

C# 在我的windows应用程序中从SQL Server 2008 R2访问多个数据库,c#,sql-server-2008-r2,C#,Sql Server 2008 R2,如何在我的windows应用程序中从SQL Server 2008 R2访问多个数据库 我想在我的windows应用程序中从单个sqlserver访问多个数据库。例如:我想从DB1中选择学生详细信息,我想从DB2中选择员工详细信息,这两个都在我的单个windows应用程序中使用,那么我该怎么办呢?为此,您可以使用不同的连接字符串,将初始目录设置为不同的数据库。 在代码中硬编码,它们看起来像: SqlConnection conn1 = new SqlConnection("Data Source

如何在我的windows应用程序中从SQL Server 2008 R2访问多个数据库


我想在我的windows应用程序中从单个sqlserver访问多个数据库。例如:我想从DB1中选择学生详细信息,我想从DB2中选择员工详细信息,这两个都在我的单个windows应用程序中使用,那么我该怎么办呢?

为此,您可以使用不同的
连接字符串
,将
初始目录
设置为不同的数据库。
在代码中硬编码,它们看起来像:

SqlConnection conn1 = new SqlConnection("Data Source=YourServer;Initial Catalog=Db1;User   Id=user;Password=pass;")
SqlConnection conn2 = new SqlConnection("Data Source=YourServer;Initial Catalog=Db2;User Id=user;Password=pass;")
并对每个特定查询使用connectionstring。您还可以将它们添加到
app.config
web.config


或者在
connectionstring
中未指定
初始目录时,在查询顶部添加
USE MyDbName
,即
USE MyDbName SELECT*FROM MyTable
在这种情况下,您将对两个数据库使用相同的connectionstring。

您最好尝试将实体保留在一个数据库中,但如果由于某种原因您不能或不想这样做,解决方案是在应用程序中使用多个连接字符串

根据您选择的ADO.Net选项,可以有不同的方法来实现

编辑:这就是我如何使用
linqtosql

我有两个数据库,每个数据库有一个表,下面是模式:

教师数据库(第一个数据库):
-教师{TeacherID[int],TeacherName[string]}

学生数据库(第二个数据库):
-学生{StudentID[int],TeacherID[int]StudentName[string]}

StudentsDataContext studentsDB = new StudentsDataContext();
TeachersDataContext teachersDB = new TeachersDataContext();
所以每个学生都有一个老师(为了简单起见)

Student-st;
教师t;
st=(来自stu in studentsDB.Students
其中stu.StudentID==int.Parse(txtStudentID.Text)
选择stu.SingleOrDefault();
t=(来自教师授课B.教师
其中teach.TeacherID==st.TeacherID
选择teach).SingleOrDefault();
MessageBox.Show(t.TeacherName);
正如您所见,我从两个表(每个表在一个单独的数据库中)中获取数据,并将它们保存在内存中(类对象st和t),然后使用它们找到了学生老师


希望有帮助。

您需要2个连接字符串, 根据您的需要将其发送到该方法,并将网格/数据连接到任何需要的地方

using(SqlConnection connection = new SqlConnection(<connstring>))
{
connection.Open();

SqlCommand command = new SqlCommand();
command.Connection = connection;

command.CommandText = <query1>;
using(SqlDataReader reader = command.ExecuteReader())
{

DataGrid1.DataSource = reader;
DataGrid1.DataBind();
}

command.CommandText = <query2>;
using(SqlDataReader reader = command.ExecuteReader())
{

DataGrid2.DataSource = reader;
DataGrid2.DataBind();
}
}
使用(SqlConnection=newsqlconnection())
{
connection.Open();
SqlCommand=newsqlcommand();
command.Connection=连接;
command.CommandText=;
使用(SqlDataReader=command.ExecuteReader())
{
DataGrid1.DataSource=读取器;
DataGrid1.DataBind();
}
command.CommandText=;
使用(SqlDataReader=command.ExecuteReader())
{
DataGrid2.DataSource=读取器;
DataGrid2.DataBind();
}
}

创建App.Config文件,并在appsettings中添加以下内容:

<appSettings>
    <add key="appDSN" value="data source=SERVER-NAME;initial catalog=StudentDB;integrated security=SSPI;persist security info=False;packet size=4096" />
    <add key="appDSN2" value="data source=SERVER-NAME2;initial catalog=EmpolyeeDB;integrated security=SSPI;persist security info=False;packet size=4096" />
</appSettings>

这是使用sqlreader的简单而古老的方法。使用mahdi-tahsildari方式(Linq到SQL)在今天更为常见。

您需要更清楚地了解您的场景。您希望如何以及从何处访问这些数据库?请澄清您的问题,这没有任何意义。@sampath,为什么以及在哪里访问多个数据库?先生,请用代码plzsir告诉我详细信息。实际上,我想使用configmanager,因此,通过详细解释,以这种方式解决我的问题。谢谢advance@sampath为您添加了连接字符串的示例。根据您在问题中提供的信息,我恐怕无法回答更具体的问题。谢谢先生………使用(SqlConnection conn1=new-SqlConnection(“FirstConnectionString”){DataTable dataFromA=//来自SELECT id的结果,来自TableA的名称}使用(SqlConnection conn2=new-SqlConnection(“SecondConnectionString”)){DataTable dataFromB=//来自SELECT id的结果,来自table b的名称}是的,这是一种方法,但是您需要将数据存储在某个地方(比如数据集)使用数据。@sampath如果您现在明白我的意思,您可以在任何ado.net情况下使用它,无论您是否使用DataAdapter,您都可以填充不同的对象并使用它们。这里什么是appDSN1?am nt gettingappDSN1指的是appsetting。您可以这样使用它:使用(SqlConnection conn1=newsqlconnection(ConfigurationSettings.AppSettings[“appDSN1”]){DataTable dataFromA=SELECT id的结果,table a的名称}使用(SqlConnection conn2=newsqlconnection(ConfigurationSettings.AppSettings[“appDSN2”]){DataTable dataFromB=SELECT id的结果,table b的名称}您可以获取appsetting密钥(项目的app.config中的密钥名称)哪个是您的连接字符串。连接字符串将被SQLConnection使用。使用一次第一个键和一次第二个键,您可以选择访问两个数据库。本例中的连接信息存储在应用程序键的值中。非常感谢每一个。如果您发布代码、XML或数据示例,请突出显示这些文本编辑器中的行,然后单击编辑器工具栏上的“代码示例”按钮(
{}
),以很好地格式化和语法高亮显示它!
<appSettings>
    <add key="appDSN" value="data source=SERVER-NAME;initial catalog=StudentDB;integrated security=SSPI;persist security info=False;packet size=4096" />
    <add key="appDSN2" value="data source=SERVER-NAME2;initial catalog=EmpolyeeDB;integrated security=SSPI;persist security info=False;packet size=4096" />
</appSettings>
//
// In a using statement, acquire the SqlConnection as a resource.
//
using (SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["appDSN1"]))
{
    //
    // Open the SqlConnection.
        //
    con.Open();
    //
    // The following code shows how you can use an SqlCommand based on the SqlConnection.
    //
    using (SqlCommand command = new SqlCommand("SELECT * FROM Students", con))
    using (SqlDataReader reader = command.ExecuteReader())
    {
    while (reader.Read())
    {
          //read through the first database
    }
    }
}

//
// In a using statement, acquire the SqlConnection as a resource.
//
using (SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["appDSN2"]))
{
    //
    // Open the SqlConnection.
        //
    con.Open();
    //
    // The following code shows how you can use an SqlCommand based on the SqlConnection.
    //
    using (SqlCommand command = new SqlCommand("SELECT * FROM employees", con))
    using (SqlDataReader reader = command.ExecuteReader())
    {
    while (reader.Read())
    {
          //read through the second database
    }
    }