6

I am trying to connect to the SQL database in my network using the code below. I've searched on Stackoverflow and web for this error and found so many other people experiencing this issue, applied and tested many none fixed my issue. Currently the code I have is below:

using System;
using System.Data.SqlClient;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            using ( SqlConnection conn = new SqlConnection)
            {
                conn.ConnectionString = "Server=***;Database=***;Trusted_Connection=True;";
            }
        }
    }
}

In the place of *** I have server and database names. Also, where do I enter the specific table?

14
  • 3
    What exact error are you facing? You'd put the table name in the query, which you specify when you create a command. Note that you need () after new SqlConnection. Commented Dec 3, 2018 at 14:58
  • a table would be part of a specific query, and you aren't showing any queries; the code you've shown shouldn't compile - it would need to be using (SqlConnection conn new SqlConnection()) (note extra parentheses) - but: the error suggests the problem is resolving the type itself; is this a missing reference? Are you referencing System.Data? or if this is .NET Core: System.Data.SqlClient (package reference)? Commented Dec 3, 2018 at 14:59
  • oh okay. In visual studio where there is SqlConnection text, there is red line under it when I go over it says "The type or namespace name 'SqlConnection' could not be found( are you missing a using directive oran assembly reference?) " and gives me show potential fixes but those create a new file. Commented Dec 3, 2018 at 15:01
  • I think it might be worth stopping this line of dev right now and taking a look at entity framework.. In an hour you'll be writing command.CommandText="SELECT * FROM table WHERE id = " + Textbox1.Text; and the fight to save another developer from spending the next 5 years writing insecure, low quality database access code will be lost :/ Commented Dec 3, 2018 at 15:01
  • 2
    using ( SqlConnection conn = new SqlConnection()) Commented Dec 3, 2018 at 15:02

1 Answer 1

15

All I did was create a new console app and enter this code to connect to DB.

So I can infer that you're using .NET Core, and so you need to add the NuGet package: System.Data.SqlClient

Your .csproj file should look like:

<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>
Sign up to request clarification or add additional context in comments.

4 Comments

Okay, I went to "ConsoleApp2\ConsoleApp2" folder and replaced the code in the .csproj with the code above, no more red lines under SqlConnection string.
In Visual Studio you can manage your NuGet dependencies by right-clicking on the "Dependencies" node under your project in the Solution Explorer, or by using the "Package Manager Console". Of course editing the .csproj file is supported too.
I did the same as mentioned by @Peace! And the red lines under SqlConnection string went away. But, I cannot find the Form in the Design Mode anymore. On executing the .exe application the Windows Form comes up but no longer able to edit the Form in Design Mode. Any ideas how to fix it ?
Ask a new question with the relevant information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.