Can not connect to local database with c# System.Data.SqlClient - c#

I have some problems with connecting to my local DB using c# System.Data.SqlClient.
Information:
DB parameters: Name:baza Host:localhost Instance:baza Port:3306 Schema:world
My connection string: #"server=localhost\baza;password=pass;User Id=root;database=world;Persist Security Info=True"
using: c#, System.Data.SqlClient, MySQL Workbench
firewall off
succesfull conection with JDBC, so server is working
JDBC parameters:
String url = "jdbc:mysql://localhost:3306/";
String driver = "com.mysql.jdbc.Driver";
String dbName = "world";
String username = "root";
String pass = "pass";
Problem:
when I use conn.Open(); this happen => (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
Tried:
server=localhost then it gives => error: 40 - Could not open a connection to SQL Server
many other stupid combinations like server=baza
http://blogs.msdn.com/b/sql_protocols/archive/2007/05/13/sql-network-interfaces-error-26-error-locating-server-instance-specified.aspx
ASP.NET beginner question: How to get rid of this error?
Connection to freesql.org server also failed
I have no idea what to do... Sth is probably with server name.
EDIT:
code:
string connstr = #"Data Source=localhost\baza;password=qspass;User Id=root;"+
"Initial Catalog=world;Persist Security Info=True";
Console.WriteLine(connstr);
SqlConnection conn = new SqlConnection(conbuild.ConnectionString);
conn.Open();
Console.WriteLine("YO!");
Some more code...
Never reached YO! :P

I suppose I should put my answer in the answer section, rather than a comment :)
I see from your JDBC connection string that you are using MySQL rather than SQL Server.
The System.Data.SqlClient namespace is the.NET Framework Data Provider for SQL Server, not MySQL.
If you are using MySQL, you will need to use the MySQL ADO.NET driver available here:
Download Connector/Net

Related

Visual Studio connects to database, but application within using the same connection string cannot

Using SSMS and Visual Studio 2015's Server Explorer tab under Data Connections, I can execute queries on remote server KOSH without issue. Why is the MVC application running locally in Visual Studio/IIS Express unable to do the same?
Using VS2015's connection Properties, I get its connection string:
Data Source=123.456.78.9;Persist Security Info=True;User ID=Foo;Password=Bar
Using that connection string, the MVC application is greeted with:
"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 message:
"The network path was not found"
I know the server/firewall/network settings are correct. That leaves the application.
using(var connection = Database.Connection(conInfo.ConnectionString, provider)) {
// rowCountSql: SELECT SUM(rows) TM_ROWCOUNT FROM sys.partitions
// WHERE object_id = object_id(#tableName) and index_id IN (0,1)
// Any other SQL yields same error
var countTask = connection.QueryAsync(rowCountSql, new { tableName = Editor.TableName });
}
Async is used because the Oracle version of the query can take several seconds to return.
The application should be correct because it connects to the same server from home (also the server's location) and to identical databases in remote data centers running 2008R2 to 2014 without a problem.
Database.Connection() is:
// This is unlikely to be the problem as it is very well tested
public static DbConnection Connection(string connectionString, string databaseProvider) {
DbProviderFactory databaseFactory = DbProviderFactories.GetFactory(databaseProvider);
DbConnection connection = databaseFactory.CreateConnection();
if(connection != null)
connection.ConnectionString = connectionString;
return connection;
}
I bet I am missing something simple, but I would be grateful for help on what that could be.
As I said in comments, maybe the piece of your code:
conInfo.ConnectionString
does not contains the correct connection string you want.
Check it out

Connection Error in SQL Server Compact with ASP.Net

I am trying to access a database, using a DataSet. I am using Visual Studio for Web Express 2013, ASP Web Forms project with SQL Server Compact 4.0. I created the database from Database Explorer, added 3 tables, now my code has getData, setData methods which will use Dataset to get data.
But the SqlConnection object is throwing exception at runtime.
[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. (provider: SQL Network Interfaces, error:
26 - Error Locating Server/Instance Specified)]
I guess this is due to connection string since error is at line:
SLQCon.Open();
I have tried more than one method but none of them has worked yet. These are few of them,
msdn link
Connection Strings
The other things work fine (web form controls), only problem is when I do something related to database, any fixes?
My code is as follows:
void setData(string tableName) {
string ConString = #"Data Source=" + "F:\\Github\\JournalClassifier\\WebApplication1\\WebApplication1\\App_Data\\KeywordsDB.sdf" + ";Connect Timeout=30";
SQLCon.ConnectionString = ConString;
SQLCon.Open(); // Exception here
// insertion code
}
Make sure your database inside App_Data folder on your root application. and change the connection string to this one
<add name="ConnectionStringName"
providerName="System.Data.SqlServerCe.4.0"
connectionString="Data Source=\KeywordsDB.sdf;Connection Timeout=30" />
things you need to concern is you are using SQL Server Compact, so the provider name need use System.Data.SqlServerCe.4.0 otherwise it will confused with SQL Server Client

Can't create connection to an sqlite DB

I've made a code that create the database automatically in the folder where the software is executed. The DB was created succesfull, but when I create the object for the connection this exception is displayed:
System.Data.SqlClient.SqlException (0x80131904): There was a network error or specific instance while trying to establish 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) ---> System.ComponentModel.Win32Exception (0x80004005): Can not find the file specified
on this line:
con.Open();
but the problem I think is on this declaration of the connection:
static SqlConnection con = new SqlConnection(
"Server=localhost;Database=AppDB.sqlite;");
What am I doing wrong?
UPDATE Details:
SqlCommand command = new SqlCommand(#"INSERT INTO Team (name,code, shortName, squadMarketValue,
crestUrl, link_self, link_fixtures, link_players, caption) VALUES (#name,
#code,#shortName,#squadMarketValue,#crestUrl,#link_self,#link_fixtures,
#link_players,#caption)", con);
How you can see I'm using parametizer query, and if I use this object:
static SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=SoccerForecast.sqlite;Version=3;");
the con variable is underlined in red, an the compilers tell me:
it is not possible to convert SqliteConnection in SqlConnection
Your connection is wrong. You didn't specified the kind of database you're trying to connect to in your question. There's a mention of SQLite while your are using an sql connection object. The sql connection object is for connection to the ms sql server database. If you really want to connect to an ms sql server database, you'll need to change the connection string to provide a username / password or a Trusted_Connection=True to use your windows account.
However, if the database you are trying to connect to is an SQLite DB, you will have to use a different connection object. This and this SO question has answers about available libraries and examples that will explain how to create a connection to a SQLite database.
The site connectionstring.com is also a good resource when you need to generate a connection string.

ODBC connection to SQL Server 2012 LocalDB instance

We have a "legacy" application, which uses ODBC connections to an underlying database, which can be Access, Oracle or SQL Server. For unit (or, perhaps more properly, "integration") test purposes, I'd like to hook up a SQL Server 2012 LocalDB instance. However, I cannot figure out a correct ODBC connection string to use.
I have tried:
[TestMethod]
public void OdbcConnectionToLocalDb()
{
string connectionString = "DRIVER=SQL Server Native Client 11.0;Trusted_Connection=Yes;SERVER=(localdb)\v11.0;Description=LocalDB;";
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
using (var command = new OdbcCommand("select * from Person", connection))
{
connection.Open();
// ...
}
}
}
However, when the connection is opened, the following exception is thrown:
System.Data.Odbc.OdbcException: ERROR [08001] [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [67].
ERROR [HYT00] [Microsoft][SQL Server Native Client 11.0]Login timeout expired
ERROR [08001] [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
Is it possible to connect to a SQL Server 2012 LocalDB via an ODBC connection/driver? Is it possible to connect to a specific file?
[EDIT]
Garrett points out it is possible, great. I must have the connection string wrong, so my question really should be: what should the connection string be?
You need to specify your connection string like this:
Provider=SQLNCLI11.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestDB;Data Source=(localdb)\v11.0
The main thing I think is that you reference it as a data source rather than server.
Yes, it's possible. Make sure you install the latest driver: SQL Server Native Client "Denali" (for ODBC and OLE DB).
Look here for more info:
http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx

windows Application Database Connection c#

Im facing problem with database connection on windows Application Database Connection c#... here is my connection ,,, PC2 is PCNAme
private static SqlConnection con = new SqlConnection(
"Data Source=PC2\\SQLEXPRESS; Initial Catalog=Database1;Integrated Security=True"
);
when I run the form I get this unhandled exception on con.Open();
sqlException was unhandeled:
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)
hope some one can help Im trying to solve it for many hours but not working.
Check this trouble shoot steps on MSDN:
The SQL Server client cannot connect to the server MSDN
And much better if you separate your connection string to your ado.net sql connection.
private const string _thisConnectionString = "Data Source=PC2\\SQLEXPRESS; Initial Catalog=Database1;Integrated Security=SSPI";
Use the "Using" Statement ensure that you always close your Connection.
Best Practices for Using ADO.NET MSDN
using (var conn = new SqlConnection(_thisConnectionString))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "Your SQL Query STuff";
conn.Open();
}
Regards
Try This:
private static SqlConnection con = new SqlConnection(
"Data Source=PC2\SQLEXPRESS; Initial Catalog=Database1;Integrated Security=True"
);
Go to SQL Server Configuration Management -> Click on SQL Server Services
Verify SQL Server and SQL Server browser both are running. you can see a Greed play button there. if it's not running right click on the service which shows Red, go to properties, Click Built in Account and Press Apply then OK, There try to start by right click.

Categories

Resources