sqlite ConnectionsTring在.NET 6 Console App visualstudio 2022中将其写入哪里

发布于 2025-01-31 07:50:25 字数 2475 浏览 3 评论 0 原文

我完全是数据库的新手,但是这个项目需要一个项目,否则它将无法使用。因此,我需要一个SQLite数据库。我安装了Nuget所需的,并用行实现了数据库表,并将数据库与我的项目连接了。

这是我的类

   public class DatenbankDaten
   {
      public string zsw_id { get; set; }
      public int personio_id { get; set; }
      public string von { get; set; }
      public string bis { get; set; }
    }

,这是代码

private static string LoadConnectionString( string id = "Default")
    {
        return ConfigurationManager.ConnectionStrings[id].ConnectionString;
    }

    public void SaveIds ( DatenbankDaten daten)
    {
        using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
        {
            cnn.Execute("insert into Ids (zsw_id,personio_id,von,bis) values (@zsw_id, @personio_id, @von, @bis", daten);
        }
    }

,我得到 nullReference异常 back ..我的意思是我理解它,因为我也没有写过它所属的连接串。目前,连接串在我的.csproj中

      <ItemGroup>
        <Content Include="zausinger.db">
            <connectionStrings>
                <add name="Default" connectionString="Data Source=.\zausinger.db;Version=3;" providerName="System.Data.SqlClient"/>
            </connectionStrings>
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
      </ItemGroup>

> and these are my PackageReferences

 

     <ItemGroup>
        <PackageReference Include="Dapper" Version="2.0.123" />
        <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
        <PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
        <PackageReference Include="System.Data.SQLite.Core" Version="1.0.115.5" />
        <PackageReference Include="System.ServiceModel.Duplex" Version="4.8.*" />
        <PackageReference Include="System.ServiceModel.Federation" Version="4.8.*" />
        <PackageReference Include="System.ServiceModel.Http" Version="4.8.*" />
        <PackageReference Include="System.ServiceModel.NetTcp" Version="4.8.*" />
        <PackageReference Include="System.ServiceModel.Security" Version="4.8.*" />
        <PackageReference Include="Telegram.Bot" Version="17.0.0" />
      </ItemGroup>    

if someone could help a newbie i would i appriciate it.

I am totaly new with databases but this project needs one otherwise it wont work. So i need a SQLite Database. I installed ever NuGet needed and implemented my Database table with the rows and connected the Database with my project.

This is my class

   public class DatenbankDaten
   {
      public string zsw_id { get; set; }
      public int personio_id { get; set; }
      public string von { get; set; }
      public string bis { get; set; }
    }

This is the Code

private static string LoadConnectionString( string id = "Default")
    {
        return ConfigurationManager.ConnectionStrings[id].ConnectionString;
    }

    public void SaveIds ( DatenbankDaten daten)
    {
        using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
        {
            cnn.Execute("insert into Ids (zsw_id,personio_id,von,bis) values (@zsw_id, @personio_id, @von, @bis", daten);
        }
    }

And i get a NullReference exception back.. I mean i understand it because i dont have written the connectionstring where it belongs too. At the moment the connectionstring is in my .csproj

      <ItemGroup>
        <Content Include="zausinger.db">
            <connectionStrings>
                <add name="Default" connectionString="Data Source=.\zausinger.db;Version=3;" providerName="System.Data.SqlClient"/>
            </connectionStrings>
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
      </ItemGroup>

> and these are my PackageReferences

 

     <ItemGroup>
        <PackageReference Include="Dapper" Version="2.0.123" />
        <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
        <PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
        <PackageReference Include="System.Data.SQLite.Core" Version="1.0.115.5" />
        <PackageReference Include="System.ServiceModel.Duplex" Version="4.8.*" />
        <PackageReference Include="System.ServiceModel.Federation" Version="4.8.*" />
        <PackageReference Include="System.ServiceModel.Http" Version="4.8.*" />
        <PackageReference Include="System.ServiceModel.NetTcp" Version="4.8.*" />
        <PackageReference Include="System.ServiceModel.Security" Version="4.8.*" />
        <PackageReference Include="Telegram.Bot" Version="17.0.0" />
      </ItemGroup>    

if someone could help a newbie i would i appriciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒绊 2025-02-07 07:50:25

.net 6是.net core 6,不再使用 app.config 和相关类。

删除过时的软件包,并使用新的配置中间件。现在从多个来源读取设置,并使用 appSettings.json 仅默认来源之一。

当前的Microsoft Sqlite驱动程序是。当前版本为6.0.5,

使用通用主机软件包,您可以将配置,日志记录和DI中间件添加到您的应用程序中。

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Data.Sqlite;
using Dapper;

using IHost host = Host.CreateDefaultBuilder(args).Build();

The default configuration sources are described in the documentation< /a>并包括JSON配置文件,环境变量和命令行参数。

之后,您可以通过其密钥检索配置设置。连接字符串有自己的方法:

var Configuration = host.Services.GetRequiredService<IConfiguration>();

var cns = Configuration.GetConnectionString("Default");
using var connection = new SqliteConnection("Data Source=hello.db");

var rows = await connection.QueryAsync("select * from Test");

Console.WriteLine($"Rows {rows.Count()}");

应用程序的设置存储在 appSettings.json 文件中:

{
    "ConnectionStrings": {
        "Default": "./mydb.db"
    }
}

最后, csproj 文件添加Microsoft.data.sqlclient和 microsoft。 extensions.hosting 依次添加配置,记录和依赖项注入作为依赖项:

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

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

  <ItemGroup>
    <PackageReference Include="Dapper" Version="2.0.123" />
    <PackageReference Include="Microsoft.Data.Sqlite" Version="6.0.5" />
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
  </ItemGroup>

</Project>

您可以在需要的情况下仅使用配置中间件,并仅添加所需的源:

IConfiguration Configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json")
    .AddEnvironmentVariables()
    .AddCommandLine(args)
    .Build();

.NET 6 is .NET Core 6 and no longer uses app.config and the related classes.

Remove the obsolete packages and use the new configuration middleware. Settings are read from multiple sources now, with appsettings.json just one of the default sources.

The current Microsoft Sqlite driver is Microsoft.Data.Sqlite. The current version is 6.0.5

Using the Generic Host package you can add Configuration, Logging and DI middleware to your application in a single line.

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Data.Sqlite;
using Dapper;

using IHost host = Host.CreateDefaultBuilder(args).Build();

The default configuration sources are described in the documentation and include JSON config files, environment variables and command line arguments.

After that you can retrieve configuration settings by their key. Connection strings get their own method :

var Configuration = host.Services.GetRequiredService<IConfiguration>();

var cns = Configuration.GetConnectionString("Default");
using var connection = new SqliteConnection("Data Source=hello.db");

var rows = await connection.QueryAsync("select * from Test");

Console.WriteLine(
quot;Rows {rows.Count()}");

The application's settings are stored in the appsettings.json file :

{
    "ConnectionStrings": {
        "Default": "./mydb.db"
    }
}

Finally, the csproj file adds Microsoft.Data.SqlClient and Microsoft.Extensions.Hosting which in turn adds Configuration, Logging and Dependency Injection as dependencies :

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

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

  <ItemGroup>
    <PackageReference Include="Dapper" Version="2.0.123" />
    <PackageReference Include="Microsoft.Data.Sqlite" Version="6.0.5" />
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
  </ItemGroup>

</Project>

You can use only the Configuration middleware if you want, and add only the sources you need :

IConfiguration Configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json")
    .AddEnvironmentVariables()
    .AddCommandLine(args)
    .Build();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文