如何访问网站(PHP)中使用的mysql数据库到C#窗口应用程序 [英] How is it possible to access mysql database being used in website (PHP) to C# window application

查看:65
本文介绍了如何访问网站(PHP)中使用的mysql数据库到C#窗口应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有c#的窗口应用程序和一个带有PHP的网站。



我想在两个应用程序中使用相同的数据库。



网站正常运行(显然是托管Linux服务器)。



我无法在C#窗口应用程序中使用相同的数据库。



当我尝试通过C#连接时,我收到错误...









用户'用户'@'localhost'拒绝访问(使用密码:是)



我尝试过:



MySqlConnection con = new MySqlConnection(Server = localhost ; Database = database_name; User = user;密码=密码);







//相同的配置在网站上运行。

I have created a window application with c# and a website with PHP.

I want to use the same database in both applications.

Website is properly running (Hosted with Linux Server obviously).

I am unable to use the same database in C# window application.

When I try to connection through C#, I get the error...




Access denied for user 'user'@'localhost' (using password: YES)

What I have tried:

MySqlConnection con = new MySqlConnection("Server=localhost; Database=database_name; User=user; Password=password");



//The same configuration is running on website.

推荐答案

尝试使用IP地址而不是localhost并提及端口号。



Try using IP address instead of localhost and mention Port number.

MySqlConnection con = new MySqlConnection(
"Server=127.0.0.1;Port=3306;Database=database_name;Uid=user;Pwd=password;");


名称localhost表示这台电脑。



您的PHP代码在Linux服务器上运行。 MySQL数据库在同一台Linux服务器上运行,因此在PHP连接字符串中使用localhost就可以了。



你的C#应用​​程序(可能)没有运行那个Linux服务器。它最有可能在Windows计算机上运行。如果您要求该代码连接到与C#应用程序相同的计算机上的MySQL数据库,它将无法工作,或者它将不会连接到同一个数据库实例。



您需要更新C#连接字符串,以指定托管数据库的Linux服务器的正确计算机名称或IP地址。您可能还需要在Linux机器上配置MySQL实例以允许来自其他计算机的连接。
The name "localhost" means "this computer".

Your PHP code is running on a Linux server. The MySQL database is running on the same Linux server, so using "localhost" in your PHP connection string is fine.

Your C# application is (presumably) not running on that Linux server. It's most likely running on a Windows computer. If you ask that code to connect to the MySQL database on the same computer as the C# application, it either won't work, or it won't be connected to the same database instance.

You need to update your C# connection string to specify the correct computer name or IP address of the Linux server which is hosting the database. You might also need to configure the MySQL instance on the Linux box to allow connections from other computers.


这篇关于如何访问网站(PHP)中使用的mysql数据库到C#窗口应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆