简体   繁体   English

在代码中使用System.Data.OleDb命名空间时找不到OleDbConnection

[英]OleDbConnection not found while using System.Data.OleDb namespace in code

i have read the other question with the same one without the OldeDb namespace in it. 我已经阅读了另一个问题,其中没有OldeDb命名空间。 But I am still having a problem 但是我还是有问题

I am creating a UWP app, and I want to upload a data from an Excel file to a DataGridView. 我正在创建一个UWP应用,我想将数据从Excel文件上传到DataGridView。

So this is my code 这是我的代码

This is my reference code 这是我的参考代码

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

then this is my code on uploading my excel file 然后这是我上传我的excel文件的代码

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);

And this is my error code 这是我的错误代码

Error CS0246 The type or namespace name 'OleDbCommand' could not be found (are you missing a using directive or an assembly reference?) 错误CS0246找不到类型或名称空间名称'OleDbCommand'(您是否缺少using指令或程序集引用?)

Any ideas why this is occurring? 任何想法为什么会这样? Thanks 谢谢

Full Code 完整代码

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);


        }
    }
}

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. 经过一番挖掘(可以在评论中找到),我发现您只能在UWP应用程序中使用一小部分.NET。

Creating a simple console-application will verify that there isn't a problem with your Visual Studio installation. 创建一个简单的控制台应用程序将验证您的Visual Studio安装是否存在问题。 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);   
        }
    }
}

Its simply that UWP does not support OleDbConnection . 简而言之,UWP不支持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. 另外,请查看: UWP的API,以验证您使用的版本正确。

Also, this error can be caused by having multiple reference variants to System.Data for everyone else stumbling across this answer. 同样,此错误可能是由System.Data多个引用变体导致的,其他所有人绊倒了这个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使使用 System.Data.OleDb,C# 也找不到命名空间 OleDbConnection - C# cannot find namespace OleDbConnection even though using System.Data.OleDb is there 在代码中使用 System.data 命名空间时未找到 OleDbConnection - OleDbConnection not found while using System.data namespace in code 在命名空间“System.Data.OleDb”中找不到类型名称“OleDbDataAdapter” - Type name "OleDbDataAdapter" could not be found in the namespace "System.Data.OleDb" C#使用System.Data.OleDb的问题 - C # using System.Data.OleDb problem Xamarin的System.Data.dll 2.0.5.0的System.Data.OleDb中缺少OleDbConnection - OleDbConnection missing in System.Data.OleDb on System.Data.dll 2.0.5.0 for Xamarin C#WebApplication-找到System.Data.OleDb但几乎为空 - C# WebApplication - System.Data.OleDb found but nearly empty PlatformNotSupportedException:此平台不支持 System.Data.OleDb - PlatformNotSupportedException: System.Data.OleDb is not supported on this platform 使用C#,是否可以使System.Data.OleDb处理特殊字符? - Using C#, is it possible to make System.Data.OleDb handle special characters? 缺少System.Data.OleDb-改用什么 - System.Data.OleDb Missing - What to use instead System.Data.OleDb替代System.Data.OracleClient(C#) - System.Data.OleDb as alternative to System.Data.OracleClient (C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM