skip to Main Content

I’m new to C# and I want to use a third library party library for the first time, in this case the sqlite library.
My problem: I haven’t got enough memory on my computer to install Visual Studio, so I need to compile and run everything from the terminal.

I created my app using the command

dotnet new console -o SqliteTestApp -f net7.0

The tutorial where I got the command from is here

Then I wrote the following code in Program.cs.

    using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SQLiteDemo
{
   class Program
   {

      static void Main(string[] args)
      {
         Console.WriteLine("Hello Sqlite!");
         CreateConnection();
      }

      static SQLiteConnection CreateConnection()
      {

         SQLiteConnection sqlite_conn;
         // Create a new database connection:
         sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=True;Compress=True;");
         // Open the connection:
         try
         {
            sqlite_conn.Open();
            Console.WriteLine("Connection worked!");
         }
         catch (Exception ex)
         {
            Console.WriteLine("Connection didn't work!");
         }
         return sqlite_conn;
      }

      
   }
}

My SqliteTestApp.csproj-file looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

The code above does not compile with the command

dotnet run

,because I don’t include the SQLite library.
My question: How do I modify this project-file , so I can use the SQLite-library and what commands to run from the command line. I know you somehow need to know how to use nuget.
Any help appreciated!

2

Answers


  1. Run this command in the command prompt to add SQLite package reference:

    dotnet add package System.Data.SQLite --version 1.0.117
    

    I assume you will use the SQLite library at System.Data.SQLite Nuget.

    Login or Signup to reply.
  2. You can use the dotnet cli to add a package in a csproj. Search the desired package on nuget.org and choose .NET Cli to display the command.

    In you case, to add System.Data.SQLite, you can execute this command in the csproj repository :

    dotnet add package System.Data.SQLite
    

    The result will be :

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net7.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="System.Data.SQLite" Version="1.0.117" />
      </ItemGroup>
    
    </Project>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search