Zulukas Zulukas - 2 months ago 16
C# Question

Querying a website database from a C# console application

I'm trying to write a simple app in C# that will query a database available to the public located at https://newswire.theunderminejournal.com/

They give an example located at https://newswire.theunderminejournal.com/sample2.php

As far as I know, the server I'm trying to connect to is "newswire.theunderminejournal.com" and the database is "newsstand".

This gives me a string of:

string connection_str ="Server=newsstand.theunderminejournal.com;Database=newsstand";


My code looks like this:

xData = "Unable to connect to database.";
string server = "'newswire.theunderminejournal.com'";
//string server = "45.75.164.122";
string database = "'newsstand'";
string connection_str = string.Format("Server={0};Database={1};", server, database);
Console.WriteLine(connection_str);

SqlConnection connection = new SqlConnection(connection_str);

try
{
connection.Open();
xData = "Connection established!";
}
catch(Exception e)
{
xData = e.Message;
}

connection.Close();
connection.Dispose();


This results only in an error message:


"A network-related or instance-specific error occured while not
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've pinged the server, getting an IP address at 45.79.164.122 and no issues at all pinging it.

Is there something obvious I'm missing here? I'm brand new to C# so if there is another/better way of doing this, I'd appreciate it!

Thanks.

Answer

You cannot create a SqlConnection to a mySQL database, which is why the error message refers to "… establishing a connection to SQL Server.".

You need to use a MySqlConnection instead, which you get when you install Connector/Net - which you can download from Oracle here