【发布时间】:2017-04-07 04:03:06
【问题描述】:
我有
string insert_cmd_str;
using (conn_server)
{
using (NpgsqlCommand insert_cmd = new NpgsqlCommand())
{
insert_cmd.Connection = conn_server;
conn_server.Open();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
insert_cmd_str = "insert into @table values (" +
"'" + dataGridView1.Rows[i].Cells[0].FormattedValue + "', " +
"'" + dataGridView1.Rows[i].Cells[1].FormattedValue + "', " +
"'" + dataGridView1.Rows[i].Cells[2].FormattedValue + "', " +
"'" + dataGridView1.Rows[i].Cells[3].FormattedValue + "', " +
"'" + dataGridView1.Rows[i].Cells[4].FormattedValue + "', " +
"'" + dataGridView1.Rows[i].Cells[5].FormattedValue + "', " +
"'" + dataGridView1.Rows[i].Cells[6].FormattedValue + "', " +
"'" + dataGridView1.Rows[i].Cells[7].FormattedValue + "', " +
"'" + dataGridView1.Rows[i].Cells[8].FormattedValue + "', " +
"'" + dataGridView1.Rows[i].Cells[9].FormattedValue + "')";
insert_cmd.CommandText = insert_cmd_str;
insert_cmd.Parameters.AddWithValue("@table", "table");
MessageBox.Show(insert_cmd.CommandText + Environment.NewLine +
insert_cmd.Parameters["@db"].Value);
insert_cmd.ExecuteNonQuery();
}
conn_server.Close();
}
}
我得到一个错误:
错误:42601:“(”或附近的语法错误
我添加了MessageBox 以在命令文本执行之前检查它,这似乎很好。如果我将相同的文本发布到 PG admin 中,它会执行命令而不会出现错误。
命令文本为:
insert into @table values ('425', '10-31-2016 00:00:00', 'False', '', 'test', 'test', 'test', 'pas_kosmetologus', 'Skambutis', '237')
@table 的值为table。
我不知道错误是从哪里来的。我什至尝试从命令文本中删除两个括号,但仍然抛出相同的错误。
我在这里遗漏了什么明显的东西吗?
【问题讨论】:
-
你的桌子叫桌子?那是一个可能导致您出现问题的关键字。
-
并非如此。我只是在这里更改它以简化它。需要向其中插入数据的表的实际名称取决于应用程序的用户。
-
可能是插入语句首先需要列,例如
insert into (column1, ...) values (value1, ...) -
现在是 vizitai_test。我认为这不会导致错误。
-
@Martynas 你检查了吗?
标签: c# sql winforms visual-studio npgsql