2014-09-11 38 views
0

我在MySQL數據庫連接的c#應用程序中設置語言有問題。在c#應用程序中設置語言

正在使用的服務器是英文版的Windows Server 2003。

我需要設置德語的查詢輸出。

我試過MySQL數據庫中的查詢序列,輸出是正確的。

mysql> SET lc_time_names = 'de_DE'; 

SELECT 
    CONCAT(
     MONTHNAME(
      STR_TO_DATE(Eng_Month, '%Y-%m') 
     ), 
     ' ', 
     YEAR (
      STR_TO_DATE(Eng_Month, '%Y') 
     ) 
    ) AS DE_Date 
FROM 
    tbl_month; 
Query OK, 0 rows affected 

+-----------+ 
| DE_Date | 
+-----------+ 
| Juni 2014 | 
| Juli 2014 | 
+-----------+ 
2 rows in set 

如果在c#應用程序中嘗試相同的解決方案,輸出只是英文。

這開始讓我相信我的整個結構是不正確的。

我缺少什麼?

我將不勝感激任何幫助,您可以給我在工作的這個問題。

我下面的代碼:

protected override void InitializeCulture() 
    { 
     Page.Culture = "de-DE"; 
     Page.UICulture = "de-DE"; 
    } 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      InitializeCulture(); 
      MonthLanguage(); 
      GridViewBind(); 
      Response.Write(Page.Culture + "<br />"); 
      Response.Write("Your current culture: " + System.Globalization.CultureInfo.CurrentCulture.DisplayName + "<br />"); 
     } 
    } 

    protected void MonthLanguage() 
    { 
     using (OdbcConnection cn = 
      new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString)) 
     { 
      sql = " SET lc_time_names = 'de_DE'; "; 

      using (OdbcCommand command = 
       new OdbcCommand(sql, cn)) 
      { 
       try 
       { 
        command.Connection.Open(); 
        command.ExecuteNonQuery(); 
       } 
       catch (Exception ex) 
       { 
        throw new ApplicationException("operation failed!", ex); 
       } 
       finally 
       { 
        command.Connection.Close(); 
       } 
      } 
     } 
    } 


    public DataTable GridViewBind() 
    { 
     sql = " ... "; 

     try 
     { 
      dadapter = new OdbcDataAdapter(sql, cn); 
      dset = new DataSet(); 
      dset.Clear(); 
      dadapter.Fill(dset); 
      DataTable dt = dset.Tables[0]; 
      GridView1.DataSource = dt; 
      GridView1.DataBind(); 

      return dt; 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
     finally 
     { 
      dadapter.Dispose(); 
      dadapter = null; 
      cn.Close(); 
     } 
    } 

回答

1

只需在與您的查詢相同的連接中執行SET lc_time_names。您可以使MonthLanguage接受連接的參數,只需在您用於查詢的連接上調用它即可。

protected void MonthLanguage(OdbcConnection conn) 
{ 
    var sql = " SET lc_time_names = 'de_DE'; "; 

     using (OdbcCommand command = 
      new OdbcCommand(sql, conn)) 
     { 
      try 
      { 
       command.Connection.Open(); 
       command.ExecuteNonQuery(); 
      } 
      catch (Exception ex) 
      { 
       throw new ApplicationException("operation failed!", ex); 
      } 
     } 
} 

public DataTable GridViewBind() 
{ 
    sql = " ... "; 
    using(var cn = new OdbcConnection(
      ConfigurationManager.ConnectionStrings["cn"].ConnectionString)) 
    { 
     try 
     { 
      MonthLanguage(cn); // This sets the language for this connection 

      dadapter = new OdbcDataAdapter(sql, cn); 
      dset = new DataSet(); 
      dset.Clear(); 
      dadapter.Fill(dset); 
      DataTable dt = dset.Tables[0]; 
      GridView1.DataSource = dt; 
      GridView1.DataBind(); 

      return dt; 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
     finally 
     { 
      dadapter.Dispose(); 
      dadapter = null; 
      cn.Close(); 
     } 
    } 
} 
0

你MonthLanguage創建一個連接,設置語言,然後關閉它失去設置語言的有效的連接。填充數據集時,它將使用具有默認語言的新連接。您可以嘗試幾件事情:

在GridViewBind設置你的SQL爲:(這會工作在SQL Server中,我不知道關於MySQL)

sql = "SET lc_time_names = 'de_DE'; Select ....."; 

或者,只是返回SQL中的基本DateTime列,並使用C#代碼中正確語言區域設置的事實來按需要對其進行格式化。

+0

謝謝你,在MySQL不行你的建議 – 2014-09-11 08:13:26

0

在MySQL中使用RazorView/c#解決。

見例如:

var tmp_data = db.Query("SET lc_time_names = 'it_IT'; SELECT MONTHNAME(yourdate) AS MONTH FROM orders GROUP BY MONTH(yourdate), YEAR(yourdate) ORDER BY yourdate DESC"); 
相關問題