datetime.now函数的datetime格式显示系统datettime格式的不同日期时间格式

时间:2013-04-17 04:36:45

标签: c# asp.net

我正在使用位于ASP.NET & C# windows server操作系统的windows server 2008 R2应用程序,我使用的服务器是VM。我将我的Windows日期时间格式更改为"en-US",我的SQL服务器日期时间格式,区域设置日期时间格式显示为"MM/dd/yyyy",但使用c#开发的.NET应用程序显示日期时间格式为DateTime.Now在调试模式下为"dd/MM/yyyy"因此我无法将数据保存到SQL数据库,它抛出异常"invalid datetime format"。如果有人可以帮我理解如何在我的应用程序中更改DatetTime.Now的日期时间格式,我将非常感激。

以下是代码段 - 呼叫部分 -

saveCustomerInfo.saveInfo(txtCustomerId.Text, userNmae, objProperties.ProposalNumber.ToString(), "", "", Total_Amount, SERVICE_TAX, STAMP_DUTY, AgentID, DateTime.Now, null, null, "", "", "", product_code, MODE)

调用函数签名是 -

public void saveInfo(string CustomerID, string UserName, string ProposalNo, string PaymentID, string PolicyNumber, int amount, double serviceTax, double stampduty, string Agent_ID, DateTime? ProposalDate, DateTime? PaymentDate, DateTime? PolicyDate, string ClaimNo, string ClaimAmount, string ClaimStatus, int ProductCode, string mode)

sql Code部分是:

public void saveInfo(string CustomerID, string UserName, string ProposalNo, string PaymentID, string PolicyNumber, int amount, double serviceTax, double stampduty, string Agent_ID, DateTime? ProposalDate, DateTime? PaymentDate, DateTime? PolicyDate, string ClaimNo, string ClaimAmount, string ClaimStatus, int ProductCode, string mode)
    {
        string connection = SetConnectionString(SPContext.Current.Site);
        SqlConnection con = new SqlConnection(connection);
        SqlCommand com = new SqlCommand("Select count(ProposalNo) from [aspnetdb].[dbo].[user_info_log] where ProposalNo = '" + ProposalNo + "'", con);
        con.Open();
        Int32 count = (Int32)com.ExecuteScalar();

        if (count == 0)
        {
            //string strFormattedProposalDate = ProposalDate.ToString().Split('/')[1] + "/" + ProposalDate.ToString().Split('/')[0] + "/" + ProposalDate.ToString().Split('/')[2];

            com.Dispose();
            com = new SqlCommand("insert into user_info_log(CustomerID,UserName,ProposalNo,PaymentID,PolicyNo,TotalAmount,ServiceTax,Stampduty,Agent_ID,ProposalDate,PaymentDate,PolicyDate,ClaimNumber,ClaimAmount,ClaimStatus,ProductCode,Mode) VALUES('" + CustomerID + "','" + UserName + "','" + ProposalNo + "','" + PaymentID + "','" + PolicyNumber + "','" + amount + "','" + serviceTax + "','" + stampduty + "','" + Agent_ID + "','" + ProposalDate + "','" + System.DBNull.Value + "','" + System.DBNull.Value + "','" + ClaimNo + "','" + ClaimAmount + "','" + ClaimStatus + "','" + ProductCode + "','" + mode + "')", con);
            com.ExecuteNonQuery();
        }
        con.Close();
        com.Dispose();
    }

错误代码是: 将varchar数据类型转换为日期时间数据类型会导致超出范围的值。 声明已经终止。

2 个答案:

答案 0 :(得分:1)

您需要更改应用程序中使用的文化。将其添加到web.config的system.web部分:<globalization culture="en-US" /> 此外,如果您不想更改所有应用程序的默认文化,您可以将en-US文化传递给DateTime.ToString方法:var str = DateTime.Now.ToString(CultureInfo.GetCultureInfo("en-US"))

除此之外,将DateTime值作为字符串发送到数据库是绝对错误的方法。考虑在sql命令中使用参数:http://csharp-station.com/Tutorial/AdoDotNet/Lesson06 这样就不会将DateTime转换为字符串

答案 1 :(得分:0)

然后尝试

this.TextBox3.Text = DateTime.Now.ToString("MM.dd.yyyy");

或者这个......

string format = "MM/dd/yyyy hh:mm:ss.fff";
DateTime d = DateTime.ParseExact("05/15/2012 10:09:28.650",
                                format,
                                System.Globalization.CultureInfo.InvariantCulture)