我该如何存储呢。 [英] How do I stored this.

查看:70
本文介绍了我该如何存储呢。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// for string
private string name = "";

//for date 
private date = DateTime.MinValue;





我不确定,如何声明这一点。对于我的插入,插入当前日期,无论我选择任何日期。我不知道为什么,请帮忙。谢谢。



I am not sure, how to declare this. For my insert, is inserted the current date regardless of I chosing any date. I am not sure why , please help. Thanks.

//insert code
public int CarInsert()
        {

            string queryStr = "INSERT INTO Car(CarName,DateFrom, DateTo,TimeFrom, TimeTo) Values(@CarName,@dateFrom,@dateTo,@TimeFrom,@TimeTo)";

            SqlConnection conn = new SqlConnection(_connStr);
            SqlCommand cmd = new SqlCommand(queryStr, conn);
            cmd.Parameters.AddWithValue("@CarName", _carName);
            cmd.Parameters.AddWithValue("@dateFrom", Convert.ToDateTime(_dateFrom));
            cmd.Parameters.AddWithValue("@dateTo", Convert.ToDateTime(_dateTo));
            cmd.Parameters.AddWithValue("@TimeFrom", _timeFrom);
            cmd.Parameters.AddWithValue("@TimeTo", _timeTo);
           

            conn.Open();
            int nofRow = 0;
            nofRow = cmd.ExecuteNonQuery();

            conn.Close();

            return nofRow;
        }

推荐答案

如果从查看该代码开始,它不会引用 date name 你声明的变量 - 所以你放入它们的任何值都会被忽略。



但是......如果你看一下 Convert.ToDateTime [ ^ ]它在 备注 部分中说:



此方法尝试完全解析值并避免抛出FormatException。它完成了当前日期丢失的月,日和年信息。



所以,我要做的第一件事是检查 _dateFrom _dateTo 字符串中是否有任何数据 - 如果他们没有,然后你生成的日期并保存到你的DB将永远是今天......
If you start by looking at that code, it doesn't reference the date or name variables you declare at all - so whatever values you put in them will be ignored anyway.

But...if you look at the definition of Convert.ToDateTime[^] it does say in the Remarks section that:

"This method tries to parse value completely and avoid throwing a FormatException. It completes missing month, day, and year information with the current date."

So, the first think I would do is check if the _dateFrom and _dateTo strings had any data in them - if they don't, then the dates you generate and save into your DB will always be today...


// for string



if(carName.Length == 0)

returnMessage + = Car Name不能为空!




//如何检查dateTime是否为空
//for string

if (carName.Length == 0)
returnMessage += Car Name cannot be blank!


//how you check if is empty for dateTime


您可以参考以下代码解决问题



DateTime odt = DateTime.MinValue;

Console.WriteLine(odt);



问候,

Praveen Nelge
You can refer the below code for you problem

DateTime odt = DateTime.MinValue;
Console.WriteLine(odt);

Regards,
Praveen Nelge


这篇关于我该如何存储呢。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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