简体   繁体   English

单个sql查询作为数据集中的多个表返回

[英]A single sql query to return as multiple tables in a dataset

I was wondering if i can use a single query in sql server 2005 to return as a dataset in c#, asp.net 2005 into different tables. 我想知道我是否可以在sql server 2005中使用单个查询作为c#,asp.net 2005中的数据集返回到不同的表中。

DataSet ds = new DataSet();
ds = new BusinessLogic.BizLogic().getData(int repID);
for(ds != null)
{
    txtDate.Text = ds.Tables[2].Rows[0]["Date"].ToString();
}

Want to figure out, how to write the store procedure to have multiple tables.A simple example would be appreciated. 想弄清楚,如何编写存储过程以拥有多个表。一个简单的例子将不胜感激。 Thank you! 谢谢!

You absolutely can, although you may want to consider the overall goal and implications of considering this design. 你绝对可以,尽管你可能想要考虑考虑这个设计的总体目标和影响。

In your stored procedure you just simply have multiple selects ala.. 在您的存储过程中,您只需要多个选择ala ..

select xxx, yyy from table1
select zzz, nnn from table2

Along those lines. 沿着这些路线。

Yes its easy, you can do it via a proc or via inline sql, but like the above poster said, consider the implications. 是的,它很容易,你可以通过proc或内联sql来做,但像上面的海报所说,考虑其含义。

 using (var sqlConn = new SqlConnection("Server=localhost;Database=Test;Integrated Security=SSPI"))
            {
                sqlConn.Open();
                string sql = "Select * From table1; Select * From table2;";
                using (var sqlCmd = new SqlCommand(sql, sqlConn))
                {
                    var da = new SqlDataAdapter(sqlCmd);
                    var ds = new DataSet();
                    da.Fill(ds);
                    Console.WriteLine(ds.Tables.Count); // Will show 2 !

                }

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM