繁体   English   中英

从 c# 中的 datagridview 更新 sql 数据库

[英]Update sql database from datagridview in c#

我是新来的,但我需要一些帮助。 我需要使用 Windows Z6450242531912981C368Z,3CAE'8 从 C# 更新 SQL 服务器数据库。 我查了一下,但仍然找不到正确的答案。 我需要通过按下按钮从 datagridview 更改或填充数据库来进行插入和更新。 我为两者创建了一个单独的 function 我正在使用此代码;

private void InsertPositionen()
{
    string qry = ""; 

    SqlCommand insert = new SqlCommand(qry, con);

    try
    {
        for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
        {
            qry = "INSERT INTO BelegePositionen (BelID, BelPosId, Artikelnummer, Menge, Preis) VALUES( " + dataGridView1.Rows[i].Cells["BelID"] + ", " 
                   + dataGridView1.Rows[i].Cells["BelPosId"] + ", " 
                   + dataGridView1.Rows[i].Cells["Artikelnummer"] + ", " 
                   + dataGridView1.Rows[i].Cells["Menge"] + ", " 
                   + dataGridView1.Rows[i].Cells["Preis"];
        }

        insert.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void UpdatePositionen()
{
        string updt = "";

        SqlCommand update = new SqlCommand(updt, con);

        try
        {
            for (int i = 0; i < dataGridView1.Rows.Count -1; i++)
            {
                updt = "UPDATE BelegePositionen SET BelID =  "
                    + dataGridView1.Rows[i].Cells["BelID"] +
                    ", BelPosID = "
                    + dataGridView1.Rows[i].Cells["BelPosID"] +
                    ", Atrikelnummer = "
                    + dataGridView1.Rows[i].Cells["Artikelnummer"] +
                    ", Menge = "
                    + dataGridView1.Rows[i].Cells["Menge"] +
                    ", Preis = " 
                    + dataGridView1.Rows[i].Cells["Preis"];
            }

            update.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Done!");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
}

真的应该做你的 SQL 这样的事情! 这使您的代码对 SQL 注入漏洞敞开大门! 停止那个 - 现在!

相反 - 使用参数化查询- 像这样:

private void InsertPositionen()
{
    string qry = "INSERT INTO BelegePositionen (BelID, BelPosId, Artikelnummer, Menge, Preis) " +  
                 "VALUES(@BelId, @BelPosId, @ArtNr, @Menge, @Preis);";
 
    SqlCommand insert = new SqlCommand(qry, con);
    
    // define the parameters
    insert.Parameters.Add("@BelId", SqlDbType.Int);
    insert.Parameters.Add("@BelPosId", SqlDbType.Int);
    insert.Parameters.Add("@ArtNr", SqlDbType.Int);  // maybe this is a string? 
    insert.Parameters.Add("@Menge", SqlDbType.Int);
    insert.Parameters.Add("@Preis", SqlDbType.Decimal, 20, 4);

    try
    {
        // in the loop, only *set* the parameter's values
        
        for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
        {
            insert.Parameters["@BelId"].Value = 1;
            insert.Parameters["@BelPosId"].Value = 2;
            insert.Parameters["@ArtNr"].Value = 3;
            insert.Parameters["@Menge"].Value = 4;
            insert.Parameters["@Preis"].Value = 99.95;

            insert.ExecuteNonQuery();
        }   
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

你的问题很模糊,因为你 state 你有问题,但不太确定你有什么问题。 如果您能描述您遇到的问题,将会有所帮助。

除了@marc_c 所说的关于 sql 注入之外,我看不到您如何管理与数据库的连接。

从代码看来,您可能会遇到打开连接字符串或根本不打开它们的情况。

使用using(...) { }将在完成后关闭连接。

private void InsertPositionen()
{
    //using the using statement you will insure that the connection is closed and resources released
    using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.db))
    {
        string cmd = "INSERT INTO BelegePositionen (BelID, BelPosId, Artikelnummer, Menge, Preis) " +
         "VALUES(@BelId, @BelPosId, @ArtNr, @Menge, @Preis);";

        //using the using statement will ensure any reasources are released when exiting the code block
        using (SqlCommand insert = new SqlCommand(cmd, connection))
        {
            // define the parameters
            insert.Parameters.Add("@BelId", SqlDbType.Int);
            insert.Parameters.Add("@BelPosId", SqlDbType.Int);
            insert.Parameters.Add("@ArtNr", SqlDbType.Int);  // maybe this is a string? 
            insert.Parameters.Add("@Menge", SqlDbType.Int);
            insert.Parameters.Add("@Preis", SqlDbType.Decimal, 20, "4");

            try
            {
                //open the connection
                insert.Connection.Open();

                // in the loop, only *set* the parameter's values
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    insert.Parameters["@BelId"].Value = dataGridView1.Rows[i].Cells["BelID"];
                    insert.Parameters["@BelPosId"].Value = dataGridView1.Rows[i].Cells["BelPosId"];
                    insert.Parameters["@ArtNr"].Value = dataGridView1.Rows[i].Cells["Artikelnummer"];
                    insert.Parameters["@Menge"].Value = dataGridView1.Rows[i].Cells["Menge"];
                    insert.Parameters["@Preis"].Value = dataGridView1.Rows[i].Cells["Preis"];

                    insert.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                MessageBox.Show("Done!");
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM