SqlDataAdapter.SelectCommand
using System;
using System.Data;
using System.Data.SqlClient;
class SelectIntoDataSet {
public static void Main() {
string connectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
SqlConnection mySqlConnection = new SqlConnection(connectionString);
string selectString = "SELECT TOP 10 ID, FirstName, LastName FROM Employee ORDER BY ID";
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText = selectString;
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
DataSet myDataSet = new DataSet();
mySqlConnection.Open();
Console.WriteLine("Retrieving rows from the Employee table");
mySqlDataAdapter.Fill(myDataSet, "Employee");
mySqlConnection.Close();
DataTable myDataTable = myDataSet.Tables["Employee"];
foreach (DataRow myDataRow in myDataTable.Rows)
{
Console.WriteLine("ID = "+ myDataRow["ID"]);
Console.WriteLine("FirstName = "+ myDataRow["FirstName"]);
Console.WriteLine("LastName = "+ myDataRow["LastName"]);
}
}
}
Related examples in the same category