Search code examples
sqlkata

How to connect from SqlKata to postgras database?


I am working with dot net core in visual studio and connected to postgras sql by using Npgsql and now i want to query the database by using sqlkata but i can't connect to my postgras data base, can anyone help me? I install it and follow the instruction base on this link enter link description here but it doesn't connect and throw exception"A network-related or instance-specific error occurred while establishing a connection to SQL Server..."


Solution

  • you should create custom PostgresCompiler

    var connection = new NpgsqlConnection("Host=localhost;Username=username;Password=pass;Database=db name");
                connection.Open();
                var compiler2 = new MyPostgresCompiler();
                var db2 = new QueryFactory(connection, compiler2);
                var list = new XQuery(connection, compiler2).From("employee").Get();
    

    and MyPostgresCompiler is:

    `public class MyPostgresCompiler : PostgresCompiler{
    public MyPostgresCompiler()
    {
        OpeningIdentifier = string.Empty;
        ClosingIdentifier = string.Empty;
    }
    public override string WrapValue(string value)
    {
        return value;
    }}`