简体   繁体   English

从C#控制台应用程序查询网站数据库

[英]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/ 我正在尝试使用C#编写一个简单的应用程序,该应用程序将查询位于https://newswire.theunderminejournal.com/上可供公众使用的数据库。

They give an example located at https://newswire.theunderminejournal.com/sample2.php 他们给出了一个位于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". 据我所知,我要连接的服务器是“ newswire.theunderminejournal.com”,数据库是“书报摊”。

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)" “未建立与SQL Server的连接时发生了与网络相关或特定于实例的错误。找不到服务器或无法访问该服务器。请验证实例名称正确并且SQL Server配置为允许远程连接。(提供者:命名管道提供程序,错误:40-无法打开与SQL Server的连接)

I've pinged the server, getting an IP address at 45.79.164.122 and no issues at all pinging it. 我对服务器执行了ping操作,获得了IP地址为45.79.164.122,并且对ping命令没有任何问题。

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! 我是C#的新手,所以如果有另一种/更好的方法可以使用,我将不胜感激!

Thanks. 谢谢。

You cannot create a SqlConnection to a mySQL database, which is why the error message refers to "… establishing a connection to SQL Server ." 您无法创建与mySQL数据库的SqlConnection ,这就是为什么错误消息涉及“ ...建立与SQL Server的连接”的原因。 .

You need to use a MySqlConnection instead, which you get when you install Connector/Net - which you can download from Oracle here 您需要改用MySqlConnection ,它是在安装Connector / Net时获得的-您可以在此处从Oracle下载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM