条件表达式中的数据类型不匹配

时间:2014-01-11 23:02:22

标签: c# winforms oledb

我非常需要帮助:

说明:以一个按钮(到达,离开)的形式实现功能。只用一个按钮实现一个案例并自动判断是否到达(根据数据库中的记录)“

我的目标 - 1.点击按钮设置到达时间,第二次点击按钮设置出发时间,并将其保存到数据库表 - 工作日... 我得错误 - 记录到达时间,但是当我第二次点击按钮时,我收到“条件表达式中数据类型不匹配”的错误。

这是我的代码,

int counter = 0;
List<DateTime> dateList = new List<DateTime>();
public void button1_Click(object sender, EventArgs e)
{
    counter++;            
    DateTime arrivalTime = DateTime.Now;
    dateList.Add(arrivalTime);
    if (counter == 1)
    {
        string write = "Insert into Weekdays (Arrival) values('" + dateList[0].ToString() + "');";
        OleDbCommand read = new OleDbCommand(write, sc);
        OleDbDataReader reading;
        try
        {
            sc.Open();
            reading = read.ExecuteReader();
            MessageBox.Show("Arrival time saved!.");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        sc.Close();
    }
    if(counter == 2)
    {
        string update = "UPDATE Weekdays SET Departure = '" + DateTime.Now + "' WHERE Arrival ='" +"';";
        OleDbCommand read1 = new OleDbCommand(update, sc);
        OleDbDataReader reading1;
        try
        {
            sc.Open();
            reading1 = read1.ExecuteReader();
            MessageBox.Show("Departure time saved!.");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        sc.Close();
    }
}

谢谢!

2 个答案:

答案 0 :(得分:2)

ichramm的answer很可能会解决您的问题,但您在此处遇到一些可能不会导致错误消息的其他问题,但这是不好的做法。

首先,参数化您的查询,这样您就不会受到SQL injection漏洞的影响(我很高兴地承认,在您的具体示例中,这似乎不是真正的风险,但总是以正确的方式做事情更好):

string update = "UPDATE Weekdays SET Departure = @DepartureTime WHERE Arrival = @ArrivalDate;";
OleDbCommand read1 = new OleDbCommand(update, sc);
read1.Parameters.AddWithValue("@DepartureTime", DateTime.Now);
read1.Parameters.AddWithValue("@ArrivalDate", dateList[0]);
...

根据您的详细信息,您可能不需要担心时区,但我仍然建议您使用DateTime.UtcNow而不是DateTime.Now,因为同样,最佳做法是仅存储日期/以UTC为单位的时间值,因此您无需担心计算存储值的时区。

我假设sc是示例代码中的OleDbConnection对象,所以如果不是这样,你可以在这里忽略我对它的引用。 OleDbCommandOleDbConnection实施IDisposable界面。这意味着如果你在using块中实例化它们,这些类被设置为自行清理,所以我建议你重新编写代码:

try
{
    using (var sc = new OleDbConnection("[YOUR_CONNECTION_STRING]"))
    {
        using (var read = new OleDbCommand())
        {
            read.Connection = sc;
            if (counter == 1)
            {
                //set up insert command
                //parameterize it
            }
            if (counter == 2)
            {
                //set up update command
                //see my suggestion above on how this should be parameterized
            }

            sc.Open();

            //your command doesn't return any results, so why use read.ExecuteReader()?
            //read.ExecuteNonQuery() will work fine for your purposes and doesn't instantiate
            //another object
            var rowsAffected = read.ExecuteNonQuery();
        }
        //at this point, regardless of whether you encounter an error, your command object is cleaned up
    }
    //now your connection is automatically closed/disposed of properly, again regardless of whether
    //you encounter an error
}
catch(Exception ex)
{
    MessageBox.Show(ex.Message);
}

注意我已将read.ExecuteReader()更改为read.ExecuteNonQuery():您不需要使用ExecuteReader(),因为您的命令没有返回结果集,因此这样您就可以避免实例化OleDbDataReader

同样,ichramm的答案应该让你超越你的直接问题,但我会认真考虑我在这里建议的变化。

答案 1 :(得分:1)

string update = "UPDATE Weekdays SET Departure = '" + DateTime.Now + "' WHERE Arrival ='" +"';";

不应该是

string update = "UPDATE Weekdays SET Departure = '" + DateTime.Now + "' WHERE Arrival ='" + dateList[0] "';";

提示:我在条件

中添加了dateList[0]