2013-12-20 121 views
0

當我調試此程序時,我通過表單添加數據,並通過下一個和上一個按鈕檢查數據。表單顯示數據,但是當我通過服務器瀏覽器打開數據庫並檢查數據時,數據庫中沒有數據。我已經多次這樣做了。數據未加載到數據庫中

注意:我試過更改COPY TO OUTPUT DIRECTORY屬性,但它沒有幫助。我也已經在表格中添加了一些數據。

public partial class Form1 : Form 
    { 
     DatabaseConnection objConnect; 
     string conString; 

     DataSet ds; 
     DataRow dRow; 

     int MaxRows; 
     int inc = 0; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void label1_Click(object sender, EventArgs e) 
     { 

     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      try 
      { 
       objConnect = new DatabaseConnection(); 
       conString = Properties.Settings.Default.Employees1ConnectionString; 

       objConnect.Connection_string = conString; 
       objConnect.Sql = Properties.Settings.Default.SQL; 

       ds = objConnect.Getconnection; 
       MaxRows = ds.Tables[0].Rows.Count; 

       NavigateRecords(); 

      } 
      catch (Exception err) 
      { 
       MessageBox.Show(err.Message,"Error"); 
      } 
     } 

     private void NavigateRecords() 
     { 
      dRow = ds.Tables[0].Rows[inc]; 
      txtFirstname.Text = dRow.ItemArray.GetValue(1).ToString(); 
      txtSurname.Text = dRow.ItemArray.GetValue(2).ToString(); 
      txtJobtitle.Text = dRow.ItemArray.GetValue(3).ToString(); 
      txtDepartment.Text = dRow.ItemArray.GetValue(4).ToString(); 
     } 

     private void btnNext_Click(object sender, EventArgs e) 
     { 
      if (inc != MaxRows - 1) 
      { 
       inc++; 
       NavigateRecords(); 
      } 
      else 
      { 
       MessageBox.Show("No more rows!"); 
      } 
     } 

     private void btnPrevious_Click(object sender, EventArgs e) 
     { 
      if (inc > 0) 
      { 
       inc--; 
       NavigateRecords(); 
      } 
      else 
      { 
       MessageBox.Show("First Record!"); 
      } 
     } 

     private void btnaddnew_Click(object sender, EventArgs e) 
     { 
      txtFirstname.Clear(); 
      txtSurname.Clear(); 
      txtJobtitle.Clear(); 
      txtDepartment.Clear(); 
      btnaddnew.Enabled = false; 
      btnsave.Enabled = true; 
      btnCancel.Enabled = true; 
     } 

     private void btnsave_Click(object sender, EventArgs e) 
     { 
      DataRow row = ds.Tables[0].NewRow(); 
      row[1] = txtFirstname.Text; 
      row[2] = txtSurname.Text; 
      row[3] = txtJobtitle.Text; 
      row[4] = txtDepartment.Text; 

      ds.Tables[0].Rows.Add(row); 

      try 
      { 
       objConnect.Updatedatabase(ds); 

       MaxRows = MaxRows + 1; 
       inc = MaxRows - 1; 

       MessageBox.Show("Database has been updated"); 
      } 
      catch (Exception err) 
      { 
       MessageBox.Show(err.Message); 
      } 

      btnaddnew.Enabled = true; 
      btnsave.Enabled = false; 
      btnCancel.Enabled = false; 

     } 

     private void btnCancel_Click(object sender, EventArgs e) 
     { 
      NavigateRecords(); 
      btnaddnew.Enabled = true; 
      btnsave.Enabled = false; 
      btnCancel.Enabled = false; 
     } 


    }` class DatabaseConnection 
    { 
     private string sql_string; 
     private string strCon; 
     SqlDataAdapter da_1; 

     public string Sql 
     { 
      set { sql_string = value; } 
     } 

     public string Connection_string 
     { 
      set { strCon = value; } 
     } 

     public DataSet Getconnection 
     { 
      get { return MyDataSet(); } 
     } 

     public DataSet MyDataSet() 
     { 
      SqlConnection con = new SqlConnection(strCon); 

      con.Open(); 

      da_1 = new SqlDataAdapter(sql_string, con); 

      DataSet dat_set = new DataSet(); 
      da_1.Fill(dat_set, "Table_Data_1"); 
      con.Close(); 

      return dat_set; 
     } 

     public void Updatedatabase(DataSet ds) 
     { 
      SqlCommandBuilder cb = new SqlCommandBuilder(da_1); 
      cb.DataAdapter.Update(ds.Tables[0]); 
     } 


    } 
+0

您確定服務器資源管理器使用的連接與您的程序在運行時使用的連接是否相同? – Steve

+0

@Steve,你是什麼意思? – FeliceM

+0

我在其他一些窗體中創建了一個數據庫,並通過添加資源選項添加它。 那好嗎? –

回答

0

This可能會幫助你。您需要使用select命令配置插入/更新/刪除。

+0

謝謝..我遵循相同的流程,但採取了不同的方式。 請幫助我我真的很感激你。 –

+0

使用相同的數據集來填充和更新:da_1.Fill(ds,「Table_Data_1」);在MyDataSet()中; –