2013-03-11 53 views
7

我在datagridview中显示数据库表。我可以将记录从datagridview正确保存到sql中的数据库。将编辑的数据保存在行中

现在,我想修改和更改一些记录并将这些更改保存在数据库中。我怎样才能做到这一点?我使用的绑定datasource附加到数据集datatable

private void Form1_Load(object sender, EventArgs e) 
{ 
    this.cPDM0020TableAdapter.Fill(this.cpdm_dataset.CPDM0020); 
} 

private void btnSave_Click(object sender, EventArgs e) 
{ 
    string code = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString().ToUpper(); 
    string currency_Name = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString().ToUpper(); 
    string boolBase = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString(); 
    string local_per_Base = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString(); 
    string base_per_Local = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString(); 
    string insert_sql = "INSERT INTO centraldb.dbo.CPDM0020(Code,Currency_Name,Base,Local_per_Base,Base_per_Local)VAL‌​UES('" + 
     code + "','" + 
     currency_Name + "','" + 
     boolBase + "','" + 
     local_per_Base + "','" + 
     base_per_Local + "')"; 

    if (this.ExecuteSql(insert_sql)) 
    { 
     MessageBox.Show("Record Inserted Successfully."); 
    } 
    else 
    { 
     MessageBox.Show("Insert Failed"); 
    } 
} 

public bool ExecuteSql(string command) 
{ 

    SqlCommand sqlCommand = new SqlCommand(command, connection); 
    connection.Open(); 
    sqlCommand.ExecuteNonQuery(); 
    this.cPDM0020TableAdapter.Fill(this.cpdm_dataset.CPDM0020); 
    dataGridView1.DataSource = cpdm_dataset.CPDM0020; 
    sqlCommand.Dispose(); 
    connection.Close(); 
    return true; 
} 

我可以保存新的条目很容易地在数据库和datagridview,但保存按钮编辑后,我不能编辑已经存在records..On点击,它再次显示了先前的值。请帮忙。

回答

0

尝试使用下面的代码>

try 
{ 
    var row = gvTransactions.CurrentRow; 
    int ID= int.parse(row.Cells[0].Value.ToString()); 
    string abc=row.Cells[0].Value.ToString(); 
} 
catch(exception ex) 
{ 
} 

获取这样的值。

注意:不要写任何东西在catch块

,然后火插入/更新查询根据您的需要。

+0

你不能声明在try块变量,一旦试已经结束,他们将丢失。 – Flater 2013-03-11 13:17:22

+1

另外,神奇宝贝异常处理通常是一个坏主意。 – Flater 2013-03-11 13:18:41

+0

这是我的代码:私人无效Form1_Load的(对象发件人,EventArgs的) { this.cPDM0020TableAdapter.Fill(this.cpdm_dataset.CPDM0020);} – user2156513 2013-03-12 04:26:40

2

您的数据库不受您的应用程序控制;当数据发生变化时,它不会将某个事件发送回您的应用程序。您必须主动重新查询数据库以进行更改。

更典型的DataGridView方法是首先将更改应用到本地数据副本,然后使用DataAdapter将更改推回到数据库。这样可以避免在更改完成后刷新整个本地数据集。请参阅使用DataAdapter更新数据源(ADO.NET)。

的基本步骤是:

  1. 连接到数据源,使用DataAdapter.Fill方法()来填补你的数据表
  2. 定义一个UpdateCommand或将InsertCommand定义如何在本地数据表,数据将被推到数据库
  3. 将一行添加到本地数据表中
  4. 调用DataAdapter.Update()将更新推送到数据库。
1

只要你有检查记录在表中存在先用选择命令

“SELECT * FROM centraldb.dbo.CPDM0020其代码=“” +代码+“””;如果记录存在执行更新查询

public bool IsExistRecord(string Query) 
    { 
     try 
     { 
      DataTable DT = new DataTable(); 
      SqlCommand CMD = new SqlCommand(Query, Connection); 
      SqlDataAdapter DA = new SqlDataAdapter(CMD); 
      DA.Fill(DT); 

      if (DT.Rows.Count > 0) 
       return true; 
      else 
       return false; 

     } 
     catch (Exception ex) 
     { 
      return false; 
     } 
    } 

如果不存在执行插入查询:

您可以使用此功能。

0

进行更新按钮:

private void btnUpdate_Click(object sender, EventArgs e) { 
    string code = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString().ToUpper(); 
    string currency_Name = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString().ToUpper(); 
    string boolBase = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString(); 
    string local_per_Base = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString(); 
    string base_per_Local = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString(); 
    string select_qry = "Select * from centraldb.dbo.CPDM0020 Where Code = '" + Code + "'"; 
    if(IsExistRecord(select_qry)) 
    { 
    string update_qry = Update centraldb.dbo.CPDM0020 set Code,Currency_Name='" + currency_Name + "',Base='" + boolBase + "',Local_per_Base,Base_per_Local='" + base_per_Local + "' where code='" + code +"'"; 
    if (this.ExecuteSql(update_qry)) { 
     MessageBox.Show("Record Updated Successfully."); 
    } else { 

     MessageBox.Show("Update Failed"); 
    } 
    } 
} 
    //code taken from Mohammad abumazen 
public bool IsExistRecord(string Query) 
{ 
     DataTable DT = new DataTable(); 
     SqlCommand CMD = new SqlCommand(Query, Connection); 
     SqlDataAdapter DA = new SqlDataAdapter(CMD); 
     DA.Fill(DT); 

     if (DT.Rows.Count > 0) 
      return true; 
     else 
      return false; 

    } 
0

由于您使用的是dataSet,你可以在你的tableApdater创建一个命令。 这里是关于Creating a Data Access Layer的文章,因为您在winforms中,您可以将该文章应用到您的项目中。

我希望这张图片能给你提示save,update,edit,delete

enter image description here

相关问题