如何使用C#以dd-MM-yyyy格式插入日期 [英] How to insert date in dd-MM-yyyy format using C#

查看:131
本文介绍了如何使用C#以dd-MM-yyyy格式插入日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在c#中以dd-MM-yyyy格式插入日期。查询插入是



 SqlCommand cmd_cust =  new  SqlCommand( @ 插入到custdetail值(' + txtInvoiceNo.Text +   ',' + txtCustomerName.Text +  ',' + txt_contact.Text +  ',' + txtAddress.Text +  ',' + txt_total_amt.Text +  ',' + dt_date.Value.ToString(  dd-MM-yyyy)+  '),con_create); 
con_create.Open();
cmd_cust.ExecuteNonQuery();
con_create.Close();







我创建了一个列名为date的表数据类型日期。插入记录后,日期列字段中的值为yyyy-dd-MM格式。我想用dd-MM-yyyy格式。

解决方案

不,它不是。

DATETIME和DATE字段离子SQL做没有格式比C#中的DateTime值更多 - 它们存储为自过去任意点以来的毫秒数。格式化仅在将日期转换为字符串以显示给用户时应用 - 因此请检查读取这些值的代码并显示它们:SSMS通常会显示带有标准格式的记录(ISO 8601或yyyy-MM- dd)除非特别告知不要。



但请不要做那样的事情!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。

这也意味着您不会将DateTime值转换为字符串,只是将其转换回SQL端的DateTime以及可能涉及的所有可能错误。只需通过参数将 dt_date.Value 直接传递给SQL作为DataTime值。


正如您所说日期中的值列字段是yyyy-dd-MM格式但你的代码说dt_date.Value.ToString(dd-MM-yyyy)所以值应该是dd-MM-yyyy。所以你的日期列字段不正确我认为。



你可以用103格式转换日期

101是美国格式mm / dd / yyyy

103英国格式是dd / mm / yyyy



convert(varchar(10),fieldname,103)

I'm trying to insert date in dd-MM-yyyy format in c#. Query for inserting is

SqlCommand cmd_cust = new SqlCommand(@"insert into custdetail values ('" + txtInvoiceNo.Text + "','" + txtCustomerName.Text + "','" + txt_contact.Text + "', '" + txtAddress.Text + "', '" + txt_total_amt.Text + "', '" + dt_date.Value.ToString("dd-MM-yyyy") + "')", con_create);
            con_create.Open();
            cmd_cust.ExecuteNonQuery();
            con_create.Close();




I have created table with column name date has datatype date. After inserting record the value in date column field is in yyyy-dd-MM format. I want this in dd-MM-yyyy format.

解决方案

No, it isn't.
DATETIME and DATE fields ion SQL do not have a "format" any more than they do for DateTime values in C# - they are stored as a number of milliseconds since an arbitrary point in the past. Formatting is only ever applied when the date is converted to a string to display to the user - so check the code that read these values out and displays them: SSMS will normally display records with it's standard formatting (ISO 8601, or yyyy-MM-dd) unless it is specifically told not to.

But please, don't do things like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
This also means that you don't convert a DateTime value to a string, just to convert it back to a DateTime at the SQL end with all the possible errors that can involve. Just pass the dt_date.Value directly to SQL as a DataTime value via a parameter.


As you are saying "the value in date column field is in yyyy-dd-MM format" but your code says "dt_date.Value.ToString("dd-MM-yyyy")" So the value should be dd-MM-yyyy. so your date column fieldis not correct i think.

you can use 103 format to convert date
101 is US format that is mm/dd/yyyy
103 is UK format that is dd/mm/yyyy

convert(varchar(10),fieldname,103)


这篇关于如何使用C#以dd-MM-yyyy格式插入日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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