gpt4 book ai didi

c# - 在 C# 应用程序中设置语言

转载 作者:行者123 更新时间:2023-11-29 00:09:39 25 4
gpt4 key购买 nike

我在连接到 MySQL 数据库的 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();
}
}

最佳答案

只需在与查询相同的连接中执行 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();
}
}
}

关于c# - 在 C# 应用程序中设置语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25781925/

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