none
.net core 5 basic cs proj and code running on linux to connect to linux sql server? RRS feed

  • Question

  • When I tried to follow the instructions for

     https://sqlchoice.azurewebsites.net/en-us/sql-server/developer-get-started/csharp/rhel/step/2.html

    using .net5.0 I could not get it right. Would anyone know a very basic example of a program running on linux that would connect to a linux sql server.


    • Edited by mwindham Saturday, November 21, 2020 2:52 AM
    Saturday, November 21, 2020 2:47 AM

Answers

  • Everything is fine now. Installed all .net cores 2.1,3.1 and 5.0 with dotnet add Microsoft.Data.SqlClient.

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp2.1</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
      </ItemGroup>
    
    </Project>
    
    Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp3.1</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
      </ItemGroup>
    
    </Project>
    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net5.0</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.0" />
      </ItemGroup>
    
    </Project>
    

    and basic program code 

    //2.1, 3.1
    using System;
    using System.Data.SqlClient;
    //using System.Linq;
    //using System.Text;
    //using System.Threading.Tasks;
    namespace lxsql1{
            class Program{
                static void Main(string[] args){
                    
                    string sc = "Data Source = troglx;Initial Catalog = x;User ID = sa; Password = \"xxxxxxxxxxx\"";
                    SqlConnection c = new SqlConnection (sc);
                    try {
                        c.Open();
                        Console.WriteLine("Connection Open ! ");
                        c.Close();
                        Console.WriteLine("Connection Closed ! ");
                    }
                    catch (Exception ex){
                        Console.WriteLine("Can not open connection ! ");
                    } 
                }
            }
    }
    
    
    
    
    //5.0
    using System;
    using Microsoft.Data.SqlClient;
    //using System.Linq;
    //using System.Text;
    //using System.Threading.Tasks;
    namespace lxsql50{
            class Program{
                static void Main(string[] args){
                    
                    string sc = "Data Source = troglx;Initial Catalog = x;User ID = sa; Password = \"xxxxxxxxxx\"";
                    SqlConnection c = new SqlConnection (sc);
                    try {
                        c.Open();
                        Console.WriteLine("Connection Open ! ");
                        c.Close();
                        Console.WriteLine("Connection Closed ! ");
                    }
                    catch (Exception ex){
                        Console.WriteLine("Can not open connection ! ");
                    } 
                }
            }
    }
    

    • Marked as answer by mwindham Sunday, November 22, 2020 3:12 AM
    Sunday, November 22, 2020 3:12 AM

All replies

  • I installed Microsoft.Data.Client 2.1.0 with nuget, proj looks like

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net5.0</TargetFramework>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.0" />
      </ItemGroup>
     
    </Project>

    and error is 

    All projects are up-to-date for restore.
    /usr/share/dotnet/sdk/5.0.100/Microsoft.Common.CurrentVersion.targets(2838,7): error MSB4057: The target "CreateManifestResourceNames" does not exist in the project. [/home/mwremote/projects/lxsql0/lxsql0.proj]

    Build FAILED.

    /usr/share/dotnet/sdk/5.0.100/Microsoft.Common.CurrentVersion.targets(2838,7): error MSB4057: The target "CreateManifestResourceNames" does not exist in the project. [/home/mwremote/projects/lxsql0/lxsql0.proj]
        0 Warning(s)
        1 Error(s)

     
    • Edited by mwindham Saturday, November 21, 2020 4:33 PM
    Saturday, November 21, 2020 6:40 AM
  • You make no mention of the project type. Is it .NET Core desktop or ASP.NET?
    • Edited by DA924x Saturday, November 21, 2020 11:18 AM
    Saturday, November 21, 2020 11:17 AM
  • console Core
    • Edited by mwindham Saturday, November 21, 2020 4:34 PM
    Saturday, November 21, 2020 4:33 PM
  • console Core
    A console program is a Windows desktop program. Windows desktop Core programs only work with the Windows O/S. Only ASP.NET Core Web based program work on other O/S platforms.
    Saturday, November 21, 2020 6:45 PM
  • Everything is fine now. Installed all .net cores 2.1,3.1 and 5.0 with dotnet add Microsoft.Data.SqlClient.

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp2.1</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
      </ItemGroup>
    
    </Project>
    
    Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp3.1</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
      </ItemGroup>
    
    </Project>
    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net5.0</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.0" />
      </ItemGroup>
    
    </Project>
    

    and basic program code 

    //2.1, 3.1
    using System;
    using System.Data.SqlClient;
    //using System.Linq;
    //using System.Text;
    //using System.Threading.Tasks;
    namespace lxsql1{
            class Program{
                static void Main(string[] args){
                    
                    string sc = "Data Source = troglx;Initial Catalog = x;User ID = sa; Password = \"xxxxxxxxxxx\"";
                    SqlConnection c = new SqlConnection (sc);
                    try {
                        c.Open();
                        Console.WriteLine("Connection Open ! ");
                        c.Close();
                        Console.WriteLine("Connection Closed ! ");
                    }
                    catch (Exception ex){
                        Console.WriteLine("Can not open connection ! ");
                    } 
                }
            }
    }
    
    
    
    
    //5.0
    using System;
    using Microsoft.Data.SqlClient;
    //using System.Linq;
    //using System.Text;
    //using System.Threading.Tasks;
    namespace lxsql50{
            class Program{
                static void Main(string[] args){
                    
                    string sc = "Data Source = troglx;Initial Catalog = x;User ID = sa; Password = \"xxxxxxxxxx\"";
                    SqlConnection c = new SqlConnection (sc);
                    try {
                        c.Open();
                        Console.WriteLine("Connection Open ! ");
                        c.Close();
                        Console.WriteLine("Connection Closed ! ");
                    }
                    catch (Exception ex){
                        Console.WriteLine("Can not open connection ! ");
                    } 
                }
            }
    }
    

    • Marked as answer by mwindham Sunday, November 22, 2020 3:12 AM
    Sunday, November 22, 2020 3:12 AM
  • These console or terminal programs run on Linux (Centos 8) with dotnet core 2.1, 3.1, and 5.0. Maybe more a dll in dotnet run.
    • Edited by mwindham Sunday, November 22, 2020 3:22 AM
    Sunday, November 22, 2020 3:15 AM