Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# -> Retrieving dataset from SQL Server 2008

Tags:

c#

.net

sql

asp.net

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?

like image 407
user559142 Avatar asked Feb 16 '26 13:02

user559142


1 Answers

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);
 }  
like image 136
Amen Ayach Avatar answered Feb 18 '26 03:02

Amen Ayach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!