datetime甲酸酯问题 [英] datetime formate problem

查看:72
本文介绍了datetime甲酸酯问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SqlCommand cmd = new SqlCommand("insert into StudentFees (StudentName,TotalFees,FeesToPay,RemainingFees,NextPaymentDate,NextPaymentAmt)values(''" + ddlStudentName.SelectedValue + "''," + Convert.ToInt64(txt_TotalFees.Text) + "," + Convert.ToInt64(txt_FeesToPay.Text) + "," + Convert.ToInt64(txt_RemainingFees.Text) + ",''" + (Convert.ToDateTime(txt_NextPaymentDate.Text)).ToString("MM/dd/yyyy") + "''," + Convert.ToInt64(txt_NextPaymentAmt.Text) + ")", cnn);



i有转换txt_NextPaymentDate到datetime.but当我在我的系统中更改日期设置时,它会给我一个错误:字符串未被识别为有效的DateTime。

我必须为任何系统中的全球日期接受做什么。

i已经尝试了

.tostring(dd / MM / yyyy),如我的代码所示,



但它不起作用。


i have convert txt_NextPaymentDate to datetime.but when i change date setting in my system,it will give me error like:String was not recognized as a valid DateTime.
what i have to do for globle date acceptance in any system.
i have try
.tostring("dd/MM/yyyy") as shown in my code,

but it is not working.

推荐答案

你可以使用全球化

You can use globalization
using System.Globalization;

CultureInfo ci=new CultureInfo("gu-IN");

SqlCommand cmd = new SqlCommand("INSERT INTO StudentFees (StudentName,TotalFees,FeesToPay,RemainingFees,NextPaymentDate,NextPaymentAmt) VALUES(@StudentName, @TotalFees, @FeesToPay, @RemainingFees, @NextPaymentDate, @NextPaymentAmt)", cnn);
cmd.Parameters.AddWithValue("@StudentName", ddlStudentName.SelectedValue);
cmd.Parameters.AddWithValue("@TotalFees", Convert.ToInt64(txt_TotalFees.Text));
cmd.Parameters.AddWithValue("@FeesToPay", Convert.ToInt64(txt_FeesToPay.Text));
cmd.Parameters.AddWithValue("@RemainingFees", Convert.ToInt64(txt_RemainingFees.Text));
cmd.Parameters.AddWithValue("@NextPaymentDate", DateTime.ParseExact(txt_NextPaymentDate.Text,ci));
cmd.Parameters.AddWithValue("@NextPaymentAmt", Convert.ToInt64(txt_NextPaymentAmt.Text));







谢谢




Thanks


使用 DateTime.ParseExact 方法

http: //msdn.microsoft.com/en-us/library/w2sa9yss.aspx [ ^ ]


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



使用参数化查询的可能性也可以解决您的问题:

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.

The chances are that using parametrised queries will also solve your problem:
SqlCommand cmd = new SqlCommand("INSERT INTO StudentFees (StudentName,TotalFees,FeesToPay,RemainingFees,NextPaymentDate,NextPaymentAmt) VALUES(@SN, @TF, @FP, @RF, @PD, @PA)", cnn);
cmd.Parameters.AddWithValue("@SN", ddlStudentName.SelectedValue);
cmd.Parameters.AddWithValue("@TF", Convert.ToInt64(txt_TotalFees.Text));
cmd.Parameters.AddWithValue("@FP", Convert.ToInt64(txt_FeesToPay.Text));
cmd.Parameters.AddWithValue("@RF", Convert.ToInt64(txt_RemainingFees.Text));
cmd.Parameters.AddWithValue("@PD", Convert.ToDateTime(txt_NextPaymentDate.Text));
cmd.Parameters.AddWithValue("@PA", Convert.ToInt64(txt_NextPaymentAmt.Text));

但是,使用DateTimePicker而不是TextBox来防止用户输入错误是值得的。

However, it would be worth using a DateTimePicker instead of a TextBox to prevent user entry errors.


这篇关于datetime甲酸酯问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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