2016-07-25 11 views
0

は実際には、販売数が細かい更新している私は、ユーザーの販売数を挿入された1つのアプリケーションを作成イムしかし、私は、そのここでイム詳細 入れて全体のコードアクセスデータベースに現在の日付を挿入するにはどうすればよいですか?

private void button1_Click_1(object sender, EventArgs e) 
     { 

      this.txtinput.MaxLength = 4; 
      cmd = new OleDbCommand("update Login set Sales_count= IIF(IsNull(Sales_count), 0, Sales_count) + 1 where [Unique_No][email protected]_No and [To_Date]='#"+DateTime.Now.ToString("dd/MM/yyyy")+"#'", con); 
      cmd.Parameters.AddWithValue("@Unique_No", txtinput.Text); 
       con.Open(); 
       int n = cmd.ExecuteNonQuery(); 

        if (n == 0) 
        { 
         MessageBox.Show("Invalid Unique No. pls try again later");// **Debugger come to this line if i insert [To_Date]='#"+DateTime.Now.ToString("dd/MM/yyyy")+"#'** // if i remove above line in code then its updating fine 

        } 
        else 
        { 
         this.DialogResult = DialogResult.OK; 
        } 



       con.Close(); 
      } 

     } 

を更新していない参照貼付アクセスのために現在の日付を置くことによって、クエリで編集するときテーブルの詳細 enter image description here enter image description here

+0

SQL-Serverの<>のMS Access ...おそらく、あなたはあなたの日付フォーマットのための二重引用符をエスケープする必要があります。例えば'ToString(\" dd/MM/yyyy \ ")...'? – BJones

+0

あなたが投稿したレコード( 'User_ID022')はTo_Dateカラムに値を持っていません。したがって、あなたが投稿したコードは更新されません。そのレコードを更新したい場合は、あなたのコード内で 'と[To_Date] = '#" + DateTime.Now.ToString( "dd/MM/yyyy")+ "#" 'を削除する必要があります。あるいは、更新する前に今日の日付をTo_Date列に入力する必要があります。 – BJones

+0

@bjones:でも、sales_countを同時に更新しようとすると、現在の日付もデータベースに挿入されます。 – Atul

答えて

1

Access SQLは、dbエンジンが現在の日付を決定するために使用できるDate()関数をサポートしています。したがって、現在の日付を取得してSQLステートメントのテキストに連結するコードは必要ありません(eek!)。また、SQLパラメータとして日付を入力する必要もありません(これは厄介ではありません)。単にdbエンジンにDate()を決定させるだけです。

cmd = new OleDbCommand("update Login set Sales_count= IIF(IsNull(Sales_count), 0, Sales_count) + 1 where [Unique_No][email protected]_No and [To_Date]=Date()", con); 

この提案は、ご質問のSQL文に基づいています。しかし、コメントでは、いくつかのレコードの今日の日付にTo_Dateの値を変更するように思えます。私はどのレコードを変更すべきか明確ではないので、WHERE句に必要なものは何も分かりません。しかし、今日の日付にTo_Date値を更新するために、アクセスSQLは次のように起動する必要があります...

UPDATE Login SET To_Date = Date() WHERE ... 
+1

こんにちは、私はクエリで変更 更新ログインセットSales_count = IIF(IsNull(Sales_count)、0、Sales_count)+ 1、[To_Date] = Date()[Unique_No] = @ Unique_Noとそのうまくいっている:) – Atul

0

これを試してみてください。

private void button1_Click_1(object sender, EventArgs e) 
    { 
     DateTime toDay = DateTime.Now; 

     this.txtinput.MaxLength = 4; 
     cmd = new OleDbCommand("update Login set Sales_count= IIF(IsNull(Sales_count), 0, Sales_count) + 1 where [Unique_No][email protected]_No and [To_Date]= @to_day", con); 
     cmd.Parameters.AddWithValue("@Unique_No", txtinput.Text); 
     cmd.Parameters.AddWithValue("@date_now", toDay.ToString("yyyyMMdd")); 
      con.Open(); 
      int n = cmd.ExecuteNonQuery(); 

       if (n == 0) 
       { 
        MessageBox.Show("Invalid Unique No. pls try again later");// **Debugger come to this line if i insert [To_Date]='#"+DateTime.Now.ToString("dd/MM/yyyy")+"#'** // if i remove above line in code then its updating fine 

       } 
       else 
       { 
        this.DialogResult = DialogResult.OK; 
       } 



      con.Close(); 
     } 

    } 
+0

まだデバッガが来ています MessageBox.Show( "Unique No. pls invalid pls again later")行 – Atul

+0

@Atulはあなたですアプリケーションで入力した情報を含むレコードがテーブルにあることを確認してください。 'SELECT * FROMログインWHERE [Unique_No] = @ Unique_Noと[To_Date] = @ to_day'をそれぞれの値を入れて試しましたか? – BJones

+0

@bjones:参考用に編集済みのアクセスデータベースの詳細 – Atul

0

のBa提供されたコメントにsed、あなたはSETの日付が必要です。あなたの元のクエリでは、あなたはWHERE句でそれを使用している:あなたはまた、私は更新されている間違った構文を持っていた

private void button1_Click_1(object sender, EventArgs e) 
{ 
    DateTime toDay = DateTime.Now; 

    this.txtinput.MaxLength = 4; 
    cmd = new OleDbCommand("update Login set Sales_count= IIF(IsNull(Sales_count), 0, Sales_count) + 1, [To_Date]= @to_day where [Unique_No][email protected]_No", con); 
    cmd.Parameters.AddWithValue("@Unique_No", txtinput.Text); 
    cmd.Parameters.AddWithValue("@to_Day", toDay.ToString("yyyyMMdd")); 
     con.Open(); 
     int n = cmd.ExecuteNonQuery(); 

      if (n == 0) 
      { 
       MessageBox.Show("Invalid Unique No. pls try again later");// **Debugger come to this line if i insert [To_Date]='#"+DateTime.Now.ToString("dd/MM/yyyy")+"#'** // if i remove above line in code then its updating fine 

      } 
      else 
      { 
       this.DialogResult = DialogResult.OK; 
      } 



     con.Close(); 
    } 

} 

。上記のクエリが機能するかどうかを教えてください。

+0

まだデバッガが下の行に移動しました if(n == 0) { MessageBox.Show( "無効な固有番号plsは後で再試行します)"; } – Atul

関連する問題