Is there a way to name the selected tables from a stored database to identify them in the Dataset?

Asked

Viewed 151 times

5

I have a Store Procedure in SQL Server that projects various data. Example:

CREATE PROCEDURE [dbo].[teste_sp]
AS
BEGIN    

  select * from compra
  select * from cliente
  select * from fatura

END

However, when retrieving Dataset in C#, table names are shown as: "Table1", "Table2" and "Table3".

Is there any way to name these tables dynamically?

  • What do you mean by naming DYNAMICALLY?

  • I can manually set in c# the table name (DataSet.Tables[0].TableName = "Nome"), but I look for a way to name these tables within the database. So I can retrieve the tables by name and not only by index.

  • How do you bring the data to C#?

  • I use using (SqlDataReader reader = ExecuteReader(CommandBehavior.CloseConnection) and load the tables making an iteration: while (!reader.IsClosed) { dt.Load(reader); ds.Tables.Add(dt); }

1 answer

1

You can use sets of datasets or tablemapping

something like that.

SqlDataAdapter da = new SqlDataAdapter(...);
DataSet ds = new DataSet();
DataTableMapping dtm1, dtm2, dtm3;
dtm1 = da.TableMappings.Add("Table", "compra"); 
dtm2 = da.TableMappings.Add("Table1", "cliente");
dtm3 = da.TableMappings.Add("Table2", "fatura");
da.Fill(ds);
  • It still doesn’t solve the problem, because I need it already named SQL.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.