我正在使用此代码删除表中的行,但它无效。 我犯了错误还是错过了什么?
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();
有任何线索吗?感谢。
回复:旁观者
显示此错误消息:
http://i901.photobucket.com/albums/ac218/pcser/error.jpg
答案 0 :(得分:1)
string sql = "DELETE FROM books WHERE \"BookID\"=1;";
这是错误的:
string sql = "DELETE FROM books WHERE BookID=1;";
再次感谢您的帮助。
答案 1 :(得分:0)
您是否尝试过使用NpgsqlCommand.ExecuteNonQuery Method
像
这样的东西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);
command.ExecuteNonQuery(); //this line here??
conn.Close();
}