C# - 尝试通过Form2上的Button插入SQL以更新Form1上的dataGridView

时间:2014-06-23 23:47:40

标签: c# database forms datagridview insert

我是论坛的新手,这是我的第一篇文章。我已经从阅读其他帖子(不同主题)中学到了一些东西。对于这个话题,我发现了一些很接近的话题,但我的实验并没有完全消失。这篇文章的标题说明了一切 - 我在Form1上的dataGridView填充OK,而我在Form2上的按钮插入一个新的数据库行工作正常。我需要Form1上的dataGridView在按下Form2上的“插入”按钮后自动填充...而不重新启动程序。我可以重新启动,新行就在那里。我知道我需要把两种形式联系在一起......只是不确定如何。我对插入部分有一些帮助,但对于像我这样的初学者来说,这有点复杂。这是一个小应用程序,我只需要工作,所以越简单越好。我会发布几个相当大的代码块(希望不会太大)。任何帮助表示赞赏!

//FORM 1 - PARTIAL CODE
private void Form1_Load(object sender, EventArgs e)
{
    // TODO: This line of code loads data into the 'DB_TEMPDataSet.Product' table. You can move, or remove it, as needed.
    Fillcombo();
    this.ProductTableAdapter.Fill(this.DB_TEMPDataSet.Product);
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    string strCmd = "Select * from Product where Mode='" + comboBox1.Text + "';";
    SqlConnection con = new SqlConnection(@"Data Source=myserver\dev1;Initial Catalog=DB_TEMP;Integrated Security=True");
    SqlCommand cmd = new SqlCommand(strCmd, con);
    try
    {
        con.Open();
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        dataGridView1.DataSource = dt;
        con.Close();
     }
     catch (Exception ex)
     {
        MessageBox.Show(ex.Message);
     }
}

Form2 secondForm = new Form2();

private void button1_Click(object sender, EventArgs e)
{
    secondForm.ShowDialog();
}

这是一个很好的表格2:

//FORM 2 - PARTIAL CODE:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();

        Fillcombo2("BranchId", "Product", comboBox1);
        Fillcombo2("Mode", "Product", comboBox2);
        Fillcombo2("ChargeCode", "Product", comboBox3);
        Fillcombo2("ProductCode", "Product", comboBox4);
        Fillcombo2("CustomerType", "Product", comboBox5);
        Fillcombo2("CreatedBy", "Product", comboBox6);
        Fillcombo2("CreatedOn", "Product", comboBox7);
        Fillcombo2("LastUpdatedBy", "Product", comboBox8);
        Fillcombo2("LastUpdatedTime", "Product", comboBox9);
    }

    void Fillcombo2(string column, string table, ComboBox box)
    {
        string strCmd2 = "Select DISTINCT " + column + " from " + table + ";";
        SqlConnection con2 = new SqlConnection(@"Data Source=myserver\dev1;Initial Catalog=DB_TEMP;Integrated Security=True");
        SqlCommand cmd2 = new SqlCommand(strCmd2, con2);
        SqlDataReader myReader2;

        try
        {
            con2.Open();
            myReader2 = cmd2.ExecuteReader();

            while (myReader2.Read())
            {
                object sName2 = myReader2[column];
                box.Items.Add(sName2);
            }
            con2.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.OK;

        string strCmd3 = @"
        INSERT INTO dbo.Product (BranchId, Mode, ChargeCode, ProductCode, CustomerType, CreatedBy, CreatedOn, LastUpdatedBy, LastUpdatedTime)
        VALUES(@BranchId, @Mode, @ChargeCode, @ProductCode, @CustomerType, @CreatedBy, @CreatedOn, @LastUpdatedBy, @LastUpdatedTime)
        ";

        using (SqlConnection con3 = new SqlConnection(@"Data Source=myserver\dev1;Initial Catalog=DB_TEMP;Integrated Security=True"))
        {
            try
            {
                con3.Open();
                using (SqlCommand cmd3 = con3.CreateCommand())
                {
                    cmd3.CommandText = strCmd3;
                    AddParameterWithValue(cmd3, "@BranchId", comboBox1.Text);
                    AddParameterWithValue(cmd3, "@Mode", comboBox2.Text);
                    AddParameterWithValue(cmd3, "@ChargeCode", comboBox3.Text);
                    AddParameterWithValue(cmd3, "@ProductCode", comboBox4.Text);
                    AddParameterWithValue(cmd3, "@CustomerType", comboBox5.Text);
                    AddParameterWithValue(cmd3, "@CreatedBy", comboBox6.Text);
                    AddParameterWithValue(cmd3, "@CreatedOn", comboBox7.Text);
                    AddParameterWithValue(cmd3, "@LastUpdatedBy", comboBox8.Text);
                    AddParameterWithValue(cmd3, "@LastUpdatedTime", comboBox9.Text);
                    cmd3.ExecuteNonQuery();
                }
                con3.Close();
                MessageBox.Show("Database row INSERT successful!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我将创建一个类来处理所有Database相关操作,这样,它可以保存DataAdapter插入/更新/删除记录

您可以在MSDN How to bind Data to DataGridViewHow to update by using SqlDataAdapter中找到该示例。 此外,您可能希望将SqlParameter用于任何sql命令,它可以保护您的应用程序以进行SQL注入攻击,例如:SqlParameter