Connect other server database - c#

I have a console app and want to connect other windows server where I have already created user, database table. In conf.xml I have
<configuration>
<crypto location="172.16.10.34" database="Crypto_Pan" username="PinbySms" password="ASDasd123"/>
</configuration>
then my code is :
Dictionary<string, string> d = conf.getCrypto();
SqlConnectionStringBuilder bu = new SqlConnectionStringBuilder();
bu.DataSource = d["location"];
bu.NetworkLibrary = "DBMSSOCN";
bu.InitialCatalog = d["database"];
bu.IntegratedSecurity = false;
bu.UserID = d["username"];
bu.Password = d["password"];
SqlConnection thisConnection = null;
try
{
thisConnection = new SqlConnection(bu.ConnectionString);
thisConnection.Open();
Console.WriteLine("success connected");
}
catch (Exception e)
{
Console.WriteLine("exeption: " + e.ToString());
thisConnection.Close();
}
I have an error when my app try to connect this database:
[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.
Does anybody have any suggestions?

there was not default port in sql configuration PROTOCOL MSQSQLSERVER TCP/IP , i changed it to 1433 and now it's ok.

Related

C#, SQL Server. Provider: TCP Provider, error: 0 - No such host is known

My connection string looks like this:
Data Source=sql88;Initial Catalog=Osm;User ID=foouser;Password=foopsw
The code that creates connection string looks like this:
var con = new SqlConnectionStringBuilder();
if (string.IsNullOrEmpty(model.Username) && string.IsNullOrEmpty(model.Password))
con.IntegratedSecurity = true;
else
{
con.UserID = model.Username;
con.Password = model.Password;
}
con.DataSource = model.Host ?? string.Empty;
con.InitialCatalog = model.Database ?? string.Empty;
return con;
My application creates database structure at SQL Server, then inserting data. So sometimes the applcation works approximately 14 hours. The code that executes SQL at SQL Server looks like this:
using (var cmd = con.CreateCommand())
{
cmd.CommandTimeout = int.MaxValue;
cmd.CommandText = sql;
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
var test = ex.Message;
throw;
}
}
Sometimes application works well, but sometimes application throws an 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 such host is known.) (Microsoft
SQL Server, Error: 11001)
I've read this page, however, in my view this is not my case because sometimes my application works well.

SQL exception was not handled by user code

My code:
if (Page.IsValid)
{
DataSet.UsersDataTable oUserDataTable =
new DataSet.UsersDataTable();
DataSetTableAdapters.UsersTableAdapter oUserTableAdapter =
new DataSetTableAdapters.UsersTableAdapter();
oUserTableAdapter.FillUserByUserName(oUserDataTable, txtUserName.Text);
if (oUserDataTable.Count!=1)
{
string strErrorMessage =
"UserName Or Password Is Not Correct ! Please Try Again . . . ";
DisplayErrorMessage(strErrorMessage);
return;
}
DataSet.UsersRow oUserRow = oUserDataTable[0];
if (string.Compare(oUserRow.Password.Trim(),txtPassword.Text.Trim(),false)!=0)
{
string strErrorMessage =
"UserName Or Password Is Not Correct ! Please Try Again . . . ";
DisplayErrorMessage(strErrorMessage);
return;
}
if (oUserRow.IsUserActive==false)
{
string strInformationMessage =
string.Format("Dear {0} You Should Not Login At This Time , Please Contact Support",txtUserName.Text);
DisplayInformationMessage(strInformationMessage);
return;
}
I get an error:
Additional information: 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)
first of all check if db connection can open, if yes, then is good to go, otherwise throw exception and check if the connection string is correct. Following code can help you for DB connection checking
try
{
using (SqlConnection conn = new SqlConnection("connection"))
{
conn.Open();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Invalid Connection String");
}
add this code before this line DataSet.UsersDataTable oUserDataTable =
new DataSet.UsersDataTable();

Can't Connect C# with SQL Server

I am trying to write a program which interacts with my SQL Server database. I am programming in C# on Parallels on my Mac and SQL Server is running via Docker.
But I just can't connect. I am just getting the same error every time I try.
I have already tried to allow the remote access on SQL Server with:
EXEC sp_configure 'remote access', 1 ;
GO
RECONFIGURE ;
GO
but this does not solve my problem.
Here is my C# code:
main Class
Database database;
public Form1()
{
InitializeComponent();
database = new Database("localhost\\MSSQLSERVER", "user1", "topsecret", "master"); // \
}
private void connect_button_Click(object sender, EventArgs e)
{
database.Connect();
}
Database class:
class Database
{
SqlConnectionStringBuilder builder;
SqlConnection connection;
public Database(string source, string userid, string password, string initialcatalog){
this.builder = new SqlConnectionStringBuilder();
this.builder.DataSource = source;
this.builder.UserID = userid;
this.builder.Password = password;
this.builder.InitialCatalog = initialcatalog;
}
public void Connect()
{
try
{
// Connect to SQL
Console.WriteLine("Connecting to SQL Server ... ");
this.connection = new SqlConnection(this.builder.ConnectionString);
connection.Open();
Console.WriteLine("Connected");
}
catch(SqlException sqle)
{
Console.WriteLine(sqle.Message);
}
}
}
And I am always getting this error:
Network-related or instance-specific error when connecting to SQL Server. The server was not found or can not be accessed. Verify that the instance name is correct and that SQL Server allows remote connections. (provider: SQL Network Interfaces, error: 25 - connection string invalid)
The MSSQLSERVER is the instance name of the unnamed, default instance on your machine - you must not use this in the connection string.
Instead, try this line of code:
database = new Database("localhost", "user1", "topsecret", "master");
Just specify no explicit instance name at all - just localhost (or (local), or .) for the current machine - that's all you need!
It was Problem with Parallels, because Paralles can not access to localhost. So i had to define the IP from Parallels in Visual Studio like this:
database = new Database("10.211.55.2", "user1", "topsecret", "Time");

How to make a connection with MySql database from my CPanel with Visual Studio 2017

I am trying to make a connection to MySQL database from my cpanel with Visual studio, but I keep getting an 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)
I have installed both MySql for Visual studio and Connector/net. I have tried with both Server Explorer and through the code using the connection string. I have added my ip to the access host list on Remote MySql in CPanel. But nothing worked.
namespace Program
{
public partial class WebForm1 : System.Web.UI.Page
{
string connectionString = #"SERVER=mydomain.net;DATABASE=mydatabasename;UID=myuser;PASSWORD=mypass";
protected void Page_Load(object sender, EventArgs e)
{
FillDropDown(DropDownList1);
}
public void FillDropDown(DropDownList dropDown)
{
try
{
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
SqlCommand sqlCmd = new SqlCommand("Select * from MyTable", sqlCon);
sqlCon.Open();
dropDown.DataSource = sqlCmd.ExecuteReader();
dropDown.DataBind();
dropDown.DataTextField = "Name";
dropDown.DataValueField = "Id";
}
}
catch(Exception ex)
{
lblError.Text = ex.Message;
}
}
}
}
The SqlConnection-object may only be used for connections to MS-SqlServers. You have to use MySqlConnection to connect to MySql-server. This also applies to the SqlCommand.
Furthermore the used connection-string is not valid for MySql ('Password' should be 'Pwd') Compare your string to https://www.connectionstrings.com/mysql/

Database Connectivity with oracle database using c#

While trying to connect the oracle database I am getting the following 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)"}
Problem may be silly one but this is my first time with database so need help:
My code is:
static void Main(string[] args)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = "Data Source=ORCL_BOA; database=BOANEWDOC;User Id=BOANEWDOC;Password=BOANEWDOC;Trusted_Connection=true";
conn.Open();
//code
}
you may need to reference Oracle.ManagedDataAccess.dll on ODTwithODAC121012.zip you can download from oracle site.
do not use System.Data.OracleClient as it is obsolete.
var connection = new OracleConnection(YourConnectionString);
try
{
connection.Open();
//AMK: Do some stuff with the db
}
catch (Exception exception)
{
//AMK: do some other stuff in case of error
}
finally
{
if(connection !=null && connection.State==ConnectionState.Open)
connection.Close();
}

Categories