2013-03-01 98 views
2

我有以下连接字符串:MySQL连接字符串不工作

Data Source=localhost;UserID=root;Password=;Database=eyebus;

当我尝试连接到数据库,我得到一个错误说,也许是服务器未找到或无法访问。这个字符串有什么问题?

我也尝试了其他字符串/单词,我在互联网上找到,但没有一个真正起作用。

此外,问题不在于服务器,因为它可以被其他应用程序访问。

class SQLManager 
    { 

     private static string conString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ToString(); 


     public List<PesquisaOcorrenciasExistentesModel> getOcorrencias() 
     { 
      List<PesquisaOcorrenciasExistentesModel> ocorrencias = new List<PesquisaOcorrenciasExistentesModel>(); 

      using (SqlConnection con = new SqlConnection(conString)) 
      { 
       try 
       { 
        con.Open(); 
       } 
       catch (Exception e) 
       { 
        Debug.WriteLine(e.Message); 
       } 
       using (SqlCommand com = new SqlCommand("SELECT * FROM ocorrencias", con)) 
       { 
        SqlDataReader reader = com.ExecuteReader(); 
        while (reader.Read()) 
        { 
         PesquisaOcorrenciasExistentesModel o = new PesquisaOcorrenciasExistentesModel(); 
         o.IdOcorrencia = reader.GetInt16(0); 
         o.IdOcorrenciaTipo = reader.GetInt16(1); 
         o.DataInicio = reader.GetDateTime(2); 
         o.DataFim = reader.GetDateTime(3); 
         o.Operador = reader.GetString(4); 
         o.Motorista = reader.GetString(5); 
         o.Cobrador = reader.GetString(6); 
         o.Terceiros = reader.GetString(7); 
         o.Descricao = reader.GetString(8); 
         o.EmailsEnviados = reader.GetString(9); 
         o.TipoOcorrenciaOutro = reader.GetString(10); 
         ocorrencias.Add(o); 
        } 
       } 
      } 

      return ocorrencias; 
     } 
    } 
} 
+0

您使用的数据库连接提供程序是什么? – 2013-03-01 19:15:10

+0

看看这篇文章:http://stackoverflow.com/questions/10505952/mysql-connection-string-is-not-working-in-c-sharp – 2013-03-01 19:15:37

+0

MySql.Data.MySqlClient。我已经看过那篇文章,但提出的解决方案并没有解决我的问题。 – 2013-03-01 19:16:38

回答

2

尝试使用Server,而不是数据源,UID而不是用户名。

或检查出这篇文章,以了解更多信息:How to form a correct MySQL connection string?

另外,不要你需要使用:

ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString 

而且,SqlConnectionSqlDataReader,并且SqlCommand用于项目建立在微软SQL。我相信你应该使用MySqlConnectionMySqlDataReaderMySqlCommand

看一看:http://www.codeproject.com/Articles/43438/Connect-C-to-MySQL

+0

已经选中此贴。另外,建议的字符串替换不起作用。 – 2013-03-01 19:22:20

+0

MySQL是否在默认端口(3306)上运行? – 2013-03-01 19:23:32

+0

另外,你可以发布你的'MySqlConnection'代码吗? – 2013-03-01 19:24:05