在代码中使用System.Data.OleDb命名空间时找不到OleDbConnection [英] OleDbConnection not found while using System.Data.OleDb namespace in code

查看:161
本文介绍了在代码中使用System.Data.OleDb命名空间时找不到OleDbConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了另一个问题,其中没有 OldeDb 命名空间.但是我仍然有问题

i have read the other question with the same one without the OldeDb namespace in it. But I am still having a problem

我正在创建UWP应用,我想将数据从Excel文件上传到DataGridView.

I am creating a UWP app, and I want to upload a data from an Excel file to a DataGridView.

这是我的代码

这是我的参考代码

using System.Collections.Generic;
using Windows.UI.Xaml.Controls;
using System.Data;
using Microsoft.Toolkit.Uwp.UI.Controls;
using System;
using System.IO;
using System.Data.OleDb

然后这是我上传excel文件的代码

then this is my code on uploading my excel file

String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fullDirectory + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";

OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select * From [" + "2018" + "$]", con);

这是我的错误代码

错误CS0246类型或名称空间名称'OleDbCommand'不能为找到(您是否缺少using指令或程序集引用?)

Error CS0246 The type or namespace name 'OleDbCommand' could not be found (are you missing a using directive or an assembly reference?)

任何想法为什么会这样?谢谢

Any ideas why this is occurring? Thanks

完整代码

using System.Collections.Generic;
using Windows.UI.Xaml.Controls;
using System.Data;
using Microsoft.Toolkit.Uwp.UI.Controls;
using System;
using System.IO;
using System.Data.OleDb;

namespace App
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void SaveButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {

        }

        private void CancelButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {

        }

        private void AppBarButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            if (this.Frame.CanGoBack)
            {
                this.Frame.GoBack();
            }
        }

        private async void BtnOpenAttendanceFileDialog_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            var picker = new Windows.Storage.Pickers.FileOpenPicker();
            picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.List;
            picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
            picker.FileTypeFilter.Clear();
            picker.FileTypeFilter.Add(".xls");
            picker.FileTypeFilter.Add(".xlsx");
            picker.FileTypeFilter.Add(".dat");
            picker.FileTypeFilter.Add(".csv");


            Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
            if (file != null)
            {
                this.txtFileLocation.Text = file.Path;
            }
            else
            {

            }
        }

        private void BtnLoadFile_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            string fileDirectory = Path.GetDirectoryName(txtFileLocation.Text.Trim());
            string fileName = Path.GetDirectoryName(txtFileLocation.Text.Trim());
            string fullDirectory = txtFileLocation.Text.Trim();

            String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fullDirectory + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";

            OleDbConnection con = new OleDbConnection(constr);
            OleDbCommand oconn = new OleDbCommand("Select * From [" + "2018" + "$]", con);


        }
    }
}

推荐答案

经过一些挖掘(可以在评论中找到),我发现您只能在UWP应用程序中使用一小部分.NET.

After some digging (which can be found in the comments) I've found that you can only use a small subset of .NET in a UWP application.

创建一个简单的控制台应用程序将验证您的Visual Studio安装是否存在问题.例如:

Creating a simple console-application will verify that there isn't a problem with your Visual Studio installation. Eg:

namespace App
{
    static void Main(string[] args)
    {
         using(var conn = new System.Data.OleDb.OleDbConnection{connString = "..info.."})
        {
             conn.Open();
             Console.WriteLine("DS:{0} DB: {1}",conn.DataSource,conn.Database);   
        }
    }
}

仅是UWP不支持 OleDbConnection .请在此处上查看更多详细信息.另外,请查看: UWP的API ,以验证您使用的是正确的版本.

Its simply that UWP does not support OleDbConnection. Please see more details here on what you can use. Also, have a look at: API's of UWP to verify that you are on the correct version.

此外,此错误可能是由于对 System.Data 有多个引用变体而导致其他所有人都在此答案中绊脚石.

Also, this error can be caused by having multiple reference variants to System.Data for everyone else stumbling across this answer.

这篇关于在代码中使用System.Data.OleDb命名空间时找不到OleDbConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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