日期:2014-05-18  浏览次数:21202 次

winform datagridview 一条数据插入多张表
datagirdview 结构如下:
单据编号 货料编号 货料名称 单位 数量 单价
1 1 1 1 1 1
1 2 2 2 2 2
1 3 3 3 3 3

现在要将 单据编号 货料编号 数量 单价 存入表 MIS_InsertInfo(只有4个字段)

将 单据编号 存入表 MIS_MIS_InsertSingle中,该怎么存呢?



------解决方案--------------------
在sql多方便的,
select 单据编号,货料编号,数量,单价,存入表 into MIS_InsertInfo from table
select 单据编号 into MIS_MIS_InsertSingle from table
------解决方案--------------------
C# code

        //数据库操作
        public int SqlUpdate(string sql)
        {
            try
            {
                //conn数据库连接自己定义
                cmd = new SqlCommand(sql,conn);
                conn.Open();
                int line = cmd.ExecuteNonQuery();
                conn.Close();
                return line;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

//datagridview会有一空行,所以-1
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                        //定义j,想取第几列自己设置
                        int j = 0;
                        string a= dataGridView1.Rows[i].Cells[j + 1].Value.ToString();
                        string b= dataGridView1.Rows[i].Cells[j + 2].Value.ToString();
                        string b= dataGridView1.Rows[i].Cells[j + 3].Value.ToString();
                        string d= dataGridView1.Rows[i].Cells[j + 4].Value.ToString();
                        string sql = @"insert into  表名('单据编号','货料编号','数量','单价','存入表');
                        values('" + a+ "','" + b+ "','" + c+ "','" + d+ "' )";
                        //调用方法
                        SqlUpdate(sql);
                    }