2013-03-11 141 views
1

我需要你的帮助。C#连接到mysql服务器

我这是说的错误消息:

"network-related or instance-specific error occurred while 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)".

我试图连接使用的IP其它本地计算机本地主机。 我的代码:

public DbStudent() 
    { 
     UGIcon = new SqlConnection(); 
     UGIcon.ConnectionString = "server= IPAddress;user="id";password="pass";database=MyDB"; 
    } 
    public SqlConnection Open() 
    { 
     UGIcon.Open(); 
     return UGIcon; 
    } 

    public void Close() 
    { 
     UGIcon.Close(); 
    } 

请问有人能帮我吗?

+4

你的标题具有的MySql而你的错误是关于SQL Server的,你想用'SqlConnection'连接到MySQL数据库吗? – Habib 2013-03-11 08:00:25

+0

是的,有可能吗?或者你可以给我另一种方式来连接mysql? – Wiranata 2013-03-11 08:02:31

+0

我不明白你的代码是如何编译的:你的连接字符串包含未转义引号...... – Kek 2013-03-11 08:03:08

回答

1

SqlConnection用于访问SQL Server,另一个数据库使用OleDbConnection。 因此,如果您想连接到MySQL,请改为使用OleDbConnection

public DbStudent() 
    { 
     UGIcon = new OleDbConnection(); 
     UGIcon.ConnectionString = "Server=IPAddress;Database=MyDB;Uid=id;Pwd=pass;"; 
    } 

你可以看到here为MySQL连接字符串

2

你需要从MySql

有下载安装,你会得到一个司机为MySQL数据库