2014-01-11 58 views
1

我非常需要帮助,这:数据类型不匹配

说明:实现在一个按钮(到达,离开)的形式的功能实现的情况下,与只有一个按钮和自动。决定是否(根据数据库中的记录)”

我的目标 到达或离开 - 1.st点击按钮组到达时间,按钮组第2点击出发时间,它它保存到数据库表 - 平日... 我得到的错误 - 到达时间被记录下来,但是当我第二次单击该按钮时,出现“标准表达式中的数据类型不匹配”错误。

这是我的代码,

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(); 
    } 
} 

谢谢!

+0

欢迎来到StackOverflow!如果你告诉我们什么是错的,它应该做什么,你会得到更好的答案。尽量减少你的例子,只需要最少量的代码来重现问题。这将帮助我们帮助你。 –

回答

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]的条件

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.UtcNowDateTime.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的答案应该让你过去你的直接问题,但我会认真考虑我在这里建议的更改。