【问题标题】:Refreshing SQL after form is closed表单关闭后刷新 SQL
【发布时间】:2012-02-14 17:42:17
【问题描述】:

我创建了一个将数据插入 SQL 数据库的应用程序。基本上,用户从主表单打开第二个表单,提示他们输入将进入数据库的数据。代码如下所示:

       private void submitButton_Click(object sender, EventArgs e)
        {
//Get form values
                try
                {
//Open test connection
                }
                catch (Exception ex)
                {
//Handle errors
                }
                finally
                {
//Close test connection
                }

                //Run the SQL statements
                try
                {
//Inser SQL data
                }
                catch (Exception ie)
                {
//Handle errors
                }
                finally
                {
                    //Close the connection
                    if (conn.State != ConnectionState.Closed)
                    {
//Close the connection
                    }
                     //Close the window
                     this.Close();
                     //Tell the main form to reload SQL data (not working)
                     mainForm firstForm;
                     firstForm = new mainForm();
                     firstForm.refreshCall();

                }

            }
        }

所以基本上,当用户点击OK(submitButton)时,数据被插入,窗口被关闭,refreshCall()方法被调用。我的方法'refreshCall'应该刷新主窗体上输出的SQL数据,如下所示:

public void refreshCall()
{
    SqlConnection conn = new SqlConnection("Data Source=SERVER\\SQL_DB;Initial Catalog=dataTable;Integrated Security=True");
    try
    {
        conn.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT part_num from dbo.Parts", conn);
        adapter.Fill(ds);
        this.listParts.DataSource = ds.Tables[0];
        this.listParts.DisplayMember = "part_num";
        conn.Close();
    }
    catch (SqlException odbcEx)
    {
        MessageBox.Show("There was an error connecting to the data source.\nError Code: 1001", "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

}

但是,当调用此方法时,数据仍未刷新,这意味着我必须关闭应用程序并重新加载它才能看到我的更改。我的代码中是否有什么地方做错了导致 SQL 数据保持不变?有一个更好的方法吗?我还应该注意,我使用与refreshCall 完全相同的代码在表单初始化时加载数据,并且工作正常。

感谢任何帮助!

【问题讨论】:

  • 当您单步调试调试器时 - 您的 SQL 插入代码是否在刷新调用之前被调用?另外-您可以发布您的SQL插入代码吗?连接/事务可能正确完成....

标签: c# sql winforms


【解决方案1】:

似乎第二个表单正在创建主表单的新实例。新实例永远不会显示。您需要为您的 refreshCall 获取现有的主表单。

【讨论】:

  • 如何让它在现有表单上调用 refreshCall(而不是创建新表单)?
【解决方案2】:

在您打开第二个表单时,您可以在主表单上使用以下代码。

// the ShowDialog will open your second form and then you use your DialogResult 
//  from the second form to tell your main form to refresh the data
yourSecondForm openSecondForm = new yourSecondForm.ShowDialog();
if(openSecondForm.DialogResult == DialogResult.OK)
{
    refreshCall()
}

然后在你的 finally 块中的第二种形式:

finally
{
    //Close the connection
    if (conn.State != ConnectionState.Closed)
    {
        //Close the connection
    }
    //Close the window
    DialogResult = DialogResult.OK;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多