sqlite ConnectionsTring在.NET 6 Console App visualstudio 2022中将其写入哪里
我完全是数据库的新手,但是这个项目需要一个项目,否则它将无法使用。因此,我需要一个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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
.net 6是.net core 6,不再使用
app.config
和相关类。删除过时的软件包,并使用新的配置中间件。现在从多个来源读取设置,并使用
appSettings.json
仅默认来源之一。当前的Microsoft Sqlite驱动程序是。当前版本为6.0.5,
使用通用主机软件包,您可以将配置,日志记录和DI中间件添加到您的应用程序中。
The default configuration sources are described in the documentation< /a>并包括JSON配置文件,环境变量和命令行参数。
之后,您可以通过其密钥检索配置设置。连接字符串有自己的方法:
应用程序的设置存储在
appSettings.json
文件中:最后,
csproj
文件添加Microsoft.data.sqlclient和microsoft。 extensions.hosting
依次添加配置,记录和依赖项注入作为依赖项:您可以在需要的情况下仅使用配置中间件,并仅添加所需的源:
.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.
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 :
The application's settings are stored in the
appsettings.json
file :Finally, the
csproj
file adds Microsoft.Data.SqlClient andMicrosoft.Extensions.Hosting
which in turn adds Configuration, Logging and Dependency Injection as dependencies :You can use only the Configuration middleware if you want, and add only the sources you need :