One sql query to return as multiple tables in dataset
I was wondering if I could use one query in SQL Server 2005 to return as a dataset in C #, asp.net 2005 to different tables.
DataSet ds = new DataSet();
ds = new BusinessLogic.BizLogic().getData(int repID);
for(ds != null)
{
txtDate.Text = ds.Tables[2].Rows[0]["Date"].ToString();
}
Looking to figure out how to write a storage routine to have multiple tables. A simple example would be appreciated. Thank!
+3
source to share
2 answers
Yes it is easy, you can do it via proc or via inline sql, but as in the above poster, think about the implications.
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 !
}
}
+2
source to share