2013-03-01 96 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