ADO.NET: Can't connect to mdf database file - c#

I'm writing an application that uses a SQL Server 2005 database. In the connection string I'm specifying the mdf file like this:
connstr = #"Data Source=.\SQLEXPRESS; AttachDbFilename=" + fileLocation + "; Integrated Security=True; User Instance=True";
When I execute the code:
public static void forceConnection()
{
try
{
conn = new SqlConnection(connstr);
conn.Open();
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if(conn != null)
conn.Close();
}
}
I receive 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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
This code works on XP but not in Vista. I tried to run Visual Studio in admin mode and moved the mdf file to "user data" folders but the error persists.
Any help?

Can you connect to the sql server db in your command prompt? I would make sure you can actually connect first.
Try open the cmd prompt and type sqlcmd -S .\SQLEXPRESS -d your_dbase

If I have mssql-connect problems with my dotnet-sourcecode I try to connect to the database with a different program. I use queryexpress that is also written in dotnet. If this program works then I know that the problem is with my program code else the problem is with connection string, proxy, network, sqlserver or user permissions.

Do you actually have sqlexpress installed? does it use the machineName\sqlexpress or does it run as a default instance?
You have to verify these cases.
and you might wanna use the actual machineName\instance name if you aren't using the default instance.

Related

UWP SQL connection Error [c#]

I try to connect to a SQL Server 2008 instance with SQL Server credentials.
With a WPF app, I have no problem.
I follow this example:
https://learn.microsoft.com/en-us/windows/uwp/data-access/sql-server-databases
I have read this:
How to connect to SQL server database from a Windows 10 UWP app
My connection string is:
static private string connectionString =
#"Data Source=SERVER\SQLSERVER;Initial Catalog=Something;
Integrated Security=false;Persist Security Info=True;User ID=user;Password=pass";
conn = new SqlConnection(connectionString);
try
{
conn.Open();
}
catch(Exception z)
{
Debug.WriteLine(z.ToString());
}
I get this error
System.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the login process. (provider: TCP Provider, error: 0 - L’opération a réussi)
If I try
static private string connectionString =
#"Data Source=SERVER\SQLSERVER;Initial Catalog = Something;
Integrated Security=true;User ID = user;Password= pass";
I get this error
System.Data.SqlClient.SqlException (0x80131904): Failed to generate SSPI context. ErrorCode=DowngradeDetected
The same error with Integrated Security=SSPI
If someone has an idea please?
Thanks
Benoit
You need to check the appropriate capabilities in your appx manifest that are required for accessing the resource on your network. Open Package.appxmanifest in VS and go to the 'Capabilities' tab.
Try this code
optionsBuilder.UseSqlServer("Data Source = SERVER\SQLSERVER; Initial Catalog = Something; Integrated Security = False; User Id = user; Password = pass; MultipleActiveResultSets = True");
Make sure that the TCP is enabled in the SQL Server Configuration Manager
I remember
  The case can be solved by installing a small repair for the server
Before that, try another server
I remembered the third case
Install Kerberos
See link
https://support.microsoft.com/en-us/help/811889/how-to-troubleshoot-the-cannot-generate-sspi-context-error-message
Download and install from the link
https://www.microsoft.com/en-us/download/details.aspx?id=39046
I try with a new VM and SQL EXPRESS 2014
It's working.
I think UWP and SQL SERVER 2008 are not compatible.
Thanks for your help Rami.
Benoit

Cannot open SQL Server database

I downloaded SQL Server 2016 Express and Visual Studio 2015.
I get this message when I try to open SQL Server is my Visual Studio program.
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)
My code is
// _ConnectionString = "Server = ARKY/SQLExpress; Database = Tracker; User Id = Track;Password =Track;Trusted_Connection=True;";
_ConnectionString = "Server = ARKY/SQLExpress; Database = Tracker;Trusted_Connection=True;Connect Timeout=10";
SqlConnection myConnection = new SqlConnection(_ConnectionString);
try
{
myConnection.Open();
return "";
}
catch (Exception e)
{
return e.Message + " " + _ConnectionString;
}
I tried my different connection strings. This is one example.
ARKY is the name of my computer and SQLEXPRESS is my named instance.
See the screen shot of my SQL Server database. I will appreciate any suggestions.
Try this:
_ConnectionString = #"Data Source=ARKY\SQLExpress;InitialCatalog=Tracker;Trusted_Connection=True;Connection Timeout=10";
You need a backslash rather than a front slash in your server name, and several parameters are misnamed.
In general, you can open "Server Explorer" in VS, add a data connection to your server there, then click on the data connection and look for the connection string in the properties window. You can cut and paste from there.
Try to use this one
_ConnectionString = "Data Source=ARKY/SQLExpress;Initial Catalog=Tracker;Trusted_Connection=True;Connect Timeout=10";
_ConnectionString = "Server = ARKY\\SQLExpress; Database = Tracker;Trusted_Connection=True;Connect Timeout=10";
Check your connection string as well
From FIX : ERROR : Could not open a connection to SQL Server:
Check if your SQL server services is up and running properly:
Go to All Programs > Microsoft SQL Server 2016 > Configuration Tools > SQL Server Configuration Manager > SQL Server Services
Check to make sure SQL Server service status is Running.
In addition, ensure that your remote server is in the same network. Run sqlcmd -L to ascertain if your server is included in your network list.
Enable TCP/IP in SQL Server Configuration
When two or more SQL Servers are connected across network they do all communication using TCP/IP. The default port of SQL Server installation is 1433. This port can be changed through SQL Server Configuration Manager. TCP/IP should be enabled for SQL Server to be connected.
Go to All Programs >> Microsoft SQL Server 2016 >> Configuration Tools >> SQL Server Configuration Manager >> Select TCP/IP
Right Click on TCP/IP >> Click on Enable
You must restart SQL Server Services for all the changes to take effect. Right click and go to menu properties to select location where default port of SQL Server can be changed.

Error connecting to mysql server on openshift

Im trying to connect to a mysql database running on openshift.
The computer I have used is running win 10.
Below is my C# code snippet of what im trying to do. The server address, password, and username have been blanked out.
string connStr = #"Server=xxxxxxx.rhcloud.com:3306;Database=hurnhusms;Uid=XXX;password=XXX;";
SqlConnection conn = new SqlConnection(connStr);
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();
Console.WriteLine("Connection successfull !");
conn.Close();
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("error => " + ex.ToString());
}
Console.ReadLine();
when I run the application all I get 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.
i have also ran rhc port-forward , i have also pinged the mysql port on the site and it is open.
Any help would be appreciate.
Okay what I figured out is that I need to execute the rhc port-forward and then keep that window open (not terminate that command). I didn't realize that window need to stay open.

ASP.NET Connection string error when using ip address

I'm trying to debug an ASP.NET page I have trying to connect to a local SQLServer DB, when the connection string I'm using has the name of the local machine, it works fine like in below
string ConnectionString = "Integrated Security=SSPI;" +
"Initial Catalog=ARTICLEDB;" +
"Data Source=MYMACHINENAME\\MYSQLSVR;";
... later on...
conn = new SqlConnection(ConnectionString);
// Open the connection
try
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
//don't work here when using ##.##.##.##\MYSQLSVR as datasource
}
return true;
}
catch
{
return false;
}
when I use the IP machine as the value for the datadource such as: ###.###.###.###\\\MYSQLSVR the I get an exception saying:
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: 28 - Server doesn't support requested
protocol)
This problem originated from trying to run the site from IIS and getting a similar error where the connection was not opened.
How can I reference the DB in the connection string so that IIS can find the DB?
I've also tried adding the connection string in IIS for the site but not sure how to reference that IIS connection string from my project/site
go to SQL Server Configuration Manager and turn on "Named Pipes" and "TCP/IP"
and your connection string should look like below
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;
Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;
http://www.connectionstrings.com/sql-server-2008
Check the following it is working for me.
Data Source=190.190.200.100,1433;Initial Catalog=DBName;Integrated Security=False;user id=userid;password=password

ADO.NET: Open SQL Connection

i want to do some sql code run in my webservice in c#
the code is just
[WebMethod]
public void GetCustomers()
{
SqlConnection MyConn = new SqlConnection("Data Source=./SQLEXPRESS;AttachDbFilename=C:/Documents and Settings/Administrator/My Documents/Visual Studio 2008/Projects/WebService2/WebService2/App_Data/Database1.mdf;Integrated Security=True;User Instance=True");
MyConn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = MyConn;
cmd.CommandText = "delete from t1 where name='1'"; //just see ["+month
// [mon+"] it's imp
cmd.ExecuteNonQuery();
}
now i get error like
System.Data.SqlClient.SqlException: 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)
Correct Format:Server=.\SQLExpress;AttachDbFilename=c:\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;
Try:
"Data Source=./SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\WebService2\WebService2\App_Data\Database1.mdf; Database=YourDatabasName;Integrated Security=True;User Instance=True"
For more details see:
http://connectionstrings.com/
You might want to check the configuration of your SQL Server as well.
The error remarks the issue could be the named pipes not being active, this is the default setting.
You can always test this fix by going;
Start -> SQL Server xxxx -> Configuration Tools -> SQL Server
Configuration Manager
Where xxxx is the version of SQL Server you are using.
In the tree look at the
-> SQL Native Client xx Configuration -> Client Protocols
Named Pipes is listed.
Set it to being Enabled
Then under
SQL Server Network Configuration -> Protocols for xxxxxx
xxxxxx Being the name of your SQL Server database instance name.
Check that Named Pipes are Enabled there also.
You will need to restart your SQL Server database in order for it to accept Named Pipe calls.
Might be worth a try.

Categories