选择的行插入到数据库

时间:2021-04-21 09:32:03

标签: c# sql datagridview

我想从 datagridview 中选择的数据插入数据。

这是我的代码

using (var connection = HelperLayer.GetConnection())
{
    try
    {
        connection.Open();
        string sqll = @"insert into SmsTableInvoice(code, telno, ficheno, TRCode, netTotal,message) 
                        values('"+code+"','"+telno+ "','"+ ficheno+ "','"+ trcode+ "','"+ total + "','"+ConvertStringArrayToString(sms)+ "') ";
         
        var command = new SqlCommand(sqll, connection);
        command.ExecuteNonQuery();
        Control();
     
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

此代码添加所有数据 如何添加刚刚选定的行 谢谢大家

1 个答案:

答案 0 :(得分:0)

保存按钮动作:
如果一个数据: var index = dataGridView.CurrentRow.Index;
代码 = dataGridView.Rows[i].Cells["code"].Value.ToString(); ....
如果 > 一个数据: for(int i=0;i 如果 EditedFormattedValue == true {
代码 = dataGridView.Rows[i].Cells["code"].Value.ToString(); ....}}

相关问题