I'm having a real hard time making a connection to a SQL Anywhere 12 database using .NET connection strings.
I'm using the following querystring:
Data Source=xxx.xxx.xxx.xxx,yyyy; Initial Catalog={MyDatabaseName}; User ID={MyUsername}; Password={MyPassword}
(where xxx.xxx.xxx.xxx = the IP address of the remote server and yyyy is the required port)
I'm trying to run this with the following code to test my connection:
private const string connectionString = "{MyQueryString}";
private const string testQuery = "{ATestSelectQuery}";
using (SqlConnection conn = new SqlConnection(connectionString))
{
ExecuteNonQuery(conn, testQuery);
}
static void ExecuteNonQuery(SqlConnection conn, string query)
{
try
{
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR: {0}", ex.Message);
}
}
However, when I run this I'm receiving the following error:
ERROR: A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)
Your code looks like you are trying to connect to Microsoft SQL server not SQLAnywhere 12 server. SqlConnection ,SqlCommand, etc are for Microsoft SQL server only. The .NET client objects for SQLAnywhere 12 are SAConnection,SACommand, etc.
You need to make sure you have the proper SQLAnywhere .NET client files installed. If you do not or are not sure then download the SQL Anywhere Developer Edition. It is free. Client versions 12-17 will connect to your version 12 server. Be aware that the connection string for SQLAnywhere is very different than the connection string to a MS SQL Server.