LINQ to PostgreSQL errors when using DbLINQ - c#

I'm using LINQ to SQL (PostgreSQL) via DbLINQ.
I have a problem doing LINQ to PostgreSQL. I succesfully generated .dbml and .cs files with dbmetal and I think I got all the references, the code compiles.
Refs:
DbLinq
DbLinq.PostgreSql
DbLinq.SqlServer
Npgsql
using DbLinq.PostgreSql;
I get an exception:
{"A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: Named
Pipes Provider, error: 40 - Could not open a connection to SQL
Server)"}
inner exception:
{"The system cannot find the path specified"}
connection string:
server=127.0.0.1;database=xxxxx;user id=postgres;password=xxxxx;
ili
server=localhost;database=xxxxx;user id=postgres;password=xxxxx;
If I try to use NpgsqlConnectionStringBuilder I get:
HOST=localhost;PORT=5432;PROTOCOL=3;DATABASE=xxxxx;USER
ID=postgres;PASSWORD=xxxxx;SSL=False;SSLMODE=Disable;TIMEOUT=15;SEARCHPATH=;POOLING=True;CONNECTIONLIFETIME=15;MINPOOLSIZE=1;MAXPOOLSIZE=20;SYNCNOTIFICATION=False;COMMANDTIMEOUT=20;ENLIST=False;PRELOADREADER=False;USEEXTENDEDTYPES=False;INTEGRATED SECURITY=False;COMPATIBLE=2.0.12.0;APPLICATIONNAME=
with an exception {"Keyword not supported: 'host'."}
I connect succesfully over SquirrelSQL and jdbc driver on Win7 64-bit, Postgres 9.2 64-bit
Edit:
this is fine
xxxxxDC dc = new xxxxxDC("server=127.0.0.1;database=xxxxx;user id=postgres;password=xxxxx;DbLinqProvider=PostgreSql;");
var q = from r in dc.xxxxx
select r;
but i get an error on
dataGridView1.DataSource = q
Now i know this is because the query is not executed immediately. But the problem remains. "The server was not found or was not accessible"
F1!
lp

You're showing a series of quite different errors.
The first one is because you haven't told LINQ to use nPgSQL, so it's trying to use MS SQL Server and it cannot connect - since there probably isn't any MS SQL Server on the machine. That's why the error says while establishing a connection to SQL Server.
You then show an nPgSQL connection string from NpgsqlConnectionStringBuilder and the resulting error about the host keyword. As best I can guess, that's because the connection string produced by that class is intended for nPgSQL's own connection handling routines, not for LINQ. You need a LINQ connection string that specifies the PostgreSQL provider.
You then show another connection string you say is "fine" that has DbLinqProvider=PostgreSql; appended to it, but say you get "an error" on a statement after that. You do not show the error message, nor do you show the code you used to set the connection up, so we cannot really help you. It's really a different question to what you originally asked anyway; please post a new question for a new problem, rather than rewriting your original question.
You need a LINQ provider for PostgreSQL. See this question and the wikipedia page on LINQ. Look at dbLinq, LINQ to Entities with a PostgreSQL driver for Entity Framework, or dotConnect.
At time of writing, nPgSQL does not include a LINQ provider so you need to add a 3rd party one. AFAIK you cannot simply use LINQ with nPgSQL directly.

I think postgresql uses a different default port than 1433. Try specifying the port 5432

It worked after I used ConnectionStringBuilder class to make the connectionstring.
Thanks for your help!

FYI, DbLinq project is dead ) In my case it didn't work right away. Your schema may work now, but later you'll likely have to invest your time to update the project )

Related

Connection forcibly closed while executing the subqueries in the QuickBooks online ODBC driver connection

I am using the trial version of QuickBooks Online ODBC (QODBC) driver and configured the DSN named QuickBooks Online Data as well as QuickBooks Online Data QRemote
I have ensured the connection using the test tool and I am able to execute the SQL queries properly using the mentioned test tool and C# code.
Please find some of the example queries that works fine:
select * from Account
select account.TimeCreated,account.TimeModified, TaxRate.ListID from account
left outer join TaxRate on account.ListID = TaxRate.ListID
select AVG(RateValue),SUM(RateValue) from TaxRate
select Top 5 ListID,RateValue from TaxRate
While trying to execute the subqueries in the test tool, I am facing the following issue.
Subquery used:
select ListID from (select ListID, TimeCreated from Account)
Exception: ERROR [42S00] ExecDirect Packet Header - Received Error:10054,
An existing connection was forcibly closed by the remote host. This normally results if the peer application on the remote host is suddenly stopped, the host is rebooted, the host or remote network interface is disabled, or the remote host used a "hard close"
ERROR [42S00] Error Invalid Socket. Either socket is closed, or the socket reference is invalid.
For more information please visit: qodbc.com/links/invalidsocket
I have tried the suggested solution in the error message, still, the issue is not resolved.
Also, tried the subquery execution in the c# console application as well and faced the same issue.
Anyone, please let me know the possible solution to resolve this issue?
Hopefully this helps:
select tmp.ListID from (select ListID, TimeCreated from account) as tmp
(Not sure about the QODBC syntax, so I went for plain SQL)
Edit:
Could be also that the QODBC system does not support full SQL syntax, so nested selects will just fail (with a very strange error message indeed - but programmers are notoriously bad at writing helpful error messages). You can test this quickly with the simplest nested select possible:
select * from (select 1)
or:
select * from (select 1) as tmp
If both fail, it means the SQL interpreter on the other end of your request is subpar. The workaround would be to just "rephrase" your select statements in a way that avoids this unsupported syntax. I could help with that if I'd know what is the actual request.
I'm not 100% sure on this, but this may be that the ODBC backend being used here (QuickBooks Online) only supports a very limited sub-set of the SQL standard.
You can see the query syntax supported here:
https://developer.intuit.com/app/developer/qbo/docs/learn/explore-the-quickbooks-online-api/data-queries
What seems relevant is to note that QuickBooks Online does not support subqueries AT ALL.
I wonder if QODBC basically just passes the raw SQL through to QuickBooks... and QuickBooks doesn't support it, so it crashes/errors?

EF/MS SQL: A network-related or instance-specific error

There are multiple questions here and in google results regarding this error. But I can't find any situation with the simptoms I have.
So, my code constructs DbContext with connection string buildin from configuration file:
Any query fails with error specified in topic header:
I 100% know that I can connect to the server with such name using credentials specified in connection string:
More, I can communicate with the database using EF infrastructure. For example migrations via VS powershell:
This problem occured after I tried to add LocalDB support on another laptop that has no SQL Server installed.
What I have tried:
obvious SQL Server process/VS/computer restart
fixing connection factory in app.config
reinstalling EF after deleting app.config
hardcoding connection string with SqlConnectionFactory
But still I have no success. What I can do else?
I didn't find root of the problem but was able to fix this issue by removing Automigrating DBContext initializer and updating DB manually.
This is confusing a bit because before I had no problems with automigrations and initializer.

A network related or instance specific error only in my machine

I am getting the below error when I try to connect to SQL server from C# code
A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: SQL
Network Interfaces, error: 26 - Error Locating Server/Instance
Specified)
My connection String looks like this
"Data Source={DatabaseServer};Initial
Catalog={DatabaseName};Integrated Security=SSPI;Enlist=false;"
I have tried all the option which I found in lots of articles but could not solve the issue. One more thing to note is my colleague is able to run the same code from his machine. So it is obviously not problem with SQL server. It has to do something with my machine.can anyone give me any pointers what could be the problem? I am able to connect to SQL server instance through SSMS.
What is the provider in your connection string? Something like
Data Source=SQLServerName;Initial Catalog=DatabaseName;Provider=SQLNCLI11.1;Integrated Security=SSPI;
Well I figured it out...Actually I am working on a remote machine and I created a new project in visual studio which gets created in remote drive by default. so it does not seems to find instance from remote drive but it works when I copy it to my local drive..
Since you can connect to your SQL server through SSMS and the error message looks like the connection string is wrong (or only working for some people somehow), you could compare the parameters SSMS uses in the connection dialog to those in your connection string in the C# code.
If you find any differences there, they could be the error you're looking for.

ASP.NET MVC failure to create database [duplicate]

This question already has answers here:
Cannot Connect to Server - A network-related or instance-specific error
(54 answers)
Closed 6 years ago.
I'm new to ASP.NET MVC and was just messing around with it and trying to see how it works. So I decided to make a query to the database accessed with the default ApplicationDbContext. Normally, if the database hadn't been instantiated before that, this query would make sure to first create the database and then make the query, right? Well, here's what I did:
// This is the default Index action in the Home controller:
public ActionResult Index()
{
var db = new ApplicationDbContext();
var usersCount = db.Users.Count();
return View();
}
I know that the query is pretty meaningless but the sole purpose of it was for it to generate the database. The thing is it that it doesn't. I get a System.Data.SqlClient.SqlException when trying to execute the query (second line of the method).
Even when checking the App_Data folder I see that the database isn't created. What could be the cause of this? Strange thing is that this works on my desktop computer, but not on my laptop. But I did notice that when I put an invalid Data Source(ex. Data Source=.) in the connection string I get the same exception on my desktop computer. So maybe something is happening with the LocalDb that's causing this behaviour.
The connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-DefaultProject-20160731061747.mdf;Initial Catalog=aspnet-DefaultProject-20160731061747;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
This is the full exception:
An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code
Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)
I would be very grateful if someone can come up with a solution for this!
P.S: Everything works fine on my laptop if I change the Data Source to my SQL Server Express server name.
Sounds like your LocalDb instance isn't installed. This is a bit of a "dev attachment" version of SQL that's installed with certain versions of Visual Studio and might have been de-selected or just plain not included perhaps if the original install was older and has gone through upgrades, etc.
This post gives a decent explaination: How to install localdb separately?
Basically google for SqlLocalDb.msi and follow the instructions.
I generally prefer Express over localDb anyway, but that's up to you.
Also note that connection strings are considered "loose" information in the web.config for MVC stuff. That is, they tend to be specific to the machine, so if you're changing machines (or working on a team), your machine is likely to have a different connection string (or a local copy/partial of web.config).

Visual C# 2010: Unable to connect to XAMPP localhost

I am unable to connect to XAMPP localhost in my C# Windows form application.
My code is:
SqlConnection connection = null;
try
{
connection = new SqlConnection("user id=root;" +
"password=12345678;" + "server=localhost");
connection.Open();
}
catch (Exception exptn)
{
MessageBox.Show(exptn.ToString());
}
I made sure to include "using System.Data.SqlClient;" so that my code runs. The timeout error I get at runtime is:
"System.Data.SqlClient.SqlException (0x80131904): A network-related or
instance-specific error occurred while establishing a connection to
SQL server. The server was not found or was not accessible. Verify
that the instance name is correct and that SQL server is configured to
allow remote connections."
There was more to the error message, but it was uncopyable and too long to post here. I have tried to install MySql and the .NET MySql connector separately from XAMPP. I added MySql.Data as a reference in Solution Explorer (My research told me to add MySql.Data and MySql.Web, and MySql.Data was the only one listed in the options.). Another source told me to add a Microsoft ODBC Data Source in Server/Database explorer, but the option was not available in Database Explorer. I have made sure XAMPP is running and working properly in phpMyAdmin, and I am able to create tables and run queries there. Many sources also seem to have different opinions as to what is needed in the connection string.
I am completely at a loss as to how I am supposed to connect to localhost databases from another application on the same computer. Forgive me if I am overlooking something basic or if this is a noob question, but I know little about connecting to databases (this type of thing is precisely the reason why). Can anyone help me connect to XAMPP localhost from C#?
Install MySQL Connector from Oracle and then use class MySqlConnection.
Here is the download link -
http://www.mysql.com/products/connector/
Download the driver for ADO.Net and the documentation to use it is here -
http://dev.mysql.com/doc/connector-net/en/connector-net-introduction.html
SqlConnection is for MQ SQL server only and thus it is looking for a instance of MS SQL Server to connect to and it fails to connect since none exists. This explains the error message.

Categories