Ok, let's keep this nice and simple
Before you go anywhere, you are going to need to add a reference to
System.Data.Sqlclient to your code. I'll assume that you're working with C#
in the example code that comes later.
Now, to actually get data out of the database, the first thing that you are
going to need to do is create a connection (since you need to connect, to
grab stuff). That's the job of a SqlConnection object. When you create it
you need to tell it the data source (which computer you want to talk to),
the initial catalog (database) that you want data from, and the security
model you want to use. The last bit can be tricky, but it's not. If you are
using Windows Authentication (i.e the database knows who you are from your
Windows log in information), then the security model is called SSPI.
However, if you need to log into the database, then you need to provide a
user name and password, and that's the method I'll use in the code in a bit.
Having got your connection, you can create a command to get the data. It's
actually a type of object called a SqlCommand, and it does nothing more than
provide you with a way to run commands against the database. In your case,
you want to select everything from the Headlines table in your test database
(so Select * from Headlines). When you create the command, you need to tell
it the SQL statement or stored procedure you want to run, and the connection
you want to use.
So far so good. Next step, open the connection, then tell the command to
ExecuteReader. What this does it run the command give you a Data Reader, a
simple data access object that lets you read the data returned from the
database in a forward only manner.
Now all you need to do is iterate through the rows of the database, and
print the values of each and every column.
When you're done, close the connection down and you're all set.
Here's the code (I'm actually using a table called Customers in the
Northwind database, but the principles are the same )
Bear in mind also that there are simpler ways of doing this (as in less
typing), and I would thoroughly recommend you take a look at a good ADO.NET
book, and also Microsoft's wonderfully useful Data Access Application Block
(search for it on MSDN).
using System;
using System.Data;
using System.Data.SqlClient;
namespace Simplereader
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
SqlConnection myConnection = new SqlConnection (
"Data Source=localhost;Initial Catalog=Northwind;" +
"Integrated Security=False;User ID=sa;Password=NewY0rk" );
SqlCommand myCommand = new SqlCommand(
"SELECT * FROM customers", myConnection );
myConnection.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
while ( myReader.Read() )
{
for ( int i = 0 ; i < myReader.FieldCount ; i ++ )
{
System.Console.Write( myReader.GetValue(i).ToString() + " " );
}
System.Console.WriteLine();
}
myConnection.Close();
System.Console.WriteLine("All done - hit return to exit");
System.Console.ReadLine();
}
}
}
have fun
--
Peter Wright
Author of ADO.NET Novice To Pro, from Apress Inc.
_____________________________