illustrates how to perform a SELECT statement using ADO.NET
2006-02-21 10:12
441 查看
using System;
using System.Data;
using System.Data.SqlClient;
public class Example23_1
{
public static void Main()
{
// step 1: formulate a string containing the details of the
// database connection
string connectionString =
"server=localhost;database=Northwind;uid=sa;pwd=sa";
// step 2: create a SqlConnection object to connect to the
// database, passing the connection string to the constructor
SqlConnection mySqlConnection =
new SqlConnection(connectionString);
// step 3: formulate a SELECT statement to retrieve the
// CustomerID, CompanyName, ContactName, and Address
// columns for the first ten rows from the Customers table
string selectString =
"SELECT CustomerID, CompanyName, ContactName, Address " +
"FROM Customers " +
"WHERE CustomerID < 'BSBEV'";
// step 4: create a SqlCommand object to hold the SELECT statement
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
// step 5: set the CommandText property of the SqlCommand object to
// the SELECT string
mySqlCommand.CommandText = selectString;
// step 6: create a SqlDataAdapter object
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
// step 7: set the SelectCommand property of the SqlAdapter object
// to the SqlCommand object
mySqlDataAdapter.SelectCommand = mySqlCommand;
// step 8: create a DataSet object to store the results of
// the SELECT statement
DataSet myDataSet = new DataSet();
// step 9: open the database connection using the
// Open() method of the SqlConnection object
mySqlConnection.Open();
// step 10: use the Fill() method of the SqlDataAdapter object to
// retrieve the rows from the table, storing the rows locally
// in a DataTable of the DataSet object
Console.WriteLine("Retrieving rows from the Customers table");
string dataTableName = "Customers";
mySqlDataAdapter.Fill(myDataSet, dataTableName);
// step 11: get the DataTable object from the DataSet object
DataTable myDataTable = myDataSet.Tables[dataTableName];
// step 12: display the columns for each row in the DataTable,
// using a DataRow object to access each row in the DataTable
foreach (DataRow myDataRow in myDataTable.Rows)
{
Console.WriteLine("CustomerID = " + myDataRow["CustomerID"]);
Console.WriteLine("CompanyName = " + myDataRow["CompanyName"]);
Console.WriteLine("ContactName = " + myDataRow["ContactName"]);
Console.WriteLine("Address = " + myDataRow["Address"]);
}
// step 13: close the database connection using the Close() method
// of the SqlConnection object created in Step 2
mySqlConnection.Close();
}
}
using System.Data;
using System.Data.SqlClient;
public class Example23_1
{
public static void Main()
{
// step 1: formulate a string containing the details of the
// database connection
string connectionString =
"server=localhost;database=Northwind;uid=sa;pwd=sa";
// step 2: create a SqlConnection object to connect to the
// database, passing the connection string to the constructor
SqlConnection mySqlConnection =
new SqlConnection(connectionString);
// step 3: formulate a SELECT statement to retrieve the
// CustomerID, CompanyName, ContactName, and Address
// columns for the first ten rows from the Customers table
string selectString =
"SELECT CustomerID, CompanyName, ContactName, Address " +
"FROM Customers " +
"WHERE CustomerID < 'BSBEV'";
// step 4: create a SqlCommand object to hold the SELECT statement
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
// step 5: set the CommandText property of the SqlCommand object to
// the SELECT string
mySqlCommand.CommandText = selectString;
// step 6: create a SqlDataAdapter object
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
// step 7: set the SelectCommand property of the SqlAdapter object
// to the SqlCommand object
mySqlDataAdapter.SelectCommand = mySqlCommand;
// step 8: create a DataSet object to store the results of
// the SELECT statement
DataSet myDataSet = new DataSet();
// step 9: open the database connection using the
// Open() method of the SqlConnection object
mySqlConnection.Open();
// step 10: use the Fill() method of the SqlDataAdapter object to
// retrieve the rows from the table, storing the rows locally
// in a DataTable of the DataSet object
Console.WriteLine("Retrieving rows from the Customers table");
string dataTableName = "Customers";
mySqlDataAdapter.Fill(myDataSet, dataTableName);
// step 11: get the DataTable object from the DataSet object
DataTable myDataTable = myDataSet.Tables[dataTableName];
// step 12: display the columns for each row in the DataTable,
// using a DataRow object to access each row in the DataTable
foreach (DataRow myDataRow in myDataTable.Rows)
{
Console.WriteLine("CustomerID = " + myDataRow["CustomerID"]);
Console.WriteLine("CompanyName = " + myDataRow["CompanyName"]);
Console.WriteLine("ContactName = " + myDataRow["ContactName"]);
Console.WriteLine("Address = " + myDataRow["Address"]);
}
// step 13: close the database connection using the Close() method
// of the SqlConnection object created in Step 2
mySqlConnection.Close();
}
}
相关文章推荐
- How to read and save Images in a Sql Server Database using ADO.NET and C#
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- How to perform AD(MS Active Directory) Authentication using JNDI from Java?
- csharp:Learn how to post JSON string to generic Handler using jQuery in ASP.Net C
- HOW TO: Upload a File to a Web Server in ASP.NET by Using Visual Basic .NET
- How to transfer data to an Excel workbook by using Visual C# 2005 or Visual C# .NET
- How to use an ASP.NET application to query an Indexing Service catalog by using Visual Basic .NET
- how to listen to N channels? (dynamic select statement)
- How To Display Hierarchical Data by Using Nested Repeater Controls and Visual C# .NET(ZT)
- csharp:Learn how to post JSON string to generic Handler using jQuery in ASP.Net
- How to access database using ADO in VB6 - 在 VB6 中用ADO对象如何访问数据库
- How to send raw data to a printer by using Visual C# .NET
- 如何将 ADO.NET Framework 整合进 Spring.NET 中 (How to integrate ADO.NET Entity Framework into Spring.NET)
- 如何将 ADO.NET Framework 整合进 Spring.NET 中 (How to integrate ADO.NET Entity Framework into Spring.NET)
- How to call an Oracle Stored Procedure that returns one or more REF CURSORS, using ADO from C++ How
- How to generate an RSS feed for your web site using ASP.NET
- HOW-TO: Handling cookies using the java.net.* API
- How to generate an RSS feed for your web site using ASP.NET
- How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With Visual Basic .NET(利用Ado.net导出到Excel)