1

我正在研究 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();
                **/
            }
        }
    }

谢谢你

4

1 回答 1

0

您可以做的是将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);
}
于 2014-03-18T13:21:17.567 回答