-1

I added System.Data.SqlServerCe.dll v3.5 in my ASP.NET Core Web API project, but I'm getting error while connecting to SQL Server CE database.

This is my code:

 private static string DbFilePath = @"C:\Git\Forecaster\DB\THSTaxPro.sdf";
        private static string DbPassword = "123567";
        private static string DbMaxSize = "4090";
        public static string ConnectionString = @"Data Source=" + DbFilePath + ";Password=" + DbPassword + ";Max Database Size=" + DbMaxSize + "";

        [Route("thsAdditionalincome")]
        public async Task<IEnumerable<AdditionalIncome>> thsAdditionalincome(string accountid)
        {
            try
            {
                using (var conn = new SqlCeConnection(_connectionString))
                {
                    var query = "select irs_account_id,income_alimony,income_alimony_taxpayer,income_alimony_spouse,income_child_support ,  income_child_support_taxpayer,income_child_support_spouse ,income_net_business, income_net_business_taxpayer, income_net_business_spouse , income_net_rental , income_net_rental_taxpayer , income_net_rental_spouse ,   income_pension ,  income_pension_taxpayer ,income_pension_spouse, income_interest_dividends , income_interest_dividends_taxpayer,income_interest_dividends_spouse, income_social_security,income_social_security_taxpayer,income_social_security_spouse,income_other_1,income_other_1_taxpayer,income_other_2,income_other_2_taxpayer,income_other_2_spouse from t_financial_analysis_individual  where IsDeleted=0 and irs_account_id=@accountid";
                    var parameters = new DynamicParameters();
                    parameters.Add("@accountid", accountid, DbType.String);

                    conn.Open();

                    var results = await conn.QueryAsync<AdditionalIncome>(query, param: new { accountid });

                    return results.ToList();
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
            



        }

This is my Code

This is the error I am getting:

This is The error i am getting

  • Do not post text as pictures. Code, error messages, etc, should all be posted as text, formatted appropriately. If you do post a screenshot, don't show your entire screen when only part of it is relevant. The snipping tool built into Windows can select an arbitrary box so there's no excuse not to. – jmcilhinney Apr 19 '23 at 05:04
  • First of all - if at all, you should use SQL Server CE **v4.0** - 3.5 is beyond dead..... and even SQL Server CE 4.0 is no longer officially supported - I'd strongly recommend alternatives, like either SQL Server Express / LocalDB, or SQLite, if you need an "embedded" database system – marc_s Apr 19 '23 at 05:43

1 Answers1

0

SQL CE is out of support. But there is a way you can use SQL CE 3.5 in asp.net core project.

Step1. Go to CE3.5 installation folder. Copy all .dll files except system.data.sqlserverce.entity to a new folder named amd64 ,paste this folder to project root.

enter image description here

Step2. from private folder copy system.data.sqlserverce.dll to project root enter image description here

You project root is like
enter image description here

Step3. open .csproj add the following reference

    <ItemGroup>

        <Content Include="amd64\sqlceca35.dll">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
        <Content Include="amd64\sqlcecompact35.dll">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
        <Content Include="amd64\sqlceer35EN.dll">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
        <Content Include="amd64\sqlceme35.dll">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
        <Content Include="amd64\sqlceoledb35.dll">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>  
        <Content Include="amd64\sqlceqp35.dll">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
        <Content Include="amd64\sqlcese35.dll">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
        <Content Include="System.Data.SqlServerCe.dll">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
    </ItemGroup>

    <ItemGroup>
        <Reference Include="System.Data.SqlServerCe">
            <HintPath>System.Data.SqlServerCe.dll</HintPath>
        </Reference>
    </ItemGroup>

Step4.install nuget packageMicrosoft.Windows.Compatibility latest version.

Now restart your project, SqlCeConnection will work.

All .csproj

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

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

  <ItemGroup>
    <PackageReference Include="Microsoft.Windows.Compatibility" Version="7.0.1" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
  </ItemGroup>

    <ItemGroup>

        <Content Include="amd64\sqlceca35.dll">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
        <Content Include="amd64\sqlcecompact35.dll">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
        <Content Include="amd64\sqlceer35EN.dll">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
        <Content Include="amd64\sqlceme35.dll">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
        <Content Include="amd64\sqlceoledb35.dll">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>  
        <Content Include="amd64\sqlceqp35.dll">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
        <Content Include="amd64\sqlcese35.dll">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
        <Content Include="System.Data.SqlServerCe.dll">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
    </ItemGroup>

    <ItemGroup>
        <Reference Include="System.Data.SqlServerCe">
            <HintPath>System.Data.SqlServerCe.dll</HintPath>
        </Reference>
    </ItemGroup>
</Project>
Qiang Fu
  • 1,401
  • 1
  • 2
  • 8
  • I try above all activity but my code given this error Unable to load DLL 'sqlceme35.dll' or one of its dependencies: The specified module could not be found. (0x8007007E) – Ravindra Singh Apr 20 '23 at 12:04
  • @RavindraSingh DId you install other sqlserverce related nuget package?If you have try remove them all. – Qiang Fu Apr 20 '23 at 14:59
  • nuget package i install list are 1.Dapper 2.Microsoft.Windows.Compatibility 3.Swashbuckle.AspNetCore.Swagger – Ravindra Singh Apr 21 '23 at 04:49
  • @RavindraSingh It's really strange, I referenced this blog with verstion 3.5 and also works well. You can check if there is anything help. https://erikej.github.io/sqlce/2020/08/17/netcore-sql-compact.html. I used .net6 for test. – Qiang Fu Apr 21 '23 at 05:10
  • this blog on 4.0. i upgrade 3.5 to 4.0 database working fine but i don't want to update my database because other related software not working after update – Ravindra Singh Apr 21 '23 at 05:36
  • @RavindraSingh I paste all my working sample csproj at the end of answer, you can have a look. – Qiang Fu Apr 21 '23 at 05:45
  • @RavindraSingh Or maybe reinstall v3.5?https://www.microsoft.com/en-us/download/details.aspx?id=5783 I didn't do any other steps. – Qiang Fu Apr 21 '23 at 05:51