I am facing some issues with connectivity .
Question-
1)Is it possible that Web application access the databse but not console application?
2)When I am connecting my console app to our databse on server-"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible ",this error is coming though when I test the connection using setting tab in VS2010 for console application,it says Connection succeed,but as soon as i write Con.open,I get the error mentioned.
3)Also,If i am not able to connect to my db with new application,how come previous build web application on production are working fine.?
NOTE-There has been massive change in Server-Sql instances and all servers were upgraded.
Do I make sense?
Code I am using-
SqlConnection con;
SqlDataReader reader;
try
{
int id;
con = new SqlConnection("Data Source=DAN\\C1;Initial Catalog=tablenames;User ID=user;Password=123");
con.Open();//Error comes here
Exact same connection string works for webapplication but not for Console.Is it have something to do with sql server upgarde and changes.
Any help or guidance?
okay well from what i can see you may want to check that you have imported and used all the required namespaces involved in an SQL connection , you will need to import the following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
After ensuring all the correct namespaces have been imported then you should check your connection string maybe there is a typing error or something there , remember if you make a mistake on the connection string the connection will be refused , and im not sure if this code will work but here goes try this method of connecting :
SqlConnection con;
SqlDataReader reader;
connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
con = new SqlConnection(connetionString);
try
{
int id;
con.Open();
Console.WriteLine("Connection Open ! ");
cnn.Close();
}
catch (Exception ex) { Console.WriteLine("Can not open connection ! "); }
If the above code gives you problems you can try this other method of doing a SQL connection in a C# console project as seen in the link below:
http://www.c-sharpcorner.com/UploadFile/5089e0/how-to-create-single-connection-string-in-console-applicatio/
First check in the Server Explorer whether you are connected to the Server or database.If u connected to the server, server name will be visible under data connection, right click on it go to properties there u can find connection string value. copy paste that value in your sql connection code. Try this it may solve your connection string problem.
Thanks everyone.But it was some security issue.Once I deploy my code on server,its working.Remote connectivity was somehow restricted in the sql server.
Related
I tried to connect my c# app to the SQL Server Analysis Services server, to execute some MDX query.
The connection is done as follows :
try{
string connetionString;
connetionString = #"Data Source=localhost;Catalog=SalesCubeDB;";
AdomdConnection cnn;
cnn = new AdomdConnection(connetionString);
cnn.Open();
Console.WriteLine("Connection established !!");
}catch(Exception e)
{
Console.WriteLine(e.ToString());
}
This connection throws the following error :
AdomdConnectionException: A connection cannot be made. Ensure that the server is running
I tried Servername instead of 'localhost', I added rules in the firewall for the traffic and also made sure that the Server Browser uses 'Local System'. If somebody has any ideas i will be grateful, THANKS.
Environment : Windows 10 VM, SQL server 19, ADOMD version 19.26.1.2
I solved my issue and this answer to everyone who might stumble on the same issue.
The client app is a console app, so I added the following packages :
System.Configuration.ConfigurationManager
Microsoft.ServiceFabric.Services.Remoting
I also added the following to the Connection String (I use Windows authentification) :
persist security info=True;Integrated Security = SSPI;
I also tried connecting using IIS 8,0 and worked perfectly.
I am trying to create a local database using MySql in Visual C# but connection is not getting established in the code but when i add the server in the server explorer under data connections its working. I tried test connection it says connection succeeded but its not working in the code.
string connStr = "server=localhost;user=root;database=entry_database;password=superadmin";
con = new SqlConnection(connStr);
This throws an error 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
You cannot use the SqlConnection class to connect to a MySQL server; that is for Microsoft SQL Server only.
Instead, install the MySqlConnector NuGet package and use the following code:
string connStr = "server=localhost;user=root;database=entry_database;password=superadmin";
using (var con = new MySqlConnection(connStr))
{
// ...
}
Your connection string is wrong.. change keys "user" to "Uid" and "password" to "Pwd".
follow eg:
string connStr = "server=localhost;Uid=root;database=entry_database;Pwd=superadmin";
con = new SqlConnection(connStr);
Correct, the format used by you to create the connection string is incorrect, giving you the error you mentioned above.
Actually, the way the connection string is written in MS-SQL is very different from MY-SQL. Every database has its own way of connecting and its individual connection tags:-
Please find the list of connection details for MS_SQL below. This would help you not only in the current mentioned scenario but also will provide a reference for future changes:-
https://www.connectionstrings.com/mysql/
I've got a little problem between oracle and C#. I can't seem to connect to my database(=localhost) within my application. I'm using Oracle XE11.2. The error occurs everytime I'm calling "conn.Open()" and Oracle returns this error: {"ORA-12154: TNS:could not resolve the connect identifier specified"}.
I'm 100% sure that the User Id and the Password are correct because I can login to the database via SQL Developer. I think the problem might be in the data source but I'm not sure. Anyone who can help out?
Here is my code:
string connstring = "Data Source=xe;User Id=Software;Password=Software";
//Open connection
OracleConnection conn = new OracleConnection(connstring);
conn.Open();
Did you try tns ping "tnsping xe" from the machine where you are running your program?
I have an AWS EC2 instance running. I've tried to make a connection to my MySQL like this in C#
MySqlConnection l_DBConn = new MySqlConnection();
l_DBConn.ConnectionString = "Server=my-ec2.compute-1.amazonaws.com;Port=3306;Database=mydatabase;uid=root;password=mypassword;port=3306;charset=utf8"
MySqlCommand command = l_DBConn.CreateCommand();
command.CommandText = "select * from users";
MySqlDataReader Reader = command.ExecuteReader();
The error received when I run the last line is "Connection must be valid and open.". I still can connect to my instance using Filezilla,Putty and MySQL WorkBench also(with SSH). And I'm using MySql.Data.MySqlClient in my code. Anyone has any idea about this? Is this because my instance does not allow remote access or a problem with my connection string?
Thank you in advance,
Wayne.
Edit: The problem is solved by adding l_DBConn.Open() and add my IP to the server using RANT ALL PRIVILEGES ON . TO root#'hostname' IDENTIFIED BY 'root-password'. Thanks!
Try calling l_DBConn.open() before executing the reader. The error implies that the connection is not open since you haven't explicitely opened it
Some ideas:
Make sure you can connect to your database locally first.
Is Sql Server setup to allow mixed authentication
Have you open port 3306 in the EC2 firewall and the windows firewall if you're running it.
I have been trying and searched thousands of times in other sites but never found a sample or simple code to use.
I've created an application C# which uses an ODBC connection, i have also MS SQL installed and configured to enable remoting database information sharing. I want to make my database available to everyone uses this application i have made by using a connection.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace CaseMatter
{
public partial class mainLog : Form
{
string userID, userName, userAddress, userFName, userLastName, userCity, userPassword, userTele;
public mainLog()
{
InitializeComponent();
this.Size = new Size(442, 162);
}
private void Submitbtn_Click(object sender, EventArgs e)
{
string ConnectionString =
"Data Source=xx.xx.xx.xx,1433;Initial Catalog=master;Integrated Security=SSPI;";
var conn = new SqlConnection(ConnectionString);
conn.Open();
var strSQLCommand = "SELECT Name FROM example WHERE id='1'";
var command = new SqlCommand(strSQLCommand, conn);
var reader = command.ExecuteReader();
while (reader.Read())
{
passwordBox.Text = reader.GetString(0);
}
reader.Close();
conn.Close();
}
}
}
I have just edited the code and tried it, i have added a try catch to handle sql exceptions but it still freezes when i click on submit button.
Hopefully someone figures this out.
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: TCP Provider, error: 0 - No connection could be made because the target machine actively refused it.)"
I suspect there are one of four things going on here.
The SQL Server is misconfigured and isn't responding to tcp connections. The easiest way to trouble shoot this is to use SQL Management Studio to connect. If you can't connect this way then you'll have to look at the sql server service setup. Also, make sure that the SQL Browser service is running on the server.
The SQL Server is configured to use a different TCP port than the one you are trying. Remove the ",1433" from your connection string and try again. SQL Browser should respond back with the actual port that sql server is listening on.
The SQL Server has a firewall in place that is blocking remote connections. Temporarily turn off the firewall and see if you can connect. If you can, then configure the firewall correctly and turn it back on.
Your local box has some type of firewall that is blocking outgoing connections on that port. Try turning yours off to see if this helps. If so, configure it correctly and turn it back on.
If this is a brand new sql server that no one has remotely connected to then it's very likely to be entirely within the configuration of SQL server or the Windows configuration.
In your example you are not passing the USER ID and Password. Is that just because you didnt want to include your credentials? I assume not as you properly XXX out your IP address. I would start by supplying credentials of a SQL server account that can access your database.
I noticed that you're using SqlConnection which is not an ODBC connection object. With SqlConnection you're actually connecting via ADO.NET. The connection string syntax for SqlConnection (both ODBC and ADO.NET) can be found on the following site:
http://connectionstrings.com
By the way, if you're connecting to SQL 2005, the connection string would look like this:
using System.Data.SqlClient;
// trusted connection (SQL configured to use your windows ID)
string ConnectionString =
"Data Source=xxxxxxx;Initial Catalog=xxxxxx;Integrated Security=SSPI;"
// sql authentication (SQL manages its own users/pwds)
//string ConnectionString =
//"Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
var conn = new SqlConnection(ConnectionString);
conn.Open();
var strSQLCommand = "SELECT Name from [example_autoincrement] where id='1'";
var command = new SqlCommand(strSQLCommand, conn);
var reader = command.ExecuteReader();
while (reader.Read())
{
passwordBox.Text = reader.GetString(0);
}
reader.Close();
conn.Close();
Ok, this is a strange one to check but just make sure the time and date is accurate on client and server. SSPI requires a maximum gap of 5 minutes between each