标题:c#与access连接的问题
只看楼主
sgrrzbc
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-8-14
结帖率:66.67%
已结贴  问题点数:20 回复次数:3 
c#与access连接的问题
using System;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace DataSet
{
    class Program
    {
        static void Main(string[] args)
        {  // Creat conection object for Microsoft Access OLE DB Provider;
            //note @ sign prefacing string literal so backslashes in path name;
            //work
            OleDbConnection thisConnection = new OleDbConnection(
                @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:\c#教程\协和医院.mdb");
            // Creat DataAdapter object
            OleDbDataAdapter thisAdapter = new OleDbDataAdapter(
                "SELECT ID, 字段1,字段2,字段3,字段4,字段5,字段6,字段7,字段8  FROM Sheet1", thisConnection);
            // Create DataSet to contain related data tables,rows and columns
            System.Data.DataSet thisDataSet = new System.Data.DataSet();
            //Fill DataSet using query defined previously for DataAdapter
            thisAdapter.Fill(thisDataSet, "Sheet1" );
            foreach (DataRow theRow in thisDataSet.Tables["Sheet1 "].Rows)
            {
                Console.WriteLine(theRow["ID"] + "\t" + theRow["字段1"] + "\t" +
                    theRow[" 字段2"] + "\t" + theRow["字段3"] + "\t" +
                    theRow["字段4"] + "\t" +
                    theRow[" 字段5"] + "\t" +
                    theRow[" 字段6"] + "\t" +
                    theRow[" 字段7"] + "\t" +
                    theRow[" 字段8"]);
            }
            thisConnection.Close();
            Console.Write("Program finished,press Enter /Return to continue:");
            Console.ReadLine();
        }

        }
    }



这句thisDataSet.Tables["Sheet1 "].Rows  未将对象引用设置到对象的实例,不知道怎么改,希望大家帮忙啊,谢谢
搜索更多相关主题的帖子: access 
2010-08-14 23:14
zhoufeng1988
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:北京
等 级:贵宾
威 望:27
帖 子:1432
专家分:6329
注 册:2009-5-31
得分:20 
thisAdapter.Fill(thisDataSet, "Sheet1" );,这里面这个表的名称是"Sheet1",
而thisDataSet.Tables["Sheet1 "],表的名称后面多加了一个空格?
您把空格去掉一下,看一下是不是这个原因导致的。
2010-08-14 23:36
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
得分:0 
thisDataSet.Tables["Sheet1 "].Rows  未将对象引用设置到对象的实例

支持ls
也就是没有这个表名.

C#超级群 74862681,欢迎大家的到来!
2010-08-15 08:26



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-315724-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.052385 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved