2017-09-27 94 views
0

Edit1:“你不能在sql中参数化标识符,SET @Column = @Value不起作用。”SqlCommand UPDATE不更新数据库

所以,如果我修改datagridview,我不能做一个迭代,我告诉sql服务器应该更新哪些列?我是否需要更新该行的每个元素?感谢您的其他建议。

我想这个C#代码来更新我的SQL Server数据库:

try 
{ 
    string parancs = "UPDATE Equipment SET @Column = @Value, Modifier = @Modifier, Modified = @Modified " + 
        "WHERE Description = @Description AND [Plane_A/C] = @Plane"; 

    SqlCommand sqlComm = new SqlCommand(parancs, connection); 
    DateTime time = DateTime.Now; 

    foreach (DataGridViewRow row in dataGridView.Rows) 
    { 
     foreach (DataGridViewCell cell in row.Cells) 
     { 
      if (cell.Value == null) 
      { 
       cell.Value = DBNull.Value; 
      } 
     } 
    } 

    sqlComm.Connection.Open(); 

    for (int i = 0; i < rowIndexes.Count; i++) 
    { 
     sqlComm.Parameters.Clear(); 
     sqlComm.Parameters.AddWithValue("@Column", dataGridView.Columns[columnIndexes[i]].HeaderText); 
     sqlComm.Parameters.AddWithValue("@Value", dataGridView.Rows[rowIndexes[i]].Cells[columnIndexes[i]].Value); 
     sqlComm.Parameters.AddWithValue("@Description", dataGridView.Rows[rowIndexes[i]].Cells["Description"].Value); 
     sqlComm.Parameters.AddWithValue("@Plane", ChoosenAC); 
     sqlComm.Parameters.AddWithValue("@Modifier", "TesztAdmin"); 
     sqlComm.Parameters.AddWithValue("@Modified", time); 

     sqlComm.ExecuteNonQuery(); 
    } 

    sqlComm.Connection.Close(); 
    MessageBox.Show("Sikeres módosítás!"); 
} 
catch (Exception ex) 
{ 
    MessageBox.Show(ex.ToString()); 
} 

rowIndexes.Count是修改的行和columnIndexes的数量是他们的立场。代码无一例外地运行,但数据不会更新。在SQL Server Profiler中,我得到:

exec sp_executesql N'UPDATE Equipment SET @Column = @Value, Modifier = @Modifier, Modified = @Modified WHERE Description = @Description AND [Plane_A/C] = @Plane',N'@Column nvarchar(11),@Value float,@Description nvarchar(6),@Plane nvarchar(8),@Modifier nvarchar(10),@Modified datetime',@Column=N'InspectHour',@Value=800,@Description=N'Engine',@Plane=N'TEST-REP',@Modifier=N'TesztAdmin',@Modified='2017-09-27 12:44:14.773' 

所以所有的参数都会得到值。如果我将UPDATE命令复制到SSMS并使用精确值而不是参数,则它可以正常工作并进行更新。

当我在程序中使用INSERT命令而不是UPDATE的相同方法时,它可以正常工作。我希望我写下一切,你可以帮忙。

蒂博尔

+2

你不能在sql中参数化标识符。 'SET @Column = @ Value'将不起作用。 –

+1

'@ Column'有什么用?当然,您无法将列更新为参数。尝试删除该参数并在命令中添加特定的列名称。然后看到它工作正常。 –

+0

我认为他们有一个名为“列”的表列。桌子是一个财产袋。这不是关于列标识符的参数化。至于为什么在Profiler输出中用@显示@Column的分配,则是另一回事。注意下面的分配是不是如何。 – dlatikay

回答

0

按照我提出的意见,我可以提供改进的代码示例:

const string parancs = 
    "UPDATE Equipment SET {column-name} = @Value, Modifier = @Modifier, Modified = @Modified " + 
    "WHERE Description = @Description AND [Plane_A/C] = @Plane"; 

DateTime time = DateTime.Now; 

foreach (DataGridViewRow row in dataGridView.Rows) 
{ 
    foreach (DataGridViewCell cell in row.Cells) 
    { 
     if (cell.Value == null) 
     { 
      cell.Value = DBNull.Value; 
     } 
    } 
} 

using (connection) 
using (var sqlComm = connection.CreateCommand()) 
{ 
    connection.Open(); 

    for (int i = 0; i < rowIndexes.Count; i++) 
    { 
     sqlComm.Parameters.Clear(); 
     sqlComm.Parameters.AddWithValue("@Value", dataGridView.Rows[rowIndexes[i]].Cells[columnIndexes[i]].Value); 
     sqlComm.Parameters.AddWithValue("@Description", dataGridView.Rows[rowIndexes[i]].Cells["Description"].Value); 
     sqlComm.Parameters.AddWithValue("@Plane", ChoosenAC); 
     sqlComm.Parameters.AddWithValue("@Modifier", "TesztAdmin"); 
     sqlComm.Parameters.AddWithValue("@Modified", time); 

     sqlComm.CommandText = parancs.Replace("{column-name}", dataGridView.Columns[columnIndexes[i]].HeaderText); 

     try 
     { 
      sqlComm.ExecuteNonQuery(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString()); 
      throw; 
     } 
    } 

    sqlComm.Connection.Close(); 
} 

MessageBox.Show("Sikeres módosítás!"); 

注意,该try/catch块现在包含一个单行 - 数据库调用。它可能会产生我们无法控制的错误。

此外,SqlConnectionSqlCommand实施Disposable Pattern所以他们需要在using块被使用,以防止memory leak