0

我有将数据插入支付分配表的 asp.net detailsview,但是我想运行一个更新命令,将数据更新到 s_transaction_enquiry 表中,这是在 C# 中完成的。当我运行代码时,更新命令在插入命令之前首先运行,因此不会将数据放入 s_transaction_enquiry 表中。

我创建了更新命令以在用户单击详细信息视图中的插入按钮时运行。插入命令链接到详细视图的 Sql 数据源。

有人告诉我我可以在 page_load 中使用“IsPostBack”属性,但确定如何做到这一点,有没有人可以帮助我?

      protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            string conn = "";
            string sqlCOmmand = "UPDATE s_transaction_enquiry, payment_allocation SET s_transaction_enquiry.payment_amount = payment_allocation.payment_amount, s_transaction_enquiry.payment_ref = payment_allocation.payment_ref, s_transaction_enquiry.payment_received_date = payment_allocation.payment_received_date WHERE payment_allocation.s_invoice_numer = s_transaction_enquiry.s_invoice_number AND payment_allocation.account_number = s_transaction_enquiry.account_number";
            conn = ConfigurationManager.ConnectionStrings["Conn"].ToString();
            UpdateRow(conn, sqlCOmmand);
        }


    }

    public void UpdateRow(string connectionString, string insertSQL)
    {
        using (OleDbConnection connection = new OleDbConnection(connectionString))
        {

            OleDbCommand command = new OleDbCommand(insertSQL);


            command.Connection = connection;

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
    }

从数据源插入语句:

  <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" DeleteCommand="DELETE FROM [payment_allocation] WHERE [payment_ref] = ?" ProviderName="<%$ ConnectionStrings:ConnectionString1.ProviderName %>" SelectCommand="SELECT * FROM [payment_allocation]" 
             InsertCommand="INSERT INTO [payment_allocation] ([payment_ref], [account_number], [account_ref], [allocate_date], [payment_amount], [payment_received_date], [s_invoice_numer]) VALUES (?, ?, ?, ?, ?, ?, ?)">

调用插入语句的按钮:

  <asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Insert" Text="Create"/>
4

1 回答 1

0

您应该在每次操作后关闭连接。您已经在 'page_load' 上打开了一个连接,但没有关闭它,并且由于page_load在每次回发时也会执行,这意味着每次单击按钮时,您都会尝试再次打开连接而不关闭它。

还要注意它在事件处理程序page_load之前执行,button_click因此每次单击按钮时都会打开连接而不关闭,并尝试在单击处理程序中再次打开它。

OleDbConnection 是一次性对象,这意味着它实现了 .dispose() 函数,这也意味着它可以在using()语句中使用。

using() 语句创建一个新对象并在之后处理它。你有一个很好的解释在 C# 中打开/关闭/处理 OleDbConnection 在微软网站上:OleDbConnection 类

基本上为了让它适应你的代码,你会想做这样的事情,首先为了方便起见将你的数据库处理放在一个单独的函数中,然后从页面加载中调用它::

    protected void Page_Load(object sender, EventArgs e)
    {
        string conn = "";
        string sqlCOmmand= "inset blablabla into blablabla";
        conn = ConfigurationManager.ConnectionStrings["Conn"].ToString();


        InsertRow(conn,sqlCOmmand);


    }


//taken from microsoft website
public void InsertRow(string connectionString, string insertSQL)
{
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        // The insertSQL string contains a SQL statement that 
        // inserts a new row in the source table.
        OleDbCommand command = new OleDbCommand(insertSQL);

        // Set the Connection to the new OleDbConnection.
        command.Connection = connection;

        // Open the connection and execute the insert command. 
        try
        {
            connection.Open();
            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        // The connection is automatically closed when the 
        // code exits the using block.
    }
}

好了,说清楚了这部分,就进入下一个:

我假设您只想在页面加载正确时才插入数据库?而不是在每次单击按钮后。因此,您需要指示 asp.net 仅在第一次加载而不是回发时才执行您的代码。您可能希望将 page_load 函数更改为如下所示:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string conn = "";
            string sqlCOmmand = "inset blablabla into blablabla";
            conn = ConfigurationManager.ConnectionStrings["Conn"].ToString();
            InsertRow(conn, sqlCOmmand);
        }
    }

至于您的点击处理程序,您应该创建另一个函数 UpdateRow 并使用 using() 语句执行相同操作并将其放入您的点击处理程序

于 2014-03-23T14:17:53.997 回答