从表单调用组件到类,并将数据保存回表



我正在做ado.net项目,我曾经有单独的代码类…现在我正试图更多地了解本地数据库,但当我试图调用包含SQL命令的类时,我是股票……请看看我的代码,我注释了我的股票

    namespace Maintenance
    {
        public partial class registerReports : Form
        {
            private void btnSave_Click(object sender, EventArgs e)
            {
                //the code works if it exists here
                /**
                    conn.Open();
                    com.Parameters.AddWithValue("@reportID",
                    tbReportIDox.Text);
                    com.ExecuteNonQuery();
                    conn.Close();
                **/
            }
        }
    }

    class reportTableSQL 
    {
        public void reportTable()
        {
            string connectionString = connectionString = "Data Source=
            ..//..//maintenanceDB.sdf";
            SqlCeConnection conn = new SqlCeConnection(connectionString);
            using (SqlCeCommand com = new SqlCeCommand("INSERT INTO
            ReportForm VALUES(@reportID)", conn))
            {
                // if i call this method from class registerReports : Form
                // it doesn't recognise tbReportIDox.Text as
                //it isn't exist in this class
                /**
                    conn.Open();
                    com.Parameters.AddWithValue("@reportID",
                    tbReportIDox.Text);
                    com.ExecuteNonQuery();
                    conn.Close();
                **/
            }
        }
    }

thank you

您可以做的是将tbReportIDox.Text拉上来作为方法的参数,即

public void reportTable(string reportName)
{   
    ...
    com.Parameters.AddWithValue("@reportID",    reportName);
    ...
}

然后在一个按钮上点击

private void btnSave_Click(object sender, EventArgs e)
{
        // resolve instance of reportTableSQL class
        // for example through: new reportTableSQL();
        var reportGenerator = new reportTableSQL();
        reportGenerator.reportTable(tbReportIDox.Text);
}

相关内容

  • 没有找到相关文章

最新更新