日期:2014-05-17  浏览次数:20812 次

现在可以肯定是inser这条语句有问题,但是实在不知道怎么改了,求指导
现在准备把datagridview中的信息,一条条的传到数据库的GoodsOrder中去,然后现在的代码是这样的:
for(int i=1;i< dataGridView1.Rows.Count;i++)
            {
                
                string a=this.dataGridView1.Rows[i].Cells[0].ToString();
                string b=this.dataGridView1.Rows[i].Cells[1].ToString();
                string c=this.dataGridView1.Rows[i].Cells[2].ToString();
                string d=this.dataGridView1.Rows[i].Cells[3].ToString();
                string g=this.dataGridView1.Rows[i].Cells[4].ToString();
                //SqlConnection connection = cont.conn();
                SqlCommand com1 = new SqlCommand(" (select * from GoodsInfo where ID= '" + ID + "')(insert into GoodsOrder(ID,GoodsName,UnitPrice,GoodsAmount,Money)) values('"+a+"','"+b+"','"+c+"','"+d+"','"+g+"')",connection);
                //connection.Open();
                //SqlDataReader dr = com1.ExecuteReader();
                //dr.Read();
                //connection.Close();
            }

加断点调试都试过了,然后现在可以肯定的是问题出在SqlCommand中的语句上面,但是不知道该怎么改了。。

------解决方案--------------------
 string sql ="insert into GoodsOrder (ID,GoodsName,UnitPrice,GoodsAmount,Money) values('" + a + "','" + b + "','" + c + "','" + d + "','" + g + "')";
SqlCommand com1 = new SqlCommandconnection(sql,connection);
这么写,代码好读

另外 你前面加select是干啥呢???
------解决方案--------------------
楼主 你代码太乱了 教你个用个清晰的 
string.Format(""insert into GoodsOrder (ID,GoodsName,UnitPrice,GoodsAmount,Money) values('{0}','{1}','{2}','{3}','{4}')",a,b,c,d,g);

C#的占位符要会啊
增删改的执行语句是
 SqlCommand com1 = new SqlCommand (sql, connection);
 com1.ExecuteNonQuery();

插入的数据不对是你获得的值不对
正确获得只是下面的方法
dataGridView1.SelectedRows[0].Cells[1].Value.ToString();

楼主多找点例题来做 你错的多了
------解决方案--------------------
hello 哈哈