好吧,我正在尝试做一些好事(对我来说很好,对你们来说很简单),我被告知我可以做到,但我不知道从哪里开始。
我在一个页面中有两个 DDL,我需要在 page_load 上填充这两个 DDL,每个 DDL 从不同的表中获取数据,它们之间没有关系(供应商/类别)。我知道如何使用两个数据库连接来做到这一点,这很简单,但我被告知我可以用一个连接来做到这一点。
我没有被告知这只是统一的连接,还是 SP 处理一个 SP 中的两个表(对我来说,我只能用一个 SP 来完成这件事似乎不合逻辑……但我知道什么。 。哈哈)
谢谢,
埃雷兹
Well, i am tring to do something nice (nice for me, simple for you guys), i was told i can do it, but i have no idea where to start.
I have two DDL's in a page, i need on page_load to popolate both, each one gets data from deferent table with no relaition between them (suppliers/catagories). i know how to do it with two DB connections, that is easy, but i was told that i can do it with one connection.
I was not told if it is only the connection that is united or also that SP deal with both tables in one SP (doesn't seem logical to me that i can do it with only one SP...but what do i know..lol)
thanks,
Erez
发布评论
评论(3)
您可以在 SP 中运行这两个查询:
your_sp
然后在 C# 端,打开数据读取器,然后可以使用 reader.NextResult() 方法移至结果集中的下一个结果。
You can run both the queries in the SP:
your_sp
Then on C# side, you open the data reader and you can use the reader.NextResult() method to move to the next result in the result set.
我认为您可以用分号分隔 SQL 语句。
例如
从供应商中选择myColumns; SELECT otherColumns FROM Categories
您可以以常规方式打开数据读取器。
读取完第一个结果集的所有数据后,您可以调用
NextResult
它将尝试执行下一条语句,并为您提供第二个结果集的阅读器。注意:我没有这样做。但这是我可以从文档中得出的结论。
I think you could separate your SQL statements by a semicolon.
e.g.
SELECT myColumns FROM Suppliers; SELECT otherColumns FROM Categories
You could open the datareader in a regular way.
Once you are done reading all the data for 1st resultset, you could make a call to
NextResult
which will try to execute the next statement and will get you the reader for 2nd resultset.Note: I have not done this. But this is what I can make out of the documentation.
不完全是我这样做的方式(我使用 2 个对象数据源);但如果您确实只想使用 1,请执行以下操作:
创建一个包含 2 个 select 语句的 sql 语句;并将其加载到 C# 数据集中。
将您的第一个 ddl 绑定到 DataSet.Tables[0]。
将第二个 ddl 绑定到 DataSet.Tables[1]。
就这样吧。 1 个连接。
编辑:如果您确实想使用 DataReader...
您可能需要 2 个 SELECT 语句,并带有一个附加字段来区分您要插入的 DDL。所以;像这样的东西:
SELECT 'Table1' AS TableName, Name, Value
FROM dbo.Table1
UNION
SELECT 'Table2' AS 表名称、名称、值
FROM dbo.Table2
,然后以任何将项目加载到 DDL 中的方法,检查表名称以查看将其添加到哪个表中
Not exactly the way I'd do it (i'd use 2 object data sources); but if you really only want to use 1, do this:
Create one sql statement that contains 2 select statements; and load that into a C# DataSet.
Bind your first ddl to DataSet.Tables[0].
Bind your second ddl to DataSet.Tables[1].
There ya go. 1 connection.
EDIT: If you really want to use a DataReader...
You'd probably need 2 SELECT statements, with an additional field to distinguish which DDL you're inserting into. So; something like this:
SELECT 'Table1' AS TableName, Name, Value
FROM dbo.Table1
UNION
SELECT 'Table2' AS TableName, Name, Value
FROM dbo.Table2
and then in whatever method you're using to load items into your DDL's, check the table name to see which one to add it into