How can i connect access database on console application? - c#

I want to update the database on my console application.
but here has an error:
The type name ‘OleDbConnection’ could not be found in the namespace ‘System.Data.OleDb’.
This type has been forwarded to assembly ‘System.Data.OleDb, Version=4.0.1.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35’
Consider adding a reference to that assembly.
I can easily reach datas from form application but when i try it on console application, i can not.
there is my code:
using System;
using System.Data;
using System.Data.OleDb;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
OleDbConnection baglanti = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.\botbase.accdb");
}
}
}
there is the photo:

I assume you are using .NET Core and not the .NET Framework?
So you probably have to use the .NET Platform Extensions (available as NuGet package):
System.Data.OleDb
The issue-comment that announces it:
Everyone,
Now that System.Data.OleDb is available on NuGet.org, it would be great if you could take it for a spin on .NET Core 3.0 preview 6. If you have an existing application, sample code, or a test that was blocked from working on .NET Core for lack of this functionality, please try now. We are hoping to get your help to cover a variety of OLEDB providers and scenarios, so we can identify any issues with the port before RTM.
Thanks!
Another really useful package for Windows dependent features:
Microsoft.Windows.Compatibility

OleDbConnection is in the System.Data.Common.DbConnection namespace.
Add a using clause to the top of your file for that namespace.

Related

VSCode C# Issue Adding An Assembly Reference

I have the C# VSC extension as well as the NuGet Package Manager installed. Here is my VSC version info:
First, I create a brand new console application in VSC with the command:
dotnet new console
This gives me the code:
using System;
namespace ReferenceTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
I now want to create a using System.Windows.Forms; reference. When I start typing this using statement however, intelli-sense fails me:
I found this question/answer already but in step #5 the autofill option shown is not there:
When I try to improvise and follow other instructions I've found in my searching by using the
NuGet Package Manager: Add Package
option, I cannot select System.Windows.Forms
I have looked for a project.json file per the instructions of several sites but I can only find a project.assets.json file and it doesn't look very similar to what I see in the examples I find. This issue is not only for System.Windows.Forms but other references I try as well.
Windows Forms and WPF applications are not supported in .NET Core, only in .NET Framework.
have a look at the following article:
https://stackify.com/net-core-vs-net-framework/

Why WindowsIdentity class is not visible in .NET Core

Having the code below in VisualStudio 2017 .NET Core 2.0 Console App
using System;
using System.Security.Principal;
namespace smallTests
{
class Program
{
static void Main(string[] args)
{
var identity = WindowsIdentity.GetCurrent();
}
}
}
Why am I getting the error:
The name 'WindowsIdentity' does not exist in the current context
If I can see this class in .NET Core 2.0 library in .Net Core docs ?
Same code works in .NET Console app.
[EDIT]
#Will #JohnnyL Commented that I do not refer, System.Security.Principal.Windows.dll, that is true.
But I am curious why it is not working, because
in .NET 4.6.1 project (where class WindowsIdentity is visible) I also do not refer this System.Security.Principal.Windows.dll specifically. However i refer System.dll.
I always thought that it works like namespace hierarchy. For instance, when I refer to
System.Security.Principal.dll
i can use class which is in
System.Security.Principal.Windows.dll.
Am I wrong?
I added System.Security.Principal.dll to .NetCore solution by hand but it still does not work.
[EDIT2]
#Will Thank you a lot for expaining the subject it helped me a lot.
I tried to figure out is WindowsIdentity compatible with Core and it seems that it is please see:
in this apisof.net in Declarations area i can see that WindowsIdentity is in .Net Core 2.0 System.Security.Principal.Windows, Version=4.1.1.0, PublicKeyToken=b03f5f7f11d50a3a
but i do not have System.Security.Principal.Windows.dll in references, should I add it? If yes from where?
in .NET Core api reference i see this class in the list (what is the purpose of that listing if it is not compatible with core?
I also find information about that class in that link
Am I looking in wrong places?
Microsoft announced Windows Compatibility Pack for .NET Core a few weeks ago,
https://blogs.msdn.microsoft.com/dotnet/2017/11/16/announcing-the-windows-compatibility-pack-for-net-core/
And by analyzing the source code of System.Security.Principal.Windows.csproj and the commit adding it,
https://github.com/dotnet/corefx/blob/master/src/System.Security.Principal.Windows/src/System.Security.Principal.Windows.csproj
My conclusion is that this is also part of the Windows only compatibility libraries, so can only be used on Windows.
To add that to your project, open your csproj and add a PackageReference tag for System.Security.Principal.Windows manually (or use Visual Studio's NuGet Package Manager).

Deleting files in a cross platform compatible manner in .NET Core

I would like to delete a file in a .NET Core 2.0 application but cannot find any alternative to File.Delete.
When creating or reading files we are encouraged to use providers as below because they wrap the underlying file system allowing cross platform access:
IFileProvider provider = new PhysicalFileProvider(dir);
IFileInfo fileInfo = provider.GetFileInfo(fileName);
However there does not appear to be any complimentary functionality for delete operations. System.IO is available in .NET Core 2.0 but is this cross platform compatible?
I just tested this trivial app on Ubuntu 16.04 and it worked.
using System;
using System.IO;
namespace gbsills
{
class Program
{
static void Main(string[] args)
{
File.Delete("file.txt");
}
}
}
Of course, you'll need to make sure you use the correct file path separators and such if you want to be cross-platform.

How to use Windows Runtime classes in .NET Core libraries?

I'm developing a library for use with WPF and Windows 10. I'm running into issues getting it to compile on the latter. Here is some of the code:
project.json
{
"frameworks": {
"net46": {
"frameworkAssemblies": {
"WindowsBase": "4.0.0.0"
}
},
"netcore50": {
"dependencies": {
"Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0"
}
}
}
}
Dependency.cs
using System;
using System.Collections.Generic;
#if NET46
using System.Windows; // .NET Framework 4.6
#elif NETCORE50
using Windows.UI.Xaml; // Windows 10 apps
#endif
public static class Dependency
{
public static DependencyProperty Register<T, TOwner>(string name, PropertyChangedCallback<T, TOwner> callback)
where TOwner : DependencyObject
{
// Code here....
}
}
While this compiles fine for net46 (which is the traditional .NET Framework), I'm having trouble getting it to work for netcore50 (which can be used by Windows 10 apps). For some reason, it looks like types like DependencyProperty or DependencyObject are not included in that configuration.
Is there a netcore50-compatible NuGet package I can install that contains these types, so I can use them from my library?
Thanks for helping.
EDIT: I just typed in DependencyProperty in VS and hit F12. It appears that the type lives in the Windows.Foundation.UniversalApiContract assembly, but there's no such package on NuGet.
Finally solved the problem on my own! (If you're looking for a quick answer, you may want to scroll down.)
I remembered by chance that the .NET Core GitHub repo had a bunch of WinRT-specific libraries, like System.Runtime.WindowsRuntime. So, I headed over there to see how they did it.
It appears they use some kind of internally-hosted "targeting pack", which contains a single Windows.winmd file (which holds all the types in the Windows Runtime), to achieve this affect. Unfortunately, the package is hosted on a private NuGet feed meant only for the .NET Core team, so I can't use it.
I've opened an issue about this on the CoreFX repo here, so I can petition Microsoft for an official solution to this problem. In the meantime, I've taken matters into my own hands. I've found all the different versions of Windows.winmd on my laptop, and uploaded them as NuGet packages. Here they are:
Target.Windows
Target.WindowsPhone
Target.WindowsRuntime
You can use them like this:
"frameworks": {
".NETPortable,Version=v4.5,Profile=Profile32": {
"dependencies": {
"Target.WindowsRuntime": "8.1.2"
}
}
}
After that, you'll be able to write something like this:
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
public class MyApp : Application
{
public MyApp()
{
var button = new Button();
button.Content = "Hello, world!";
}
}
and it'll just work.
With .NET Core 3 and up (now in preview) there is a package you can install that includes most WinRT classes Microsoft.Windows.SDK.Contracts
WPF Isn't compatible with .net Core nor are W10 Universal Apps, to my knowledge only Console apps and web apps are currently compatible with .net core, you should be able to still use the the new code base with the new project system but you will need to remove .net core from your configuration in order to compile
if you want to use .net core with linux with a desktop app you will simply have to wait, or use a compatible windowed app framework ( if any are available yet), you should be able to use a cross platform framework base around html/js such as Electron or Cordova ( not sure on this one on whether there is a desktop app framework with Cordova)

C# .Net Write plug-ins for windows live writer and my plugins cant be loaded

I'm new in .Net and I'm now trying to write a plugin for windows live writer because I prefer to publish blogs using it rather than using the web editors. And I want to develop some small plugins for my daily use. But after I created a class libarary project and built it following the steps as some developers described, the WLW does not load the plugin so I don't know how to debug.
My Code:
using System;
using System.Collections.Generic;
using System.Text;
using WindowsLive.Writer.Api;
using System.Windows.Forms;
namespace Insert_Colorful_Table
{
[WriterPluginAttribute
("f7581112-dddd-47c9-9db0-46987a2aaae1",
"Insert Colorful Table",
Description = "Helps you create a beautiful table.",
ImagePath = "icon.gif",
PublisherUrl = "http://ggicci.blog.163.com")]
[InsertableContentSource("Insert Colorful Table")]
public class Plugin : ContentSource
{
public override DialogResult CreateContent
(IWin32Window dialogOwner, ref string content)
{
content = #"<table><tr><td>Ggicci</td></tr></table>";
return DialogResult.OK;
}
}
}
I did configure the 'Build Events' of the project and set the 'Build Action' of the image to 'Embedded Resource'. And no errors occured when building my project.
okay, I've worked out what went wrong. Windows Live Writer supports plugins that are built with either the Microsoft .NET Framework version 1.1 or 2.0. Writer requires users to have .NET 2.0 present to install the application. But I used .NET 4.0. So when I changed target framework to 2.0, then it worked well.

Categories