I have a C# Xamarin Android application which I need to connect to a SQL Server instance accessible over the network.
try
{
using (SqlConnection cn = new SqlConnection(ConnectionString))
{
cn.Open();
IsConnected = true;
}
}
catch (Exception ex)
{
Toast.MakeText(Application.Context, "Error Occuerred: " + ex.Message, ToastLength.Long).Show();
}
The Connection String is as follows: Data Source=IPAddress;Initial Catalog=DatabaseName;Persist Security Info=True;User ID=SomeUser;Password=SomePassword
I have included the INTERNET and WIFI_State Permissions in the manifest. (To check if wifi is currently turned on and connected to some kind on network)
I am getting an error: Server Does Not Exist or Connection is refused.
I can connect my Honeywell Scanning device to the database with the same connection string.
Please NOTE I don not want a web service to handle the SQL connection in order for me to select and update tables.
Wrote an ASP. NET web service which is very efficient and quick
Related
i have SQL server express 2012 installed on windows 10 64 bit, the connection from same computer worked fine, problem when i connect from another computer on same network,
i get 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 connection
-provider TCP provider error 0 the wait operation timeout
i try the following things but not fix the problem:
Enable and automaticly start SQL Browser
Enbale TCP/IP and set port 1433 for all IP types
Start Named inslance SQLExpress2012 autmaticaly as Build-in : Network service
connect without user and password with IntegratedSecurity=true
The database is using mixed-mode authentication
add an exception in the firewall for port 1433
enable named pipe
allow remote connection
this is my code:
public static bool TestSQLServerConnection(
string ComputerNameOrIPAddress,
string SQLServerInstanceName,
string PortNumber)
{
try
{
SqlConnectionStringBuilder SQLServerConnectionString = new SqlConnectionStringBuilder();
SQLServerConnectionString.DataSource = ComputerNameOrIPAddress+",+"+PortNumber+"\\"+SQLServerInstanceName;
SQLServerConnectionString.InitialCatalog = DatabseName;
SQLServerConnectionString.TrustServerCertificate = true;
SQLServerConnectionString.IntegratedSecurity = true;
//SQLServerConnectionString.UserID = "...";
//SQLServerConnectionString.NetworkLibrary = "DBMSSOCN";
//SQLServerConnectionString.Password = "...";
SqlConnection con = new SqlConnection(SQLServerConnectionString.ConnectionString);
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText= "select 1";
con.Open();
cmd.ExecuteScalar();
con.Close();
return true;
}
catch (Exception ex)
{
throw ex;
}
}
when i ping the ip address or computer name of computer of SQL Server it connected and return response, means the computer is connected to network.
i search for while without any solution, please help me.Thanks
I add
SQL Browser Service UDP Port 1434 on firewall and worked fine.
Use SQL Server Management Studio to connect remotely to an SQL Server Express instance hosted on an Azure Virtual Machine
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.
My MySQL connect code below is 'Catching' this error: "Unable to connect to any of the specified MySQL hosts."
My DB is on my linux website and I have remote access enabled using % so anyone can access the DB.
My code is .NET C# and I have a winform.
string server = "http://www.mywebite.net";
string database = "mywebsite_app";
string uid = "admin";
string password = "Password";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
MySqlConnection dbConn;
try
{
dbConn = new MySqlConnection(connectionString);
dbConn.Open();
if (dbConn.State.ToString() != "Open")
{
this.Text = "could not open database connection";
}
else
{
this.Text = "database connection opened";
}
}
catch (Exception ex) // catch on general exceptions, not specific
{
this.Text = ex.Message;
}
Thanks for any help...
If you mysql database is on remote hosting, then you usually out of luck try to connect to that database remotely.
The hosting provider usually blocked remote mysql access.
Try using ssh-tunneling.
download putty
http://www.chiark.greenend.org.uk/~sgtatham/putty/
open the command line, and execute putty from the installation directory
putty username#remote_mysqlhost -L 3306:localhost:3306
or you could use the gui, fill the address with your mysql remote, but dont open connection yet.
go to ssh tunnel, add new forwarded port 3306, and the destination as localhost:3306.
as of why the destination is localhost, it is because the destination is what the ssh session see. because you already connected to your remote server in your ssh session, thus, the mysql host is local.
this could also be a set up for dmz.
you can create a dmz gateway using putty to tunnel trough any computer within the dmz network. but the computer behind the gateway, wont publicly expose to the internet.
dont forget to klik the add button to make sure the configuration is added to tunnel list.
open the connection and use your password (ssh user and password, usually the same as webhosting control panel user and password
Now, instead connecting to your remote mysql, you can connect to mysql trough localhost. it would be tunneled by putty to your remote mysql.
try to connect using mysql administration tool first. to test wheter or not the connection is success.
the way ssh tunnel work is almost the same as vpn, from user view.
but without the hassle of setting up vpn server on remote host.
I need to connect my C# application developed in my Standalone PC with my Hosted Linuux MySQL Server. How can i do it.. Is there any server configuration setup or any kind of Remote Connection Permission Setting have to be done? Please help with this..
Make sure that the server where MySQL is at can accept connections.
Read this to read how to configure your c# application to connect to MySQL.
The connection string code should like this
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
You need to allow the remote connection by this way
Mysql has very good documentation how to connect mysql server (local and remote).
It is here http://dev.mysql.com/doc/index-connectors.html
And your case may be this http://dev.mysql.com/doc/connector-c/en/index.html
Its Pretty Simple to Implement
1) Download My Sql Connector From https://dev.mysql.com/downloads/connector/net/6.9.html
2) In your C# Project add refrence of Mysql.Data.Dll
3) use this Connection String
string connectionString= "SERVER=000.000.000.000;DATABASE=testdb;UID=test;PASSWORD=test123;"
4) use same like this sample code
MySqlConnection conn = new MySqlConnection(connectionString);
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();
// Perform database operations
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
conn.Close();
Console.WriteLine("Done.");
or Read this tutorials
https://dev.mysql.com/doc/connector-net/en/connector-net-tutorials-connection.html
I am writing an application requires to connect to sql server. I prefer to write the App in Java. but When I try to connect to the server I got the connection refused error. I am using JTDS JDBC driver. I think it is due to the port 1433 or 1434 are not open. The server is in my work place and I can not change the ports. But funny enough, I use c# writing the same thing, it connects successfully. is it because SqlConnection class in C# library works better with ms sql server? or am I doing something wrong here? FYI, the server we are using in work is MS SERVER 2003.
Sorry I did not provide any code earlier. One tricky part is that the server we have is called "SERVER" on the local network.
C#:
SqlConnection objConnection = new SqlConnection("Data Source= SERVER\\SQLEXPRESS;Initial Catalog=SSS;Persist Security Info=True;User ID=user;Password=pass");
SqlCommand objcommand = new SqlCommand();
string strSQL;
objcommand.Connection = objConnection;
strSQL = "select * from company where companyid = #companyID ";
try
{
objConnection.Open();
SqlDataReader Query = objcommand.ExecuteReader();
while (Query.Read())
{
MessageBox.Show(Convert.ToString(Query["clientRef"]));
}
objConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error Retreiving info: " + ex.ToString(), "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
objConnection.Close();
}
As I am not really familiar with C#. I have got the code above from one of the colleges. and it returns the info correctly.
Java:
try{
Connection connection;
Class.forName("net.sourceforge.jtds.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:jtds:sqlserver://network.local/SERVER\\SQLEXPRESS:1433/SSS","user","pass");
System.out.println("Connection succeed!");
}
catch (Exception e) {
e.printStackTrace();
}
The java code above got Network error IOException: Connection refused error.
FYI: I can ping server.network.local successfully. But when I telnet server.network.local 1433/1434, I got telnet: Unable to connect to remote host: Connection refused.
Check how your .NET app is connecting. It may be as simple as it using named pipes, which JTDS supports as well.
It might be that the defaults are different, are you setting the port when you initialise your driver
jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]
Something like this jdbc:jtds:sqlserver://nameofyourdatabaseserver.or.ipaddress:port/yourdatabasename
Try using the full dns name for your server or the IP address
At the following link you will find a useful tutorial which describes in detail how to connect to MS SQLServer database, both from Java and C#. It also describes how to query the database, pass and retrieve data and much more. Hope you find it useful: http://www.shahriarnk.com/Shahriar-N-K-Research-Embedding-SQL-in-C-Sharp-Java.html