2017-06-14 70 views
0

有一個酒店的程序。這裏是「到達」賓客代碼:無法獲得算法如何更新數據庫中的選定行ms-access

try 
       { 

        string dtparrival = this.dateTimePicker4.Text.ToString(); 
        string statusarrival = "Arrival"; 

        string strSql = "SELECT * FROM guestreg WHERE g_status = '" + statusarrival + "' AND g_ad = '" + dtparrival + "'"; 


        OleDbCommand cmd = new OleDbCommand(strSql, connection); 
        connection.Open(); 
        cmd.CommandType = CommandType.Text; 
        OleDbDataAdapter da = new OleDbDataAdapter(cmd); 
        DataTable scores = new DataTable(); 
        da.Fill(scores); 
        arrivaldgv.DataSource = scores; 
        /**************************************************************************/ 
        this.arrivaldgv.Columns[0].Visible = false; 
        this.arrivaldgv.Columns["g_totalrate"].Visible = false; 
        this.arrivaldgv.Columns["u_added"].Visible = false; 
        this.arrivaldgv.Columns["u_timeadded"].Visible = false; 
        /**************************************************************************/ 
        this.arrivaldgv.Columns["g_name"].HeaderText = "Name"; //1 
        this.arrivaldgv.Columns["g_surname"].HeaderText = "Surname"; //2 
        this.arrivaldgv.Columns["g_company"].HeaderText = "Company"; //3 
        this.arrivaldgv.Columns["g_ad"].HeaderText = "Arrival Day"; //4 
        this.arrivaldgv.Columns["g_dd"].HeaderText = "Departure Day"; //5 
        this.arrivaldgv.Columns["g_amountofdays"].HeaderText = "Amount of Days"; //6 
        this.arrivaldgv.Columns["g_na"].HeaderText = "Number of Adults"; //7 
        this.arrivaldgv.Columns["g_amountofdays"].HeaderText = "Amount of Days"; //8 
        this.arrivaldgv.Columns["g_room"].HeaderText = "Room Number"; //9 
        this.arrivaldgv.Columns["g_rate"].HeaderText = "Rate"; //10 
        this.arrivaldgv.Columns["g_totalrate"].HeaderText = "Total Rate"; //11 
        this.arrivaldgv.Columns["g_info"].HeaderText = "Information"; //12 
        this.arrivaldgv.Columns["u_added"].HeaderText = "User"; //13 
        this.arrivaldgv.Columns["u_timeadded"].HeaderText = "Time"; //14 
        this.arrivaldgv.Columns["g_status"].HeaderText = "Status"; //15 
        /**************************************************************************/ 
        connection.Close(); 
       } 
       catch 
       { 
        MessageBox.Show("Please, try again."); 
       } 

有數據網格視圖在我的應用程序(這裏是截圖) Screenshot

有「簽到」按鈕,我想改變的值從「Arrival」到「In House」數據庫中的「g_status」。問題在於使用SQL命令和搜索選定行的算法。 下面是代碼,但它不工作:(更新)

private void checkinbtn_Click(object sender, EventArgs e) 
    { 
     int selectedrowindex = arrivaldgv.SelectedCells[0].RowIndex; 
     DataGridViewRow selectedRow = arrivaldgv.Rows[selectedrowindex]; 
     string value = Convert.ToString(selectedRow.Cells["g_status"].Value); 

     OleDbDataAdapter adapter; 
     OleDbCommandBuilder cmdBuilder; 
     string checkin = "In House"; 
     DataSet ds = new DataSet(); 
     string sql = "UPDATE guestreg SET g_status = '" + checkin + "'" + " where ID = '" + value + "';"; 
     Int32 i; 
     try 
     { 
      connection.Open(); 
      adapter = new OleDbDataAdapter(sql, connection); 
      cmdBuilder = new OleDbCommandBuilder(adapter); 
      adapter.Fill(ds); 

      for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) 
      { 
       ds.Tables[0].Rows[i].ItemArray[2] = checkin; 
      } 

      adapter.Update(ds.Tables[0]); 
      connection.Close(); 
      MessageBox.Show("Data updated ! "); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error:  " + ex); 
     } 
    } 

這裏是數據庫結構:screenshot

在此先感謝。

回答

1

是否有任何包含數據的唯一ID(數據庫主鍵)的列?

如果是的話,你可以使用:

int selectedrowindex = arrivaldgv.SelectedCells[0].RowIndex; 
DataGridViewRow selectedRow = arrivaldgv.Rows[selectedrowindex]; 
string value = Convert.ToString(selectedRow.Cells["corresponding column name"].Value); //maybe the first invisible column? 

string sql = "UPDATE guestreg SET g_status = '"+checkin+"'" + " where *name of primary key* = '" + value + "';"; 

而且,這裏是一個簡單的執行SQL命令的方式:

private void checkinbtn_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     var cmd = new OleDbCommand(); 
     cmd.CommandType = CommandType.Text; 
     cmd.CommandText = "UPDATE guestreg SET g_status='In House' where *name of primary key*[email protected]"; 
     cmd.Parameters.AddWithValue("@id", value); 
     cmd.Connection = connection; 
     connection.Open(); 
     cmd.ExecuteNonQuery(); 
     { 
      MessageBox.Show("Update Success!"); 
      connection.Close(); 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show("Error:  " + ex); 
    } 
} 

讓我知道如果我誤解了什麼,或者如果語法在某處是錯誤的,因爲我無法測試它。

+0

謝謝你的回答。我的數據庫中有ID列,但它在datagridview中不可見。將現在您的代碼:) –

+0

不幸的是它沒有工作:( –

+0

@AdilAliyev如果它沒有太多的麻煩,你可以發佈代碼,你嘗試更新你的問題? –