使用同一服务器的两个数据库? [英] Use two databases from same server?
问题描述
我在同一台服务器上有两个SQl服务器数据库(客户,行情)。我正在构建一个winform,它有几个文本框,组合框,用于将数据输入到引用数据库的复选框,它还有一个文本框,用于在客户(Id)数据库中搜索客户名称。一旦我找到了特定的客户,我想为该客户添加一个新创建的报价(CustomerId)。
我的问题我怎么能把这两个结合在一起,使用sql或实体框架。
编辑1:
我想尝试做的一个例子。
输入报价数据:
产品:鞋子
数量:20
搜索客户数据库........ Joe Bloggs Id 123
找到客户;
在单独的数据库中将客户ID和报价信息插入报价表。
我是什么尝试过:
我能够查询客户数据库以搜索客户。并且能够将数据插入到报价数据库中。
Hi,
I have two SQl Server Databases(Customer, Quotes) on the same server. I am building a winform that has several textboxes,comboboxes, checkboxes that is used to enter data into the quote db, It also has one textbox that is used to search the Customer(Id) db for a customer name. Once i find the particular customer i would like to add a newly created Quotes(CustomerId) against that Customer.
My question how could i marry the two together, using sql or entity framework.
EDIT 1:
An example of what im trying to do.
Enter Quote Data:
Product : Shoes
Quantity : 20
Search Customer db ........ Joe Bloggs Id 123
Found Customer ;
Insert Customer id and Quote info into Quote table on seperate db.
What I have tried:
Im able to query the Customer db to search for a customer.And seperately im able to insert data into Quotes db.
推荐答案
只需创建两个连接字符串即可。
例如:
It's easy just create two connection strings.
example:
public int GetId(){
using (SqlConnection con = new SqlConnection("CONNECTION STRING TO CUSTOMER DB")
{
//logic to get id
}
}
public void Save(int customerId, object Quote){
using (SqlConnection con = new SqlConnection("CONNECTION STRING TO QUOTE DB")
{
//logic to save quote with customer id
}
}
如果你需要在交易中这样做我建议你阅读一些关于它的文章。
了解ADO.NET中的事务和TransactionScope的初学者教程 [ ^ ]
您对分布式交易感兴趣。
If you need to do it in transaction I recommend you to read some articles about that.
A Beginner's Tutorial for Understanding Transactions and TransactionScope in ADO.NET[^]
You are interested in distributed transactions.
这篇关于使用同一服务器的两个数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!