Error trying to connect to SQL Server using C# - c#

My code is as follows:
string constring = "Data Source=132.186.127.169"+ "Initial Catalog=CadPool1;" + "Integrated Security=True";
SqlConnection con;
con = new SqlConnection(constring);
con.Open();
string query="SELECT * from CadPoolProjectTable1";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
MessageBox.Show("selected");
con.Close();
I am getting error at the line con.Open();. The error is:
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)

You are missing a ';' after the server name in the connection string.
string constring = "Data Source=132.186.127.169"+ "Initial Catalog=CadPool1;" + "Integrated Security=True";
It should be
string constring = "Data Source=132.186.127.169;"+ "Initial Catalog=CadPool1;" + "Integrated Security=True";
The error says that your app was not able to connect to the server. I would do the following.
Check for the server address (on a quick look it looks good)
Connect using management studio and in your case it should have worked.
It means the issue is with the code. Since you are concatenating the string I would debug the code and see what the end result for the connection string is.
Tip:If it is a web application add the connection string to web.config file. More info here How to: Read Connection Strings from the Web.config File

You're missing a semicolon in your connection string
Data Source=132.186.127.169;"+ "Initial...
^
If you need to build the connection string yourself you can use the SqlConnectionStringBuilder class. That way you won't to be as troubled by these subtle mistakes.

Your connection string is wrong:
string constring =
"Data Source=132.186.127.169;Initial Catalog=CadPool1;Integrated Security=True";
You don't need to concatenate the strings together, but more importantly, you were missing the semi-colon ";" between the data source and the initial catalog settings.

First, you are missing a ; (semicolon) between Data Source and Initial Catalog.
Second, if this is a newly installed SQL Server instance, you may need to go into SQL Server Configuration Manager, and enable the protocol(s) you'll need.

Please check if you can ping the server mentioned via cmd
Also try telnet to the server from your machine.
One more thing to check would be port the server is configured for if its not the default one you will have to add the port as 132.186.127.169,XXX

Servername in the connection string is wrong. and also since there are no dynamic values you don't need string concatenation. Change it to:
string constring = "Data Source=132.186.127.169;
Initial Catalog=CadPool1;
Integrated Security=True";

Related

Oracle Connection in C# - connection string

I'm currently trying to build an application in C# and connecting it to a live db running in Oracle 11g.
I have the following connection details
Host IP: 10.204.1.3
Port: 1521
DB Name: PROD
My source code
string connString = "DATA SOURCE=10.204.1.3:1521/PROD;PERSIST SECURITY" +
"INFO=True;USER ID=username; PASSWORD=userpass";
OracleConnection conn = new OracleConnection(connString);
conn.Open();
I was able to add a connection in Server Explorer with the Connection String used by VS but is having the error below in conn.Open();
An unhandled exception of type 'System.NullReferenceException' occurred in
Oracle.DataAccess.dll
Sorry if this is a basic question, I'm new in VS, and Oracle and can't find the solution in the other part of the web. Thanks in advance.
My code is now working. I should've read the Oracle documentation (reference below).
string connString = "DATA SOURCE=10.204.3.1:1521/PROD;" +
"PERSIST SECURITY INFO=True;USER ID=username; password=password; Pooling
=False;";
OracleConnection conn = new OracleConnection();
conn.ConnectionString = connString;
conn.Open();
Reference: http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/appdev/dotnet/Web_version_Fully_Managed_ODPnet_OBE/odpnetmngdrv.html

How to connect an application to a SQL Server using IP Adress in C#?

My problem is that I have created a SQL Server database but I access it locally, only from my own computer. What I want to do is to connect the app to a server and to save all the data there.
The code I used to connect to the SQL Server database is:
SqlConnection con = new SqlConnection(#"Data Source=(localdb)\v11.0;AttachDbFilename=C:\Users\donca\Desktop\Memo\Memo\ContNou.mdf;Integrated Security=True");
SqlCommand cmd1 = new SqlCommand("SELECT * FROM [dbo].[Cont] WHERE Nume_utilizator = #Nume_utilizator and Parola = #Parola;", con);
cmd1.Parameters.AddWithValue("#Nume_utilizator", this.Nume_utilizator.Text);
cmd1.Parameters.AddWithValue("#Parola", this.Parola.Text);
cmd1.Connection = con;
con.Open();
My question is: how can I change this to connect and access data from a SQL Server and save data there?
Just change the connection string:
"Data source=ServerName\InstanceName;"
You can use the IP address instead of the servername.
The default instance name is MSSQLSERVER.
Make sure you turn on "Remote connections" on the distant server. You can use Management studio -> Server properties -> connections -> Allow remote connections.
If it dosn't work check that the TCP/IP protocol is activated for you instance.
You can find it in SQl Server Configuration Manager.
You just have to change your connection string. That's all. For an example, if your server name is CORP and the SQL instance name is SQL2012, your connection string will be like this.
SqlConnection con = new SqlConnection(#"Data Source=CORP\SQL2012;Initial Catalog=ContNou;Integrated Security=True");
In case if you cannot find the SQL server by its name, you can use the IP too.
SqlConnection con = new SqlConnection(#"Data Source=10.4.2.208;Initial Catalog=ContNou;Integrated Security=True");

connecting with Sqlexpress

i am trying to connect the sql with a database on server. So when i entered the data source. its showing me Error Unrecognized escape sequence
Please help me out.
string constring = "Data Source=DVSQL\SQLEXPRESS;Initial Catalog=DB;User ID=user;Password=****";
i tried below both ways then its showing me Error Unable to connect any of specified SQL host
string constring = "Data Source=DVSQL\\SQLEXPRESS;Initial Catalog=DB;User ID=user;Password=****";
string constring = "Data Source=DVSQL/SQLEXPRESS;Initial Catalog=DB;User ID=user;Password=****";
What is the mistake i am doing when i check the connection using TEST BUTTON.. its showing me TEST CONNECTION SUCCEEDED . When i am calling from program its not connection. Why its like that. i am using VS 2013
Placing an # sign before your first sql connection statement will prevent the string from including escape sequences.
string constring = #"Data Source=DVSQL\SQLEXPRESS;Initial Catalog=DB;User ID=user;Password=****";
Maybe something in the password is throwing it off.

Can't connect to SQL Server DB

I'm trying to connect to a DB and I followed the connection string suggestions. However, I'm not a DB-guy so some things are, hrmp... less than obvious.
For instance, the DB server is within the network and the connection to it goes from another server, also in the same network. I've used the server name and port to connect to it using Management Studio so it's up and running.
This is my connection string.
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
conn.ConnectionString =
"integrated security=SSPI;"+
"server=server.name.as.in.management.studio,4340" +
"persist security info=False;database=NameOfTheDb";
The authentication is done using AD and the error message is 40 - can't find the server. Besides the obvious - the server can't be found - what can I do to trouble-shoot this, obtain more information etc.?
I'm at a customer and their system is not as well documented as one'd like. I get very little information and the coverage is questionable. The person who set up the attrocity is gone since a long time.
Suggestions are welcome.
EDIT
Following the corrections provided, I'm getting error code 0 - The requested name is valid but no data of the requested type was found.
What do I do with this?! :)
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
conn.ConnectionString =
"integrated security=SSPI;"+
"Data Source=myServerAddress;" +
"persist security info=False;" +
"Initial Catalog=NameOfTheDb";
Hope this helps.
Sure servce in Managmnet Sql Server is Start.
You can do that by go to ---> Control Panel-->All Control Panel Items-->Administrative Tools--Services --> SQL Server(MSSQLSERVER) and click Start.
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlDbConnection();
conn.ConnectionString =
"Data Source=ServerName;" +
"Initial Catalog=DataBaseName;" +
"User id=UserName;" +
"Password=Secret;";
conn.Open();

How to backup from .mdf database that I created

I created a .mdf database file with Visual Studio 2008. I can retrieve and insert data into database but when I want to backup I receive an error.
My code:
string con = #"Data Source=.\SQLEXPRESS;AttachDbFilename=|C:\test\Data|\DB.mdf;Integrated Security=True;User Instance=True";
connect = new SqlConnection(con);
connect.Open();
SqlCommand command = new SqlCommand(#"backup database [" + System.Windows.Forms.Application.StartupPath + "\\Data\\DB.mdf] to disk ='"+str+"' with init,stats=10",connect);
command.ExecuteNonQuery();
connect.Close();
MessageBox.Show("The support of the database was successfully performed", "Back", MessageBoxButtons.OK, MessageBoxIcon.Information);
The error is:
error : invalid value for key 'attachdbfilename'.
Seems like your connection string is incorrect.
Try this one:
string con = #"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\test\Data\DB.mdf;Integrated Security=True;User Instance=True";
For more options, have a look at: http://www.connectionstrings.com/sql-server-2005
This is for SQL Server 2012 and .NET 4.0.1 only.
If you have those, you should be able to use AttachDbFilename.
Anyway, if you have an .MDF for embedded database and the instance is not running, you can just copy .MDF and .LDF to back up.
just use your connection string as
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["<your connection string name from your app.config file>"].ConnectionString);
i tried it and it worked for me.

Categories