日期:2014-05-18  浏览次数:20431 次

asp.net里面我咋不会遍历数据库了呢?
想几天以前,我驰骋asp,vbscript,table里面,何等的自由!vbscript连接数据库,rs=new adodb.recordset,然后do while not rs1.eof在表格里面一行一行的显示出来出来信息。

最近刚刚开始学习.net。发现.net出来的数据到是非常的方便,跟不用考虑什么表格table子类的东西,只要设置好datasource,有了gridview,datalist,detailsview,formview等等,自动数据全部出来啦!!!

可是,现在我想通过手工的循环把数据库里面的数据遍历一遍,我发现我不会呀!!给我例程序啊。。我不行啦!

------解决方案--------------------
C# code
using System;
using System.Data;
using System.Data.SqlClient;

namespace Microsoft.AdoNet.DataSetDemo
{
    class NorthwindDataSet
    {
        static void Main()
        {
            string connectionString = GetConnectionString();
            ConnectToData(connectionString);
        }

        private static void ConnectToData(string connectionString)
        {
            //Create a SqlConnection to the Northwind database.
            using (SqlConnection connection =
                       new SqlConnection(connectionString))
            {
                //Create a SqlDataAdapter for the Suppliers table.
                SqlDataAdapter adapter = new SqlDataAdapter();

                // A table mapping names the DataTable.
                adapter.TableMappings.Add("Table", "Suppliers");

                // Open the connection.
                connection.Open();
                Console.WriteLine("The SqlConnection is open.");

                // Create a SqlCommand to retrieve Suppliers data.
                SqlCommand command = new SqlCommand(
                    "SELECT SupplierID, CompanyName FROM dbo.Suppliers;",
                    connection);
                command.CommandType = CommandType.Text;

                // Set the SqlDataAdapter's SelectCommand.
                adapter.SelectCommand = command;

                // Fill the DataSet.
                DataSet dataSet = new DataSet("Suppliers");
                adapter.Fill(dataSet);

                // Create a second Adapter and Command to get
                // the Products table, a child table of Suppliers. 
                SqlDataAdapter productsAdapter = new SqlDataAdapter();
                productsAdapter.TableMappings.Add("Table", "Products");

                SqlCommand productsCommand = new SqlCommand(
                    "SELECT ProductID, SupplierID FROM dbo.Products;",
                    connection);
                productsAdapter.SelectCommand = productsCommand;
             
                // Fill the DataSet.
                productsAdapter.Fill(dataSet);
                //放到dataset 以后可以用我说办法
dt =dataset.tables[0] 
for(int i=0;i <dt.rows.count;i++) 
{ 
dr.rows[i][0] 
}
                // Close the connection.
                connection.Close();
                Console.WriteLine("The SqlConnection is closed.");

                 }
        }

        static private string GetConnectionString()
        {
            // To avoid storing the connection string in your code, 
            // you can retrieve it from a configuration file.
            return "Data Source=(local);Initial Catalog=Northwind;"
                + "Integrated Security=SSPI";
        }
    }
}