0

我正在尝试使用 npgsql 创建我的新 asp.net 网站。我的数据库如下所示:

[oid, countyname, status]

使用 npgsql 我已经在 gridview 中连接并显示了数据。我已经使用以下代码完成了此操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Npgsql;
using System.Data;
using System.Web.Security;


public partial class Secured_pia : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {         
        string sQuery = "SELECT countyname, status FROM countydb";
        string sConn = "Server=localhost;Port=5432;User Id=user1;Password=pass2;Database=database;";

        DataTable DT = new DataTable();
        NpgsqlConnection Conn = new NpgsqlConnection(sConn);

        Conn.Open();

        DataSet DS = new DataSet("DS1");
        NpgsqlDataAdapter DA = new NpgsqlDataAdapter();

        DA.SelectCommand = new NpgsqlCommand(sQuery, Conn);

        DA.Fill(DS, "DS1");

        GridView1.DataSource = DS;
        GridView1.DataBind();

        Conn.Close();
    }
}

一切正常。现在我想编辑该表。正如我读得很好,我可以使用 NpgsqlCommandBuilder 来制作插入、更新和删除命令。所以我把代码改成这样:

DA.SelectCommand = new NpgsqlCommand(sQuery, Conn);
NpgsqlCommandBuilder UpdateCMD = new NpgsqlCommandBuilder(DA);         
DA.Fill(DS, "DS1");

现在,我不知道该怎么做才能打开编辑。该代码是否足以在gridview中打开编辑?我的 aspx 代码如下所示:

<asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:CommandField ShowEditButton="true" />
    </Columns>
</asp:GridView>

aspx.cs 代码好吗?我必须向我的网格视图添加任何属性吗?感谢您的回复,并对我的简单问题感到抱歉。我是这方面的新手。

4

1 回答 1

0

我只想在gridview旁边的页面上添加一个按钮。然后像这样实现你的更新按钮点击事件:

 public partial class Secured_pia : System.Web.UI.Page
    {
        String sConn = "Your connection string";
        private NpgsqlConnection conn;
        private NpgsqlDataAdapter da;
        private DataSet ds = new DataSet();
        private DataTable dt = new DataTable();
        private NpgsqlCommandBuilder cb;

        public Secured_pia ()
        {
            InitializeComponent();
        }
//your form load code above goes here but you will have to do 
//a global declaration like I did here instead of in the form load event like you have. 

//update button implementation.  
private void btnUpdate_Click(object sender, EventArgs e)
 {
    cb = new NpgsqlCommandBuilder(da);           
    da.Update(dt);
 }
于 2014-01-07T22:00:33.383 回答