Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将更改从dataGridView保存到SQL Server DB_C#_Sql_Sql Server_Database_Datagridview - Fatal编程技术网

C# 将更改从dataGridView保存到SQL Server DB

C# 将更改从dataGridView保存到SQL Server DB,c#,sql,sql-server,database,datagridview,C#,Sql,Sql Server,Database,Datagridview,我有一个带有dataGridView的表单来显示考勤表的内容 TagID SessionID ScanningTime --------------------------------------- 4820427 Test1 14/08/2013 18:12 我想手动向表中添加一条记录,如下所示 TagID SessionID ScanningTime -----------------

我有一个带有dataGridView的表单来显示考勤表的内容

    TagID      SessionID       ScanningTime
    ---------------------------------------
    4820427      Test1       14/08/2013 18:12
我想手动向表中添加一条记录,如下所示

    TagID      SessionID       ScanningTime
    ---------------------------------------
    4820000      Test1       14/08/2013 18:12
    0000001      Test2       15/08/2012 17:00
…并在单击按钮后将更改保存到SQL Server数据库

我已尝试创建更新查询:

command.Text = "UPDATE Attendance
SET TagID= @tagNo, SessionID= @sessionNo, ScanningTime= @scantime"; 
但我不知道如何将DGV中的值指定给参数

将更改从dataGridView保存到SQL Server数据库的正确方法是什么


请注意,在使用DGV时,我不使用DataSet或TableAdapter。

您需要使用
DataGridView
RowsAdded
方法,并缓存必要的信息以插入数据:

// new form field for caching
private List<DataGridViewRow> _addedRowsCache = new List<DataGridViewRow>();

private void dataGridView1_RowsAdded(object sender,
    DataGridViewRowsAddedEventArgs e)
{
    for (int i = e.RowIndex; i < e.RowIndex + e.RowCount; i++)
    {
        _addedRowsCache.Add(dataGridView.Rows[i]);
    }
}

正确的方法是开放式问题,但要回答您的问题,正确的方法是开始编写一些代码,并测试您所编写的内容是否会产生预期的结果。。展示一些
主动性/努力
@DJKRAZE感谢您的评论。我已经更新了我的问题。这真的很有帮助,谢谢。我正在寻找一种方法来更新表,如果有任何更改(不仅添加一行,还修改现有记录)@jaspernoth,还有一些事情。首先,如果您发现
RowsAdded
不适用于缓存,您也可以尝试
UserAddedRow
。其次,对于更新,使用
CellBeginEdit
事件缓存
DataGridViewRow
。事件args将允许您返回到行。但是,请将这些行缓存在不同的列表中,以便了解差异。
// new class field to store the INSERT sql
private string _insertSQL = "INSERT INTO tbl (field1, field2) VALUES (@field1, @field2)";

// this block goes inside the click event
if (_addedRowsCache.Count > 0)
{
    using (SqlConnection c = new SqlConnection(connString))
    {
        c.Open();

        foreach (DataGridViewRow r in _addedRowsCache)
        {
            using (SqlCommand cmd = new SqlCommand(sql, c))
            {
                // add any parameter values, I don't know where `val(n)`
                // comes from here. Maybe from the `DataBoundItem`
                // off the `DataGridViewRow`, or maybe from a `Cell`
                // out of the `Cells` collection of the `DataGridViewRow`
                cmd.Parameters.AddWithValue("@field1", val1);
                cmd.Parameters.AddWithValue("@field2", val2);

                cmd.ExecuteNonQuery();
            }
        }
    }
}