我正在使用此代码删除表中的行,但它不起作用。
我是犯了错还是漏掉了什么?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Npgsql;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button_RemoveBook_Click(object sender, EventArgs e)
        {
            NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;Port=5432;User Id=user;Password=1234;Database=library;");
            conn.Open();
            string sql = "DELETE FROM books WHERE BookID=1;";
            NpgsqlCommand command = new NpgsqlCommand(sql, conn);
            conn.Close();
        }
    }
}

上面的任务运行时,我注意到数据库没有做任何更改。bookID=1,这是数据库中的第一行,仍然保留。
我试过使用INSERT命令,它起作用了。新数据已插入表的最后一行。下面的代码运行良好。
private void button_addBook_Click(object sender, EventArgs e)
{
    NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;Port=5432;User Id=user;Password=1234;Database=library;");
    conn.Open();
    string addRow = string.Format("insert into books values(50,'Time','Frank','Science')");
    NpgsqlCommand command = new NpgsqlCommand(addRow, conn);
    conn.Close();

有线索吗?谢谢。
回复:astander
它显示以下错误消息:
http://i901.photobucket.com/albums/ac218/pcser/error.jpg

最佳答案

阿斯坦德和伙计们。
谢谢你的帮助。
经过几次尝试,我找到了解决办法。
BookID是列的名称,必须在列名之前和之后添加此符号。
因此,正确的方法是:

    string sql = "DELETE FROM books WHERE \"BookID\"=1;";

这是错误的:
    string sql = "DELETE FROM books WHERE BookID=1;";

再次感谢你的帮助。

10-02 01:27
查看更多