我阅读了另一个问题,其中没有OldeDb
名称空间。但是我仍然有问题
我正在创建UWP应用,我想将数据从Excel文件上传到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文件的代码
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指令或程序集引用?)
任何想法为什么会这样?谢谢
完整代码
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);
}
}
}
答案 0 :(得分:1)
经过一些挖掘(可以在评论中找到),我发现您只能在UWP应用程序中使用一小部分.NET。
创建一个简单的控制台应用程序将验证您的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);
}
}
}
仅是UWP不支持OleDbConnection
。
请查看here有关您可以使用的内容的更多详细信息。另外,请查看:API's of UWP,以验证您使用的版本正确。
此外,该错误可能是由于其他所有人都在绊倒这个答案时遇到System.Data
的多个引用变体引起的。