在我的 Windows 应用程序中从 SQL Server 2008 R2 访问多个数据库 [英] Accessing multiple databases from SQL Server 2008 R2 in my windows application

查看:33
本文介绍了在我的 Windows 应用程序中从 SQL Server 2008 R2 访问多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to access multiple databases from SQL Server 2008 R2 in my windows application?

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

i want to access multible databases from single sqlserver in my windows application.For ex:i want to select student details from DB1 and i want to select Employee details from DB2 both are used in my single windows application ,so what should i do?

推荐答案

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

You'd better try to keep your entities in one database, but in case you can't or don't want to due to a reason, the solution would be to use multiple connection strings in your application.

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

And based on the ADO.Net choice you choose, there can be different ways to achieve so.

这就是我用 Linq-to-Sql

我有两个数据库,每个数据库都有一个表,这是架构:

I have two databases and each has one Table, here's the schema:

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

TeachersDB (first Database):
-Teachers {TeacherID [int], TeacherName[string]}

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

StudentsDB (second Database):
-Students {StudentID [int], TeacherID[int] StudentName[string]}

StudentsDataContext studentsDB = new StudentsDataContext();
TeachersDataContext teachersDB = new TeachersDataContext();

所以每个学生都有一个老师(为了简单起见)

Student st;
Teacher t;

st = (from stu in studentsDB.Students
     where stu.StudentID == int.Parse(txtStudentID.Text)
     select stu).SingleOrDefault<Student>();

t = (from teach in teachersDB.Teachers
    where teach.TeacherID == st.TeacherID
     select teach).SingleOrDefault<Teacher>();

MessageBox.Show(t.TeacherName);

如您所见,我从两个表(每个在一个单独的数据库中)获取数据并将它们保存在内存中(类对象 st 和 t),然后与它们一起工作并找到了学生老师.

as you can see I got data from two tables (each in a seperate database) and saved them in memory (class objects st and t) and then worked with them and found the students teacher.

希望能帮到你.

这篇关于在我的 Windows 应用程序中从 SQL Server 2008 R2 访问多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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