这段代码是一个更大功能的一部分我希望我已经复制了{}来正确地平衡它们。我有一个dataGridView已更新,这个percedure只是将更改写回一个空白的表?
OleDbCommand cmd = new OleDbCommand();
for (int ii = 0; ii < dataGridView1.Rows.Count; ii++)
{
DataGridViewRow dr = dataGridView1.Rows[ii];
if (dr.Selected == true)
{
string connetionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\temp\\Set.mdb;Persist Security Info=False";
try
{
int number = 0; //possible empty statment warning next line
using (OleDbConnection connection = new OleDbConnection(connetionString))
{
sql = "Insert into EnableOne SET Property=@var1, Pvalue=@var2, Pdefault=@var3, PType=@var4";
connection.Open(); //This fail's to compile connection does not exist in the current context
using (OleDbCommand cmd1 = new OleDbCommand(sql, connection))//This fail's to compile connection does not exist in the current context
{
number = dataGridView1.CurrentCell.RowIndex;
int numr = 0;
numr = dataGridView1.Rows.Count;
{
for (int x = 0; x < numr; x++)
{
cmd1.Parameters.Add(new OleDbParameter("@var1", dataGridView1.Rows[x].Cells[0].Value.ToString()));
cmd1.Parameters.Add(new OleDbParameter("@var2", dataGridView1.Rows[x].Cells[1].Value.ToString()));
cmd1.Parameters.Add(new OleDbParameter("@var3", dataGridView1.Rows[x].Cells[2].Value.ToString()));
cmd1.Parameters.Add(new OleDbParameter("@var4", dataGridView1.Rows[x].Cells[3].Value.ToString()));
cmd1.CommandText = sql;
cmd1.ExecuteNonQuery();
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
以上代码产生此错误
Error System.Data.OleDb.OleDbException (0x80040E14): Syntax error in INSERT INTO statement.
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) 在System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams,Object&amp; executeResult) 在System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object&amp; executeResult) 在System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior,Object&amp; executeResult) 在System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior,String method) 在System.Data.OleDb.OleDbCommand.ExecuteNonQuery() 在
中的CrewCheifSettingsBeta3.Form1.updater(字符串表)中答案 0 :(得分:1)
上面的代码中没有明显的问题可能是编译器错误的根源(正如您在代码注释中所说的那样)。但是还有很多其他问题会导致至少两个运行时错误和一个逻辑错误。
最重要的一个是INSERT INTO语句中使用的错误语法。使用INSERT INTO时没有SET。 (SET在UPDATE语句中使用)
.....
using (OleDbConnection connection = new OleDbConnection(connetionString))
{
sql = @"Insert into EnableOne (Property,Pvalue,Pdefault,PType)
VALUES (@var1, @var2, @var3, @var4)";
connection.Open();
using (OleDbCommand cmd1 = new OleDbCommand(sql, connection))
{
number = dataGridView1.CurrentCell.RowIndex;
int numr = dataGridView1.Rows.Count;
cmd1.Parameters.Add("@var1", OleDbType.VarWChar);
cmd1.Parameters.Add("@var2", OleDbType.VarWChar);
cmd1.Parameters.Add("@var3", OleDbType.VarWChar);
cmd1.Parameters.Add("@var4", OleDbType.VarWChar);
for (int x = 0; x < numr; x++)
{
cmd1.Parameters["@var1"].Value = dataGridView1.Rows[x].Cells[0].Value.ToString()));
cmd1.Parameters["@var2"].Value = dataGridView1.Rows[x].Cells[1].Value.ToString()));
cmd1.Parameters["@var3"].Value = dataGridView1.Rows[x].Cells[2].Value.ToString()));
cmd1.Parameters["@var4"].Value = dataGridView1.Rows[x].Cells[3].Value.ToString()));
// Not needed, already set in the constructor....
//cmd1.CommandText = sql;
cmd1.ExecuteNonQuery();
}
}
}
正如您所看到的,我做了其他更改。我在循环外定义参数而不给它们一个值,当在循环内部时我设置参数的值并执行查询。在您的代码中,不断向同一命令添加参数将导致每个循环的相同记录(第一个参数块)的一系列插入。
另一个问题是网格行上的双循环。这将导致重复记录(如果您没有主键保护您)或如果您尝试在表中插入重复键,则会显示错误消息。
// First loop over the grid rows.....
for (int ii = 0; ii < dataGridView1.Rows.Count; ii++)
....
// Second loop over the grid rows.
for (int x = 0; x < numr; x++)
我认为您可以删除内部循环并将x变量替换为外部循环中定义的ii变量。
因此,一般来说,我建议按照此架构重构您的代码
Create and open the connection in a using statement
Create and prepare the OleDbCommand in a using statement
Create the parameters needed without values
Start your loop over the rows
if the rows is selected
Set each parameter with the data from the selected row
Execute the query