问题
How to access multiple databases from SQL Server 2008 R2 in my windows application?
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?
回答1:
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.
And based on the ADO.Net choice you choose, there can be different ways to achieve so.
Edit: This is how I did it with Linq-to-Sql
I have two databases and each has one Table, here's the schema:
TeachersDB (first Database):
-Teachers {TeacherID [int], TeacherName[string]}
StudentsDB (second Database):
-Students {StudentID [int], TeacherID[int] StudentName[string]}
StudentsDataContext studentsDB = new StudentsDataContext();
TeachersDataContext teachersDB = new TeachersDataContext();
so each student has one teacher (for the sake of simplicity)
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);
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.
I hope it helps.
回答2:
For this you could use different connectionstrings in which you set the initial catalog to the different db's.
Hardcoded in the code they will look like:
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;")
and use the connectionstring for each specific query. Those you can add as well into your app.config or web.config.
Or add a USE MyDbName on top of your query when there's no Initial Catalog specified in your connectionstring ie USE MyDbName SELECT * FROM MyTable In this case you'll be using the same connectionstring for both databases.
回答3:
Create an App.Config file and add the following in the 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>
The key is the name you give to your setting, the value is in this case the connectionstring
and use the following when connecting to retrieve data from both databases:
//
// 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
}
}
This is the easy and old way using sqlreader. Using mahdi tahsildari way (Linq to SQL) is more of today.
回答4:
You will need 2 connection strings, sent it based on your need to the method and hook up your grid/data anywhere whereever its needed.
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();
}
}
来源:https://stackoverflow.com/questions/14055314/accessing-multiple-databases-from-sql-server-2008-r2-in-my-windows-application