I'm trying to create a database in SQL Server Express using WinForms and C#
Here is what I'm trying to do
Microsoft.SqlServer.Management.Smo.Server srv = new Microsoft.SqlServer.Management.Smo.Server srvServer();
int i = srv.Databases.Count;
just to get the count at the start. But I get the error
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) this is the stack track
at Microsoft.SqlServer.Management.Common.ConnectionManager.Connect()
at Microsoft.SqlServer.Management.Common.ConnectionManager.get_ServerVersion()
at Microsoft.SqlServer.Management.Smo.ExecutionManager.GetServerVersion()
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.GetDbComparer(Boolean
inServer)
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.InitializeStringComparer()
at Microsoft.SqlServer.Management.Smo.AbstractCollectionBase.get_StringComparer()
at Microsoft.SqlServer.Management.Smo.SimpleObjectCollectionBase.InitInnerCollection()
at Microsoft.SqlServer.Management.Smo.SmoCollectionBase.get_InternalStorage()
at Microsoft.SqlServer.Management.Smo.SmoCollectionBase.InitializeChildCollection(Boolean
refresh)
at Microsoft.SqlServer.Management.Smo.SmoCollectionBase.get_Count()
at CreateDB.CreateDB.btnCreateDB_Click(Object sender, EventArgs e) in C:\Users\Guest1\Downloads\CreateDB\CreateDB\CreateDB.cs:line 82
What should be done?
If you're using SQL Server Express, and you've installed with all the defaults, then your server instance will be called .\SQLEXPRESS. You need to use that in your code:
using Microsoft.SqlServer.Management.Smo;
Server srv = new Server(".\\SQLExpress");
int i = srv.Databases.Count;
If you create a new Server instance without specifying an instance name, it tries to connect to the default instance (with no name) - which you don't have, if you've installed just SQL Server Express.
First make the connection by using the SqlConnection object. you should do this
SqlConnection conn = new SqlConnection(#"Data Source=.\SQLExpress;Initial Catalog=master;Integrated Security=True");
Microsoft.SqlServer.Management.Smo.Server server = new Microsoft.SqlServer.Management.Smo.Server(new ServerConnection(conn));
int i = server.Databases.Count;
Related
I have this connection string and working well when I test my aplication from the same SQL Express, I have an instance called DOKUSTAR.
using (SqlConnection conn = new SqlConnection(#"server=.\DOKUSTAR;Database=RdaDB10;Trusted_Connection=Yes"))
using (SqlCommand comm = new SqlCommand(#"select top(1) pais, clase, operation from Document_Class where codigoafip = #codafip", conn))
The problem is when I tried to connect from outside of the server does not works the connection and show me the following messages:
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).
Any suggestions will be greatly appreciated.
.\DOKUSTAR refers to an instance of DOKUSTAR on the current machine.
You need to change it to MACHINE_NAME\DOKUSTAR
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
Hi I am trying to connect to a local SQL Server Compact database (.sdf) in a Windows forms project and have been facing this problem for quite some time. I am not allowed to use datasets for the project, all the queries and connections are written in the application.
System.Data.SqlClient.SqlException
A network-related or instance-specific error occurredwhile 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)
Code:
SqlConnection _Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["restaurant"].ToString());
SqlCommand _Command = _Connection.CreateCommand();
_Connection.Open(); // <- throws exception
To connect a Sql Server Compact you need a different set of classes contained in the namespace SqlServerCe (SqlCeConnection, SqlCeCommand and so on....)
SqlCeConnection _Connection = new SqlCeConnection(ConfigurationManager.ConnectionStrings["restaurant"].ToString());
SqlCeCommand _Command = _Connection.CreateCommand();
_Connection.Open();
of course, you need to reference the assembly that contains the above mentioned classes.
System.Data.SqlServerCe.dll (ADO.NET provider)
and add the using statement
using System.Data.SqlServerCe;
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
I've got an SQL server and database setup on an external server (let's call the domain name "hello.com" for the purposes of this), and I want to connect to this server via a C# program. So far I have this (All server/database details are different to the real ones):
private static void SetupSQL()
{
string connectionString = "server=hello.com; database=db1; uid=user1; pwd=xxxxx;";
connection = new SqlConnection();
connection.ConnectionString = connectionString;
try
{
connection.Open();
Console.WriteLine("Connected");
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
This is giving me an error message:
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)
I have checked all the connection string, and I am allowed remote access, as I have SQLWorkbench open querying the database right now on the same computer.
Any ideas?
You'll need the MySQL driver:
http://dev.mysql.com/downloads/connector/net/
You can then use the the MySqlConnection connection class to connect.
MySqlConnection connection = new MySqlConnection(connectionString);
http://www.codeproject.com/Articles/43438/Connect-C-to-MySQL
You can't use SqlConnection object to connect to MySQL database, you should use MySqlConnection instead after you import its dll