最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Problem with parameterized SQL query in C# while connecting to Sybase SQL Anywhere - Stack Overflow

programmeradmin6浏览0评论

I'm getting unhandled exception:

System.IO.FileNotFoundException: „Could not load file or assembly 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

while executing parameterized query to Sybase SQLAnywhere 16 database. I'm able to properly establish connection to Sybase database. I'm able to execute not secure (SQL Injection vulnerable) query to that database. But this code doesn't work:

    public static DataTable GetData(SAConnection connection, string tableName, string columnName, string filterValue)
{
    DataTable dataTable = new DataTable();
    //
    try
    {
        //string query = $"SELECT * FROM {tableName} WHERE {columnName} = '{Program.ParseFilterValue(filterValue)}'"; // this works
        //
        string query = $"SELECT * FROM {tableName} WHERE {columnName} = ?";
        //
        Console.WriteLine($"query = {query}");
        //
        using (SACommand cmd = new SACommand(query, connection))
        {
            SAParameter parm = new SAParameter();
            parm.SADbType = SADbType.VarChar;
            cmd.Parameters.Add(parm);
            cmd.Parameters[0].Value = filterValue;
            //
            using (SADataReader reader = cmd.ExecuteReader())
            {
                dataTable.Load(reader);
            }
        }
    }
    catch (SAException ex)
    {
        Console.WriteLine($">> Query error: {ex.Message}");
    }
    return dataTable;
}

I've tried many solution suggested here by Stackoverflow community and by Gemini and Perplextity (with DeepSearch) AI chatbot - without success. It seems to me that the problem is that I cannot add or don't know how to add the reference to System.Data.Entity assembly in my VisualStudio 2022 project.

Edit: Refering to comments, my application is simple test, console application written in .NET 8.0. Yes, I've read the post suggested by user Luuk. Here is my project file:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <UserSecretsId>intentionally removed</UserSecretsId>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="EntityFramework" Version="6.5.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.3" />
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.2" />
    <PackageReference Include="System.Data.Odbc" Version="9.0.3" />
    <PackageReference Include="System.Linq.Dynamic.Core" Version="1.6.0.2" />
    <PackageReference Include="System.Security.Permissions" Version="9.0.2" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="iAnywhere.Data.SQLAnywhere.v4.5">
      <HintPath>bin\Debug\net8.0\MyLibs\iAnywhere.Data.SQLAnywhere.v4.5.dll</HintPath>
    </Reference>
  </ItemGroup>

</Project>
发布评论

评论列表(0)

  1. 暂无评论