如何在sql数据库中保存datagridview多行 [英] how to save datagridview multiple row in sql database

查看:119
本文介绍了如何在sql数据库中保存datagridview多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个datagridview和一些文本框。当我点击添加按钮然后所有值显示在datagridview中我现在要保存datagridview中的所有行。



但它保存sql中的所有行很多最后一次添加在gridview中的时间不是不同的行保存在具有不同值的数据库中。



我在谷歌搜索但没有找到任何特定的ans。这解决了我的问题。请告诉我如何在sql中保存所有行



我的代码是:





Hi,

I have a datagridview and some textboxes. when I am click on the add button then all values show in datagridview now I want to save all the row in datagridview.

But its save all the rows in sql many times which is lasttime add in gridview not different rows save in database which is have different values.

I am searching in google but don't find any specific ans. which solve my problem. please tell me how to save all the rows in sql

My code is:


private void btnPrint_Click(object sender, EventArgs e)
        {
            foreach (DataRow dr in objDT.Rows)
                        {
                            Add_PO_Details();
                        }
        }

public void Add_PO_Details()        //Purchase Invoice Details
        {
            try
            {
                string query = "Insert into Entry_D(txtInvNo,txtPG," +
                "txtPS,cmbIType,txtItemCode,txtItem) values('"

                + txtInvNo.Text + "','" + txtPG.Text +
                "','" + txtPS.Text + "','" + this.cmbIType.Text +
                "','" + txtItemCode.Text + "','" + txtItem.Text + "')";

                SqlConnection conn = new SqlConnection(connstr);
                conn.Open();
                SqlCommand cmd = new SqlCommand(query, conn);
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                conn.Close();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

推荐答案

您正在单步执行datagridview,但在SQL中您只使用文本框...所以有一个步骤缺失...你需要从datagridview填充文本框或直接使用datagridview值。



例如:



You're stepping through your datagridview, but in the SQL you are only using the textboxes ... So there is a step missing ... you need to either populate the text boxes from your datagridview or use the datagridview values directly.

For example:

private void button1_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow dr in objDT.Rows)
    {
        txtInvNo.Text = dr.Cells[0].Value.ToString();
        txtPG.Text = dr.Cells[1].Value.ToString();
        // etc...
        Add_PO_Details();
    }
}



或更改例程的签名以接受DataGridViewRow作为输入参数...并执行类似...的操作


or change the signature of your routine to accept a DataGridViewRow as an input parameter ... and do something like ...

public void Add_PO_Details(DataGridViewRow dr)        //Purchase Invoice Details
{
    try
    {
        string query = "Insert into Entry_D(txtInvNo,txtPG," +
        "txtPS,cmbIType,txtItemCode,txtItem) values('";

        query += dr.Cells[0].Value.ToString() + "','";
        query += dr.Cells[1].Value.ToString() + "','";
        //etc


这篇关于如何在sql数据库中保存datagridview多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆