如何在控制台上查看适配器中的数据 [英] How to see the data in adapter on console

查看:86
本文介绍了如何在控制台上查看适配器中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我使用以下代码查看控制台上的适配器值。



Hi,

I am using the following code to see the adapter values on console.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Collections;

namespace ConsoleApplication36
{
    class Program
    {

        public static OleDbDataAdapter CreateCustomerAdapter(OleDbConnection connection)
        {
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            OleDbCommand command;

            // Create the SelectCommand.
            command = new OleDbCommand("SELECT * FROM Customers " +
                "WHERE Country = ? AND City = ?", connection);

            command.Parameters.Add("Country", OleDbType.VarChar, 15);
            command.Parameters.Add("City", OleDbType.VarChar, 15);

            adapter.SelectCommand = command;

            // Create the InsertCommand.
            command = new OleDbCommand(
                "INSERT INTO Customers (CustomerID, CompanyName) " +
                "VALUES (?, ?)", connection);

            command.Parameters.Add(
                "CustomerID", OleDbType.Char, 5, "CustomerID");
            command.Parameters.Add(
                "CompanyName", OleDbType.VarChar, 40, "CompanyName");

            adapter.InsertCommand = command;
            return adapter;
        }


        static void Main()
        {
            var p = new Program();
            p.Bar();
        }

        void Bar()
        {
            OleDbConnection connection = null;
            //DataSet ds = new DataSet();
            OleDbDataAdapter adapter1 = new OleDbDataAdapter();

            adapter1 = CreateCustomerAdapter(connection);
            Console.WriteLine(adapter1);
            Console.ReadKey();

        }
    }
}





输出:

System.Data.Oledb.OledbDataAdapter



如何查看适配器内的数据



Output:
System.Data.Oledb.OledbDataAdapter

How can I see data inside the adapter

推荐答案

更改您的代码:



Change your code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Collections;
 
namespace ConsoleApplication36
{
    class Program
    {
 
        public static OleDbDataAdapter CreateCustomerAdapter(OleDbConnection connection)
        {
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            OleDbCommand command;
 
            // Create the SelectCommand.
            command = new OleDbCommand("SELECT * FROM Customers " +
                "WHERE Country = ? AND City = ?", connection);
 
            command.Parameters.Add("Country", OleDbType.VarChar, 15);
            command.Parameters.Add("City", OleDbType.VarChar, 15);
 
            adapter.SelectCommand = command;
 
            // Create the InsertCommand.
            command = new OleDbCommand(
                "INSERT INTO Customers (CustomerID, CompanyName) " +
                "VALUES (?, ?)", connection);
 
            command.Parameters.Add(
                "CustomerID", OleDbType.Char, 5, "CustomerID");
            command.Parameters.Add(
                "CompanyName", OleDbType.VarChar, 40, "CompanyName");
 
            adapter.InsertCommand = command;
            return adapter;
        }
 

        static void Main()
        {
            var p = new Program();
            p.Bar();
        }
 
        void Bar()
        {
	    DataTable t = new DataTable();
            OleDbConnection connection = null;
            //DataSet ds = new DataSet();
            OleDbDataAdapter adapter1 = new OleDbDataAdapter();
 
            adapter1 = CreateCustomerAdapter(connection);
	    adapter1.Fill(t);
            PrintDataTable(t);
            Console.ReadKey();
 
        }
        void PrintDataTable(DataTable dt)
        {
		foreach (DataRow row in dt.Rows)
	{
     	Console.WriteLine();
     		for(int x = 0; x < dt.Columns.Count; x++)
     		{
          	Console.Write(row[x].ToString() + " | ");
     		}
	}
        }
    }
}


这篇关于如何在控制台上查看适配器中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆