简体   繁体   中英

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. 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.

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

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

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.

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM