none
Get dataset from database! RRS feed

  • Question

  • I wrote this block of code...

    private DataSet GetDSFromDB()
            {
                1 DataSet ds = new DataSet();
                2 SqlConnection conn = new SqlConnection("server=localhost; database=hiretakers; trusted_connection=true");
               
                4 SqlCommand customCmd = new SqlCommand("select * from hiretakers.customers;", conn);
                5 SqlCommand discCmd = new SqlCommand("select * from hiretakers.discs ", conn);
               
                7 SqlDataAdapter customAdapter = new SqlDataAdapter(customCmd);
                8 SqlDataAdapter discAdapter = new SqlDataAdapter(discCmd);
               
              10 customAdapter.Fill(ds,"customers");
              11 discAdapter.Fill(ds, "discs");

              13 return ds;
            }

    but i get this error:  Invalid object name "customers"(10 line);

                                 Invalid object name "discs"(11 line);

    what is wrong?

    Friday, January 7, 2011 10:46 AM

Answers

  • Do you have multiple instance of server in your localhost? if so... specify the connection string:

    new SqlConnection("Data Source=Server\Instance;Initial Catalog=hiretakers;Integrated Security=SSPI"

    the string Catalog=hiretakers is basicly your database

    in your SqlCommand... specify the table (discs)... and not database.table (hiretakers.discs) as in

    SqlCommand discCmd = new SqlCommand("select * from discs"conn);
    • Proposed as answer by Jackie-SunModerator Wednesday, January 12, 2011 2:23 AM
    • Marked as answer by artxach Friday, January 14, 2011 4:45 PM
    Friday, January 7, 2011 8:56 PM
  • Actually, I think your connection string is probably ok. But your select statement should be either:

    select * from hiretakers.dbo.discs

    --or

    select * from hiretakers..discs  -- notice the two dots, eliminating the dbo

    --or, most simply

    select * from discs -- as Ronin suggested, & I recommend also


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Proposed as answer by Jackie-SunModerator Wednesday, January 12, 2011 2:23 AM
    • Marked as answer by artxach Friday, January 14, 2011 4:45 PM
    Saturday, January 8, 2011 5:01 PM

All replies

  • Do you have multiple instance of server in your localhost? if so... specify the connection string:

    new SqlConnection("Data Source=Server\Instance;Initial Catalog=hiretakers;Integrated Security=SSPI"

    the string Catalog=hiretakers is basicly your database

    in your SqlCommand... specify the table (discs)... and not database.table (hiretakers.discs) as in

    SqlCommand discCmd = new SqlCommand("select * from discs"conn);
    • Proposed as answer by Jackie-SunModerator Wednesday, January 12, 2011 2:23 AM
    • Marked as answer by artxach Friday, January 14, 2011 4:45 PM
    Friday, January 7, 2011 8:56 PM
  • Actually, I think your connection string is probably ok. But your select statement should be either:

    select * from hiretakers.dbo.discs

    --or

    select * from hiretakers..discs  -- notice the two dots, eliminating the dbo

    --or, most simply

    select * from discs -- as Ronin suggested, & I recommend also


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Proposed as answer by Jackie-SunModerator Wednesday, January 12, 2011 2:23 AM
    • Marked as answer by artxach Friday, January 14, 2011 4:45 PM
    Saturday, January 8, 2011 5:01 PM