gpt4 book ai didi

mysql - 如何使我的数据库连接正常工作?

转载 作者:行者123 更新时间:2023-11-29 11:46:10 24 4
gpt4 key购买 nike

我在 AZURE 帐户中设置了 MySQL 数据库,但无法以任何方式连接它。我尝试过:

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
SqlConnection sqlconnection = new SqlConnection("Database=Banco1; Data Source=br-cdbr-azure-south-a.cloudapp.net;User Id=USERID Password=PASSWORDid");
Console.WriteLine("1");
sqlconnection.Open();
Console.WriteLine("2");
SqlCommand cmd = new SqlCommand("CREATE TABLE Customer(First_Name char(50), Last_Name char(50), Address char(50), City char(50), Country char(25), Birth_Date datetime); ", sqlconnection);
Console.WriteLine("3");
cmd.ExecuteNonQuery();
Console.WriteLine("4");
Console.ReadLine();
}
}
}

我尝试通过命令进行打印,但程序仅打印“1”,这意味着我无法打开连接...我该如何解决这个问题?

输出错误:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Error in the network or specifics to the instance when stablishing conexion to the SQL Server. The server was not found or was not avaible. Check if the instance name is correct and if the SQL Server is configured to allow remote conexions. (provider: Named Pipes Provider, error: 40 - Could not stablish connection to SQL Server)

最佳答案

SqlConnection用于连接SQL数据库,对于MySQL数据库,需要使用MySqlConnection。请先安装MySql Connect/NET然后添加引用enter image description here

现在您可以使用以下代码成功连接到您的Mysql数据库:

MySqlConnection con = new MySqlConnection("your_mysql-connect_string");

关于mysql - 如何使我的数据库连接正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34909271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com