I am 100% newbie to SQl and wanted to make a ConsoleApp with the use of database. I read some about it and tried. When I needed to make SqlConnection, my VS 2019 Preview showed me this
Severity Code Description Project File Line Suppression State
Error
CS1069 The type name 'SqlConnection' could not be found in the namespace 'System.Data.SqlClient'.
This type has been forwarded to assembly 'System.Data.SqlClient, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly.
ConsoleApp1 C:\Users\User\Desktop\Bald Code\ConsoleApp1\ConsoleApp1\Program.cs 12
Active
i don't get why it doesn't work
Here's my code
using System;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string connectionString;
SqlConnection cnn;
}
}
}
If you just updated EntityFrameworkCore from version 2.x to 3.x and you're running into this, change your using statement to Microsoft.Data.SqlClient instead of System.Data.SqlClient.
If you're using EntityFrameworkCore.SqlServer it already has that as a dependency, so you shouldn't need to install it explicitly.
This Microsoft blog explains the change.
Assuming that you're using .NET Core - just add NuGet package: System.Data.SqlClient
Your .csproj might look similar to:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
</ItemGroup>
</Project>
Most likely the System.Data.SqlClient.DLL in not in the Bin folder, and you have not set reference to the DLL in the project. You have missed the database choice when You install visual studio. And now You just manually add references named "System.Data.SqlClient".
1.right click you project name and choose nuget package options.
2.search "System.Data.SqlClient" and install it.
Hope this is fix Your Error
The specific way to fix this from within VS Code is to
Open a terminal by going to Terminal -> New Terminal
Run dotnet add package System.Data.SqlClient
Run dotnet restore
That last command might not be necessary, but doing this made it work for me. It seems that console app templates don't come prepared for a reference to SqlClient.
Update
I think moving forward projects should use Microsoft.Data.SqlClient and not System.Data.SqlClient: https://devblogs.microsoft.com/dotnet/introducing-the-new-microsoftdatasqlclient/.
As #Mmm mentioned in the comments if you are using .NET Core and have already installed the System.Data.SqlClient package, closing and reopening the project resolved the issue for me too.
I had that error today and what's happening its that VS CODE its no recognizing System.Data.SqlClient;, that's why you can't call the variable...
How did I fix it??
I installed:
SQL server compact toolbox
Nuget Package version updater
Close visual studio/Open it again and your program should be rocking and popping!!
[!["Manage Nuget Package for Solution"]
it fixed my issue.
[1]: https://i.stack.imgur.com/hpk2e.png
I just installed nuget package from the "Manage Nuget Package for Solution". Previously I had used through command but that did not work so I used the UI to install this package.
NuGet Package
Install-Package System.Data.SqlClient
solve my problem.
For me System.Data.SqlClient was already installed. What i had to do was, I went to the Nuget package manager and downgrade the version to 4.8.2 from 4.8.3 and it worked.
It's pretty simple
Go to solution explorer > right click & click manage NuGet packages > and install System.data.SqlClient 4.5.1. or later.
Click here to screenshot
Related
I have written some code that uses the following packages:
using System;
using System.IO;
using System.Reflection;
using Microsoft.Azure.WebJobs;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
But when I compile the code there is a few errors related to the following code, that I suppose it is written by the "Microsoft.Data.SqlClient" package:
using Microsoft.Data.Tools.Schema.Sql.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
namespace GetDatafromBlob0201
{
[TestClass()]
public class SqlDatabaseSetup
{
[AssemblyInitialize()]
public static void InitializeAssembly(TestContext ctx)
{
// Setup the test database based on setting in the
// configuration file
SqlDatabaseTestClass.TestService.DeployDatabaseProject();
SqlDatabaseTestClass.TestService.GenerateData();
}
}
}
Where "using Microsoft.VisualStudio.TestTools.UnitTesting" throws an error.
I have tried installing the NuGet package, but that didn't solve the error.
These are the errors I get:
Errors after compiling
Mostly CS0246.
The last one indicates that the reference could not be used.
I am also noticing a warning sign under my proyect, on "Packeges" and also on "Assembly":
The one on Assembly its on Microsoft.Data.Tools.Components, Version= 16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL. It doesn't give me details on the warning.
The one on Packages is on Microsoft.Data.Tools.UnitTest and it says: Package 'Microsoft.Data.Tools.UnitTest' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead the project target framework '.NETCoreApp,Version=v3.1'. This package may not be fully compatible with your project.
Test with your csproj file and install any nuget packages, I did not get any build errors.
These are the nuget packages which I have installed.
Microsoft.Data.SqlClient
Microsoft.Azure.WebJobs
Microsoft.Data.Tools.UnitTest
So please make sure that there is no red error lines on your code editor.
Then, close VS, delete .vs hidden folder under the solution folder, bin and obj folder.
After that, restart VS and your project, then, run update-package -reinstall command under Tools-->Nuget Package Manager-->Package Manager Console
Update 1
I think you have created a MSTest test project(net core) project rather than a unit test project(net framework) project.
In my side, I used unit test project(net framework), and all work well.
Actually, some of your nuget packages are for net framework. That is what the warning NU1701 did. But you can ignore that warning since it is normal that you have install the package into net core rather than net framework. And the package can be used under net core. And it is just a little warning rather than a error.
Due to that, update-package -reinstall cannot work for new-sdk project(your situaiton) and only for packages.config with net framework.
Maybe try these:
1)Besides, I think Microsoft.Data.Tools.Components.dll is from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\Extensions\Microsoft\SQLDB. You have used some dlls from there. And due to some situations, these do not work and you should readd them on your current environment.
And first, please delete any references from here:
Then, readd these dlls from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\Extensions\Microsoft\SQLDB to check if your project still have these errors.
Or you could remove Microsoft.Data.Tools.Components.dll from there to fix it.
If these do not help, maybe try nuget packages like what I said above:
1) clean all nuget caches first or just delete all files under C:\Users\xxx\.nuget\packages
2) to remove that warning, please add this xml node under csproj file of the unit test project.
3) Or you could try to use unit test project(net framework) project and then add your code there and I ensure that could work.
I've created a nuget package including just 1 .dll.
To test installation of the .nupkg, I
create a C# console application in VS
add a local feed pointing to my package directory
install my package from there
start typing in main...
The problem is, if I do this:
using System;
using MyNamespace;
namespace tester
{
class Program
{
static void Main(string[] args)
{
MyClass.MyStaticMethod()
Console.WriteLine("Hello World!");
}
}
}
the line MyClass.MyStaticMethod() says I'm missing an assembly reference, and using MyNamespace; is an unnecessary declaration.
Reading around, this seems to originate from the fact that my nuget package installs to the global packages location: %Current_User%.nuget.\packages, and the .csproj file includes:
<ItemGroup>
<PackageReference Include="MyPackage" Version="1.0.0" />
</ItemGroup>
Is there a way I can ensure all consumers of my package create a packages.config file, and not install my package to the global packages location?
Can I specify this in the .nuspec file somewhere?
The packages.config way is basically the classic (read: "old") way to reference NuGet packages and has since been superseeded by the PackageReference way of referencing packages. You should make sure your package is compatible with PackageReference.
If you don't increase the version number of your package every time you build it and want to try it out in a project, it won't see the update.
Either increase the version or clear your local NuGet packages cache using dotnet nuget locals clear all.
If the DLL is still not being referenced (check the dependencies node that VS shows you in the solution explorer), then your package may not contain the DLL in the expected place (e.g. lib\netstandard2.0 folder if your source project was a .NET Standard 2.0 library)
So I've come across a similar issue twice now while working on my first project in C#. When trying to add either using System.Data; or using System.Timers;, I get the following error:
The type or namespace name 'x' doesn't exist in the namespace 'System' (are you missing an assembly reference?).
I have tried beginning a new project and running restore to see if I had accidentally removed something in the dependencies, but upon generating a new project I still receive the same error. I have tried to research the question and have seen answers referring to the 'solutions explorer', but as far as I can see there doesn't seem to be such a feature by this name in Visual Studio Code 1.8.
Can anyone point me in the right direction for how to get these working, perhaps by manually adding into the dependencies?
.csproj Project file
The following topic applies to .csproj project file and : .NET Core 1.x SDK, .NET Core 2.x SDK
Adds a package reference to a project file.
dotnet add package
Example
Add Newtonsoft.Json NuGet package to a project:
dotnet add package Newtonsoft.Json
.json Project file
The following topic applies to .json project file:
This guide walks you through the process of adding any assembly reference in Visual Studio Code. In this example, we are adding the assembly reference System.Data.SqlClient into .NET Core C# console application.
Note
At step #6, enter the assembly reference that you want.
Some assembly reference is applicable to .NET Framework and it will gives you error(s).
OleDb is not available in .NET Core, probably because it's not cross platform.
Prerequisites
Install Visual Studio Code
Install .NET Core SDK (Preview 2 version)
Install NuGet Package Manager from the Visual Studio Code Extension Marketplace
Install C# extension from Visual Studio Code Extension Marketplace
Steps
Launch Visual Studio Code
Open your project folder
Launch VS Code Command Palette by pressing F1 or Ctrl+Shift+P or Menu Bar > View > Command Palette
In Command Palette box, type nu
Click on NuGet Package Manager: Add Package
Enter package filter e.g. system.data (Enter your assembly reference here)
Press Enter
Click on System.Data.SqlClient
The following prompt pops up
Click on Restore
The following Output panel pops up
In the Explorer panel, click on project.json to open it
In the Editor panel, it shows the assembly reference added into project.json file
Assembly reference, System.Data.SqlClient used in Program.cs
Use the command dotnet add package to add a package reference to your project. For example: dotnet add package Newtonsoft.Json, which adds the package reference to the *.csproj project file:
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
and now you can run the command dotnet restore to restores the dependencies of your project.
Reference: dotnet add package
drag the dll file and drop it into the bin folder
Above answer from ikolim doesnt work as indicated by someone else too, there is no, Nuget: Install/Reference command. There is only Add Package! So the answer in the below link solved my problem. Manually editing the Myproject.csproj file.
Duplicate of this thread
I've stored the files in a project folder named "dlls" and added the reference files in my .csproj file like this:
<ItemGroup>
<Reference Include="Microsoft.Office.Client.Policy.Portable">
<HintPath>dlls\Microsoft.Office.Client.Policy.Portable.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Office.Client.TranslationServices.Portable">
<HintPath>dlls\Microsoft.Office.Client.TranslationServices.Portable.dll</HintPath>
</Reference>
</ItemGroup>
In case of extisting .dll reference,
Right click project
Add existing item > select path to .dll
After added dll in project,right click .dll
build-action = Content, Copy-to-output-dir = Always/ or if newer
I'm trying to call loggerFactory.AddSerilog(); as per this documentation, but the AddSerilog method is not recognized:
"Error CS1061 'ILoggerFactory' does not contain a definition for 'AddSerilog' and no extension method 'AddSerilog' accepting a first...".
I'm using ASP.NET CORE with the full .NET framework.
What am I doing wrong?
You may forget this following line in project.json
"Serilog.Extensions.Logging": "1.0.0",
See also https://carlos.mendible.com/2016/09/19/step-step-serilog-asp-net-core/
Different circumstance, but same problem. In my case, I was using .Net Core 2.1 and had a NuGet reference to Serilog, but was missing a reference to Serilog.AspNetCore. The issue first manifested as .UserSerilog() not being found for the IWebHostBuilder of my CreateWebHostBuilder static method under Program.cs.
Adding the Serilog.AspNetCore NuGet package to my project solved the problem.
The posted answer is correct but I will add that you may want to use the NuGet package manager that way you can get the latest version.
Right click on solution
-> Choose "Manage NuGet packages for solution"
-> type "serilog.extensions.logging" into search box
-> Click on Serilog.Extensions.Logging and press install
You will get a dropdownlist of the different versions you should choose the latest.
Or quicker from Package Manager console verify that Default Project drop-down has your project selected and run
install-package Serilog.Extensions.Logging
On .NET Core 3.1, within a console app, I simply had to install the serilog.extentions.hosting NuGet package.
This will add the below line to the ItemGroup within YourProjectName.csproj
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.7" />
If you have package Serilog.Extensions.Logging added to your project already, it could be just a matter of adding using Serilog; to the top of your code file.
I am working on an Asp.Net MVC 4 Application in which I am using SignalR 2.0.1 and I Mapped it using Owin Startup class and it worked fine at first.
All of a sudden when I tried to rebuild my app it said that the type are namespace IAppbuilder could not be found.
Following is my start up class
using Microsoft.Owin;
using Owin;
using WhiteBoardApp;
namespace WhiteBoardApp
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
I have installed Owin package too, and for some reasons I could not find Owin Startup class so I just added a normal class and included all the references that are needed.
May I know where I am making a mistake
Try to use Package Manage Console and do
Update-Package Owin -Reinstall
I was having similar issue. But instead Owin, problem was causing Microsoft.Owin, obviously
Update-Package Owin -Reinstall
Didn't work, neither did Update-Package Owin
BUT
Install-Package Microsoft.Owin
did work fine for me, thanks.
The IAppBuilder interface is found under Owin package. Just add a reference in your class file:
using Owin;
And rebuild. Your project will pick this up.
I have no idea why VS didn't pick this up, but it didn't. Once I added this reference to my project, then everything fell into place.
I encountered the same problem while building my project. Here are the steps that helped fix my problem:
Go to Solution Explorer and look for your project
Under your project, expand the References; You should see warnings on the problematic reference
Right click References and open Manage NuGet Packages
Search the name of problematic reference i.e. Microsoft.Owin; After loading it shows that it is already installed (It is, but it installed incorrectly. Checking the properties > version at step 2 shows 0.0.0.0)
Check Force uninstall, even if there are dependencies on it
Uninstall
Install
Build and run the project
Problems
Cannot install Microsoft.Web.Infrastructure because it already exists in the packages folder. Rolling back...
Go to your project folder and look for packages
Find the problematic package i.e. Microsoft.Web.Infrastructure
Delete the folder
Resume from step 7
Alternatives
Here are the alternatives I've read about to fix this kind of problem.
Clean and Rebuild Project / Solution
Restart Visual Studio
Restart PC
Good luck.
My Visual Studio 2013 for some reason didn't realize that the references paths existed. The yellow exclamation mark in front of the references was shown for all the added packages. I checked ../packages/ but all files existed, i also opened the .csproj file which referenced the correct paths.
Closing and opening the solution returned quite a lot of errors, and could not load the projects included in the solution.
Restarting Visual Studio 2013 saved the day for some unexplained reason.
My following using's equivalent in F# present a problem of hiding the IAppBuilder. It turns out that the Owin stipulation was being interpreted as an incomplete System.Web.Http.Owin reference, even though the Owin.dll providing the Owin namespace was referenced.
open System.Net.Http
open System.Web.Http
open Microsoft.Owin
open Owin
The problem was resolved by rearranging the usings as follows:
open Microsoft.Owin
open Owin
open System.Net.Http
open System.Web.Http
...granted, this may be a bug peculiar to the F# compiler and name conflicts are handle better in C# and elsewhere.
In my case, I had moved around the project folders and the location of the vs solution file (.sln). Once I was done with re-adding the projects, there was a packages folder on the solution level and one was left in a project sub folder.
This way, in that project, the relative package folder links in the .csproj file got messed up.
The reinstallation or other tips regarding the nuget package manager in this thread were helpful. I noticed, that after I reinstalled a few packages, in my git source code diff, the path of the packages folder was changed within the csproj file.
Before
<HintPath>packages\Microsoft.Owin.4.0.1\lib\net45\Microsoft.Owin.dll</HintPath>
After
<HintPath>..\packages\Microsoft.Owin.4.0.1\lib\net45\Microsoft.Owin.dll</HintPath>
So, if you run in the same issue and you have a lot of nuget packages, it might be easier to close the whole solution, open the csproj file(s) in a text editor like vscode and fix the relative links with search and replace. Then just save, close, reopen solution in VS and restore nuget packages. That should do the trick.
(In any case, you should delete the local packages folder on the project level, so that the project really fails, if it does not get the right packages.)
It's an ordering issue.
using Microsoft.Owin;
using Owin;
Leads to Microsoft.Owin to be defined first, then Owin is found under already imported Microsoft namespace. If you mouse over Owin of using Owin you should see it was resolved to Microsoft.Owin again and furthermore IDE will gray out using Owin as redundant unused reference.
Do:
using global::Owin;
Which clarifies for the compiler not to look for Owin under already defined namespaces (e.g. Microsoft. namespace).
http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr
Check for the visual studio you are using
You can find the following comment
Note: If you are using Visual Studio 2012, the SignalR Hub Class (v2) template will not be available. You can add a plain Class called ChatHub instead.
Also
Note: If you are using Visual Studio 2012, the OWIN Startup Class template will not be available. You can add a plain Class called Startup instead.