我已从 ASP.NET 控制台应用程序连接到我的 SQL Server Compact Edition 数据库。我在我的 ASP.NET Core Web API 应用程序中使用相同的东西和库连接到 SQL Server CE 数据库文件,但出现异常。
我的代码:
public class ClienDetailRepository: IClientDetailRepository
{
private readonly string _connectionString;
private static string DbFilePath = @"C:\Git\Forecaster\DB\THSTaxPro.sdf";
private static string DbPassword = "*****";
private static string DbMaxSize = "4090";
public static string ConnectionString = @"Data Source=" + DbFilePath + ";Password=" + DbPassword + ";Max Database Size=" + DbMaxSize + "";
public ClienDetailRepository(string connectionString)
{
_connectionString = connectionString;
//_connectionString = ConnectionString;
}
}
我在这条线上遇到错误
SqlCeConnection sqlconTHS = new SqlCeConnection(ConnectionString);
这是错误:
“System.Data.SqlServerCe.SqlCeConnection”的类型初始值设定项抛出异常
您使用 Windows 吗?我在 Windows 上测试,没有出现你的问题,你可以参考我下面的步骤。
将
System.Data.SqlServerCe.dll
文件和整个amd64
文件夹从Microsoft SQL Server Compact Edition\v4.0\Private directory
复制到创建.csproj
文件的文件夹:
安装 Windows 兼容包,使任何所需的 Windows 特定 .NET API 可用。
在项目的根目录中添加对
System.Data.SqlServerCe.dll
文件的文件引用,您的 .csproj 文件应该像这样结束(我使用的是 .net6):
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>ProjectName</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="6.0.3" />
</ItemGroup>
<ItemGroup>
<None Remove="amd64\Microsoft.VC90.CRT\Microsoft.VC90.CRT.manifest" />
<None Remove="amd64\Microsoft.VC90.CRT\msvcr90.dll" />
<None Remove="amd64\Microsoft.VC90.CRT\README_ENU.txt" />
<None Remove="amd64\sqlceca40.dll" />
<None Remove="amd64\sqlcecompact40.dll" />
<None Remove="amd64\sqlceer40EN.dll" />
<None Remove="amd64\sqlceme40.dll" />
<None Remove="amd64\sqlceqp40.dll" />
<None Remove="amd64\sqlcese40.dll" />
<None Remove="System.Data.SqlServerCe.dll" />
</ItemGroup>
<ItemGroup>
<Content Include="amd64\Microsoft.VC90.CRT\Microsoft.VC90.CRT.manifest">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="amd64\Microsoft.VC90.CRT\msvcr90.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="amd64\Microsoft.VC90.CRT\README_ENU.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="amd64\sqlceca40.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="amd64\sqlcecompact40.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="amd64\sqlceer40EN.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="amd64\sqlceme40.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="amd64\sqlceqp40.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="amd64\sqlcese40.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="System.Data.SqlServerCe.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Reference Include="System.Data.SqlServerCe, Version=4.0.0.1">
<HintPath>System.Data.SqlServerCe.dll</HintPath>
</Reference>
</ItemGroup>
我的测试代码:
string connectionString = "Data Source=THSTaxPro.sdf";
//using SqlCeEngine engine = new SqlCeEngine(connectionString);
//engine.CreateDatabase();
using SqlCeConnection connection = new SqlCeConnection(connectionString);
connection.Open();
using (SqlCeCommand command = connection.CreateCommand())
{
//command.CommandText = "CREATE TABLE Blogs ( Id INT, Title NVARCHAR(256) )";
//command.ExecuteNonQuery();
command.CommandText = "SELECT * FROM Blogs";
SqlCeDataReader reader = command.ExecuteReader();
int Count = reader.FieldCount;
}
当然,请确保你的connectionString是正确的。
测试结果:
更多详情,您可以参考此链接。