Connecting to a SQL Server 2005 (Express Edition) database

S

Sherwood

Greetings,

I am using Visual C#.NET 2005 (Express Edtion) and want to retrieve data
from a SQL Server 2005 (Express Edition) database. However, I see that there
is no SQL Data Adapter control nor a SQL Connection control. What controls
should I use instead?

Thanks in advance!
 
M

Mudassar Hassan

You can connect to sql server using the code.

1. First create connection object and give it a connectionstring

System.Data.SqlClient.SqlConnection con;
con.ConnectionString = "Data Source=yourservername;Initial
Catalog=yourdbname;User ID=sa";
con.Open();

2. Now you can use retrieve data from database using sqldataReader object or
SQLDataAdapter Object through SQLCOmmand like

System.Data.SqlClient.SqlCommand sql;
sql.CommandText = "SELECT * FROM emp";
sql.Connection = con;

string empname;
System.Data.SqlClient.SqlDataReader rd;
rd = sql.ExecuteReader;
while (rd.Read=true)
{
empname = rd("columnname");
}
rd.Close;
 
S

Sherwood

Thanks. However, I tried compiling this code and received the following error:

"Cannot convert method group 'ExecuteReader' to non-delegate type
'System.Data.SqlClient.SqlDataReader'. Did you intend to invoke the method?"

This error occurs on the following line of code:
rd = sql.ExecuteReader;

Any ideas?

Thanks!
 
C

Chris Jobson

Thanks. However, I tried compiling this code and received the following
error:

"Cannot convert method group 'ExecuteReader' to non-delegate type
'System.Data.SqlClient.SqlDataReader'. Did you intend to invoke the
method?"

This error occurs on the following line of code:
rd = sql.ExecuteReader;

I suspect the line should be:
rd = sql.ExecuteReader();

Chris Jobson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top