从C#日期转换到MySql格式 [英] Date conversion from C# to MySql Format

查看:333
本文介绍了从C#日期转换到MySql格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何到C#转换日期时间到MySql DATETIME格式。我正在从文本框的值类似2011年7月27日该格式。但我想以这种格式转换2011-7-27。所以我在这里stuking。请帮帮我。我的目标是过滤两个日期和演出的纪录在asp.net ListView控件。
这里是我的代码:

 的DateTime DT1 = Convert.ToDateTime(txtToDate.Text); 
日期时间DT2 = Convert.ToDateTime(txtFromDate.Text);
lvAlert.DataSource = facade.GetAlertsByDate(DT1,DT2);
lvAlert.DataBind();


解决方案

我没有使用MySQL的使用.NET,但甲骨文与.NET类似日期的转换问题。只有这样,才能保持snae这个一直使用日期值参数,无论是输入作为welll为WHERE子句比较。与一个MySQL日期参数类型创建的参数,只是给它一个.NET datetime值,应该工作而无需你做转换。



编辑补充示例代码



此代码示例显示了使用的参数为DateTime值,而不是编码转换为文本值,并直接在嵌入的文本值的基本技术SQL命令文本。

 公开数据表GetAlertsByDate(DateTime的开始,日期时间结束)
{
的SqlConnection康恩=新的SqlConnection(的connectionString);
的SqlCommand CMD =新的SqlCommand(
SELECT * FROM WHERE警报之间EVENTTIME和@启动@end,康恩);
DataTable的表=新的DataTable();

{
的SqlParameter参数;
参数=新的SqlParameter(@启动,SqlDbType.DateTime);
param.Value =启动;
cmd.Parameters.Add(参数);
参数=新的SqlParameter(@结束,SqlDbType.DateTime);
param.Value =结束;
cmd.Parameters.Add(参数);
SqlDataAdapter的大=新SqlDataAdapter的(CMD);
da.Fill(表);
}
终于
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
返回表;
}

这是SQL Server代码,但该技术应该是为大多数数据库相同。对于Oracle,例如,唯一的变化是使用Oracle数据访问对象,并使用:在参数名称来代替@的。 。MySQL的技术也应该非常相似



有关许多数据库,快捷方式可以存在用于创建参数,如:



  cmd.Parameters.AddWithValue(@启动,启动); 



当你知道值不为空这工作,和正确的参数类型可以从派生C#类型的价值。 AddWithValue是特定于SQL Server; 添加作品也不过是SQL Server已经过时了。



希望这有助于。


How to convert C# datetime to MySql Datetime format. I am getting value from text box like 7/27/2011 this format. But i want to convert in this format 2011-7-27. So here i am stuking. Please help me. My objective is to filter the record between two dates and show in a listview control in asp.net. Here is my code:

DateTime dt1 = Convert.ToDateTime(txtToDate.Text);
  DateTime dt2 = Convert.ToDateTime(txtFromDate.Text);
            lvAlert.DataSource = facade.GetAlertsByDate(dt1, dt2);
            lvAlert.DataBind();

解决方案

I haven't used MySQL with .NET, but Oracle has similar date conversion issues with .NET. The only way to stay snae with this has been to use parameters for date values, both for input as welll as for WHERE clause comparisons. A parameter created with a MySQL date parameter type, and just giving it a .NET datetime value, should work without needing you to do conversions.

EDITED TO ADD SAMPLE CODE

This code sample shows the basic technique of using parameters for DateTime values, instead of coding conversions to text values and embedding those text values directly in the SQL command text.

public DataTable GetAlertsByDate(DateTime start, DateTime end)
{
    SqlConnection conn = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand(
        "SELECT * FROM Alerts WHERE EventTime BETWEEN @start AND @end", conn);
    DataTable table = new DataTable();
    try
    {
        SqlParameter param;
        param = new SqlParameter("@start", SqlDbType.DateTime);
        param.Value = start;
        cmd.Parameters.Add(param);
        param = new SqlParameter("@end", SqlDbType.DateTime);
        param.Value = end;
        cmd.Parameters.Add(param);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(table);
    }
    finally
    {
        cmd.Dispose();
        conn.Close();
        conn.Dispose();
    }
    return table;
}

This is SQL Server code, but the technique should be the same for most databases. For Oracle, for example, the only changes would be to use Oracle data access objects, and use ":" in place of "@" in parameter names. The technique for MySQL should also be very similar.

For many databases, shortcuts may exist for creating parameters, such as:

cmd.Parameters.AddWithValue("@start", start);

This works when you know the value is not null, and the correct parameter type can be derived from the C# type of the value. "AddWithValue" is specific to SQL Server; "Add" works also but is obsolete in SQL Server.

Hope this helps.

这篇关于从C#日期转换到MySql格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆