SqlException Error Message - c#

I am developing an application that runs on windows Ce 6.0, the application should connect to a database that is located on my PC., but every time I try to open the connection i get A SQLException error message. this is how I am opening the connection
..........
SqlConnection EdiSqlConnection;
String ConnectionString;
ConnectionString = "Server='serverName';Database=DatabaseName;Trusted_Connection=true;";
..........
EdiSqlConnection = new SqlConnection(ConnectionString);
try {
EdiSqlConnection.Open();
Output.Text = "Connected"; //this is a message to see that we are connected
}
catch (Exception ex) {
Output.Text = "Message: " + ex.Message;
return;
}
Why am I getting the error message? and how do i resolve it?
Thank you.

There isn't enough information here, so you'll need to provide more.
What type of exception is being thrown? Are you Sure its a SqlException? You are catching all exceptions.
What is the value of ex.Message in the catch block?
Which database are you connecting to? SQL Server (version?) or SQL Compact?
Assuming your application and the database are running on separate machines, you may have problems with trusted connection. Your WinCE device is unlikely to be on a domain or have a way to authenticate a user, so authentication will probably fail. Try using SSPI authentication instead. See "Trusted Connection from a CE device" on this page. Note that you don't have to give a domain user ID, just a user ID and password that is valid on the database server machine and which has access rights for the database.

remove the quotes from 'serverName'

Related

Xamarin Can't Connect to SQL Database

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

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.

Unable to connect to remote mySQL database

I've been trying the following code to attempt to connect to a remote database I'm hosting on digital ocean.
MySqlConnection myConnection = new MySqlConnection("Server = 104.236.197.146; Port = 3306; Database = BattleRoyale; Uid = root; Pwd = password");
try
{
myConnection.Open();
}
catch (Exception e)
{
MessageBox.Show( e.ToString(), "Database connection error" , MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Whenever I attemt to run this code I get the following error:
MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySql hosts.
I believe the problem is with the hosted database. I've tried the following things:
GRANT ALL PRIVILEGES ON . TO root#"%" IDENTIFIED BY 'rootPass';
I've been trying to check to see if TCP/IP is enabled, but I cannot find it via phpMyAdmin nor going through putty or anything.
As you can tell, I don't know much about Linux machines and I'm just trying to connect to this database to work on a school project. Who knows what I've done trying to fix this issue. Any help would be greatly appreciated.
Possible reasons include:
MySQL is configured to listen on "localhost" or 127.0.0.1 only - this does not allow remote connections at all, regardless of the firewall configuration
Firewall may be configured to not let through communication on port 3306
MySQL may not be running on the standard port 3306 but some other port
Most probably it's 1. or 2. Especially 1. is something you don't think of at first.
Please note that many hosters don't allow access to the databases from outside their own network. That's why often PhpMySQL works but remote connections don't.

Stored Procedure in sql server 2005

I use the following function for insert the value in database through the stored procedure.but i have the exception in sqlConnection.Open();How to solve this?
try
{
string dbConnectionString = "something";
SqlConnection sqlConnection = new SqlConnection(dbConnectionString);
SqlCommand command = new SqlCommand("sp_Test", sqlConnection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("#id ", SqlDbType.Int).Value = TextBox1.Text;
command.Parameters.AddWithValue("#FullName", SqlDbType.VarChar).Value = TextBox2.Text;
sqlConnection.Open();
command.ExecuteNonQuery();
sqlConnection.Close();
}
catch (SqlException ex)
{
Console.WriteLine("SQL Error" + ex.Message.ToString());
}
Well, the error you are getting doesn't really have anything to do with stored procedures, but I can answer the question of "how to solve this?" in the context of the exception you are getting.
First, I hope you are not using "something" as the actual connection string to your sql server. You should be using a real connection string. Here are some various examples you can use to connect to SQLServer - http://www.connectionstrings.com/sql-server-2005
Using the first example from the above url
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
You'll want to change "myServerAddress" to the ip address or host name of the server. This could be "localhost" or it could be another computer you have on the network. Change "myDataBase" to the name of the database you created, and make sure the name matches exactly as the database might be configured with case sensitivity. And finally change "myUsername" and "myPassword" to the user and password of the account you use to access the server under Sql Server Management Studio.
Now, assuming you have actually done the above, here are some steps you can take to debug connection issues.
Ping the computer - from and command prompt type "ping servername" where servername is the name or ip address of the server that runs SQL Server. If you don't get a response from the server then you need to debug your local network.
If the server is running locally try using "." for the "myServerAddress" value. If it's sqlserver express edition, use the express format - ".\SQLExpress" or "myServerAddress\SQLExpress"
If SQL Server is running on another computer try each of the three links under the section "More Information" from this kb article - http://support.microsoft.com/kb/914277 That will ensure remote connections are supported.
But the real issue is that you cannot connect to the database. I recommend searching for solutions to that problem first. Or perhaps supply more information about your setup here on SO in a different question.
Hope that helps,
-Marcus

What is the difference between Java and C# when it comes to connect to Sql Server Database?

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

Categories