2014-03-13 22 views
0

我想訪問全局變量「i」的前一個值。 這是我的代碼。我如何訪問變量的前一個值?

int i = 0; 
    private void btnSave_Click(object sender, EventArgs e) 
    { 
     if (con.State == ConnectionState.Open) { con.Close(); } 
     try 
     { 

      con.Open(); 
      i++; 
      //int i2=i-1; 
      String s = "insert into Add_Information values('"+i +"','"+ txtCompanyName.Text + "','" + txtOffAddress1.Text + "','" + txtOffAddress2.Text + "','" + txtCityAndPin.Text + "','" + txtContactPersonName.Text + "','" + txtDesignation.Text + "'," + txtOffPhoneNo.Text + "," + txtFaxNo.Text + "," + txtMobileNo.Text + ",'" + txtOfficialEmailId.Text + "','" + txtPersonalEmailId.Text + "','" + txtAlternativeContactPersonName.Text + "','" + txtWebsite.Text + "','" + txtClientClassification.Text + "','" + txtDetails.Text + "','" + txtGroup.Text + "','" + pictureBox1.Image + "')"; 



      SqlCommand cmd = new SqlCommand(s, con); 
      if (cmd.ExecuteNonQuery() >= 1) 
      { 

       MessageBox.Show("Succesfully Updated !!"); 

      } 
      con.Close(); 
     } 
     catch (Exception ex) 
     { 
      /*if(txtFaxNo.Text==""||txtMobileNo.Text==""||txtOffPhoneNo.Text=="") 
      { 
       MessageBox.Show("Fax No,Mobile No,Office No should not be character"); 
      }*/ 
      con.Close(); 
      MessageBox.Show(ex.Message); 
     } 

我要訪問我的以前的值存儲在數據表中Add_Information即使在目前的形式被關閉

回答

0

可以使Add_Information一個標識列,並且不通過它的價值,在DBMS會自動爲您生成增加的值。您還可以查詢數據庫以獲取列Add_Information的最大值。如果您在應用程序中存儲值,那麼它可能在表單關閉時可用,但在應用程序關閉時會丟失。

1

我同意阿迪爾但是,如果你不想做

Add_Information

身份編寫返回的

Add_Infomation

最大值存儲過程

from table and使用它