none
ADO.NET : Get the table name from sql server for DataTable in DataSet RRS feed

  • Question

  • Hi,

    I have written the program that fills the data set with multiple data tables from sql server, Now whenever I access the Table Name property I get table names as Table 1, Table 2 , Table 3...... etc..

    I need the actual table names from the database server, directly from the query which is fired to load the data table. 

    Any suggestions?? 

    Thanks & Regards

    • Moved by Julie Sheets Thursday, November 21, 2013 2:53 PM More appropriate forum
    Thursday, November 21, 2013 12:14 PM

Answers

  • The name of the tables in the database are not retrived from the query unless you explicitly include them in the select statement(s). You could set the TableName property of the DataTable in the data access layer where you probably know from which table or tables you are selecting from:

    SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        conn.Open();
        string query = "SELECT * FROM [MyTable]";
        SqlCommand cmd = new SqlCommand(query, conn);
    
        DataTable dt = new DataTable();
        dt.TableName = "MyTable"
        dt.Load(cmd.ExecuteReader());
       ....
    

    Thursday, November 21, 2013 4:17 PM
  • Hello,

    You can also have a try with codes like below to specify the actual table name:

    DataSet ds = new DataSet();
    
    
                SqlDataAdapter da1 = new SqlDataAdapter("select * from course", connection);
    
    
                da1.Fill(ds, "Course");
    
    
                SqlDataAdapter da2 = new SqlDataAdapter("select * from [RowVersionTable]", connection);
    
    
                da2.Fill(ds, "RowVersion");
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 22, 2013 3:32 AM
    Moderator

All replies

  • Hi,

    Since this forum is dedicated to questions related to the Microsoft Project product, chances that you will get help here are very slim.

    Greetings,

    Thursday, November 21, 2013 1:15 PM
  • I've moved your post to what I hope is a more appropriate forum.
    Thursday, November 21, 2013 2:53 PM
  • The name of the tables in the database are not retrived from the query unless you explicitly include them in the select statement(s). You could set the TableName property of the DataTable in the data access layer where you probably know from which table or tables you are selecting from:

    SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        conn.Open();
        string query = "SELECT * FROM [MyTable]";
        SqlCommand cmd = new SqlCommand(query, conn);
    
        DataTable dt = new DataTable();
        dt.TableName = "MyTable"
        dt.Load(cmd.ExecuteReader());
       ....
    

    Thursday, November 21, 2013 4:17 PM
  • Hello,

    You can also have a try with codes like below to specify the actual table name:

    DataSet ds = new DataSet();
    
    
                SqlDataAdapter da1 = new SqlDataAdapter("select * from course", connection);
    
    
                da1.Fill(ds, "Course");
    
    
                SqlDataAdapter da2 = new SqlDataAdapter("select * from [RowVersionTable]", connection);
    
    
                da2.Fill(ds, "RowVersion");
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 22, 2013 3:32 AM
    Moderator
  • Thanks for help....

    Regards,

    Clubsharepoint

    Friday, November 22, 2013 4:00 AM
  • It sounds like one or both of these replies worked for you. Please remember to come back and mark one or both as answers (they've so far only been marked as proposed answers).

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, November 24, 2013 5:49 AM
  • Yes use the tableadaptermanager

    http://msdn.microsoft.com/en-us/library/vstudio/bb384426.aspx


    Success
    Cor

    Wednesday, November 27, 2013 12:48 PM