Reputation: 1407
I have problem with set language in c# application connected on a MySQL db.
The server in use is a Windows Server 2003 in english language.
I need set the output of query in german language.
I tried the queries sequence in MySQL db and the output is correct.
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
If try the same solution in c# application the output is only english.
This is beginning to make me believe my structure as a whole is not correct.
What am I missing ?
I would greatly appreciate any help you can give me in working this problem.
My code below:
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();
}
}
Upvotes: 1
Views: 1221
Reputation: 1
Solved in MySQL, using RazorView / c#.
See example:
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");
Upvotes: 0
Reputation: 7838
Just execute SET lc_time_names
in the same connection as your query. You can make MonthLanguage
accept a parameter for the connection and just call it on the connection you're using for your query.
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();
}
}
}
Upvotes: 1
Reputation: 7344
Your MonthLanguage creates a connection, sets the language, and then closes the connection which looses the effect of setting the language. When you fill your dataset, it uses a new connection which has the default language. You can try a couple of things:
In GridViewBind set your SQL as:
sql = "SET lc_time_names = 'de_DE'; Select .....";
(This would work in SQL Server; I don't know about MySQL.)
Alternatively, just return the base DateTime column from the SQL and use the fact that you're in the correct language locale in the C# code to format it as you want.
Upvotes: 0