c#应用程序中的Set语言
我对连接在MySQL数据库上的c#应用程序中的set语言有问题 正在使用的服务器是英语版的Windows server 2003 我需要用德语设置查询的输出 我在MySQL数据库中尝试了查询序列,结果是正确的c#应用程序中的Set语言,c#,mysql,C#,Mysql,我对连接在MySQL数据库上的c#应用程序中的set语言有问题 正在使用的服务器是英语版的Windows server 2003 我需要用德语设置查询的输出 我在MySQL数据库中尝试了查询序列,结果是正确的 mysql> SET lc_time_names = 'de_DE'; SELECT CONCAT( MONTHNAME( STR_TO_DATE(Eng_Month, '%Y-%m') ), ' ',
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();
}
}
protectedoverride void InitializeCulture()
{
Page.Culture=“de de”;
Page.UICulture=“de de”;
}
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
初始化文化();
每月语言();
GridViewBind();
响应。写入(Page.Culture+“
”;
回答。写下(“您当前的区域性:+System.Globalization.CultureInfo.CurrentCulture.DisplayName+”
);
}
}
受保护的无效月语言()
{
使用(ODBCCN)连接=
新OdbcConnection(ConfigurationManager.ConnectionString[“cn”].ConnectionString))
{
sql=“SET lc_time_names='de_de';”;
使用(odbc)命令=
新的OdbcCommand(sql,cn))
{
尝试
{
command.Connection.Open();
command.ExecuteNonQuery();
}
捕获(例外情况除外)
{
抛出新的ApplicationException(“操作失败!”,ex);
}
最后
{
command.Connection.Close();
}
}
}
}
公共数据表GridViewBind()
{
sql=“…”;
尝试
{
dadapter=新的OdbcDataAdapter(sql,cn);
dset=新数据集();
dset.Clear();
数据填充(数据集);
数据表dt=数据集表[0];
GridView1.DataSource=dt;
GridView1.DataBind();
返回dt;
}
捕获(例外情况除外)
{
掷骰子;
}
最后
{
dadapter.Dispose();
dadapter=null;
cn.Close();
}
}
您的MonthLanguage创建一个连接,设置语言,然后关闭连接,从而失去设置语言的效果。填充数据集时,它使用具有默认语言的新连接。您可以尝试以下几种方法:
在GridViewBind中,将SQL设置为:
sql = "SET lc_time_names = 'de_DE'; Select .....";
(这将在SQL Server中工作;我不知道MySQL。)
或者,只需从SQL返回base DateTime列,并在C代码中使用正确的语言区域设置即可将其格式化。只需在与查询相同的连接中执行
SET lc\u time\u 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();
}
}
}
使用RazorView/c#在MySQL中解决 见示例:
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");