user559142
user559142

Reputation: 12517

C# -> Retrieving dataset from SQL Server 2008

I have a table called NAMES in my SQL Server database. I am trying to retrieve the entire table and put it into a dataset:

//get the connection string from web.config
string connString = ConfigurationManager.ConnectionStrings["Platform"].ConnectionString;
DataSet dataset = new DataSet();

using (SqlConnection conn = new SqlConnection(connString))
{
    SqlDataAdapter adapter = new SqlDataAdapter();                
    adapter.SelectCommand = new SqlCommand("NAMES", conn);
    adapter.Fill(dataset);
}  

This throws a sql exception though,

"Invalid Object Name NAMES"...

What am I doing wrong?

Upvotes: 2

Views: 17169

Answers (3)

Satinder singh
Satinder singh

Reputation: 10198

First check whether Select * from dbo.[Names] is wroking in ur sql ?

     string connString = ConfigurationManager .ConnectionStrings["Platform"].ConnectionString;
        Dataset ds=new Dataset();
        SqlConnection con = new Sqlconnection(connString);
        SqlDataAdapter adapter = new SqlDataAdapter("Select * from dbo.[Names]",con);
        adapter.Fill(ds);

Upvotes: 0

Amen Ayach
Amen Ayach

Reputation: 4348

Open the connection !!!!!!

 //get the connection string from web.config
 string connString = ConfigurationManager .ConnectionStrings["Platform"].ConnectionString;
 DataSet dataset = new DataSet();

 using (SqlConnection conn = new SqlConnection(connString))
 {
     SqlDataAdapter adapter = new SqlDataAdapter();                
     adapter.SelectCommand = new SqlCommand("select * from [NAMES]", conn);
     conn.Open(); 
     adapter.Fill(dataset);
 }  

Upvotes: 7

ChrisBD
ChrisBD

Reputation: 9209

You're not passing an actual SQL select command to the SqlCommand constructor.

Upvotes: 7

Related Questions