现在准备把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中的语句上面,但是不知道该怎么改了。。
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中的语句上面,但是不知道该怎么改了。。
SqlCommand com1 = new SqlCommandconnection(sql,connection);
这么写,代码好读另外 你前面加select是干啥呢???
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();
string sql = "insert into GoodsOrder (ID,GoodsName,UnitPrice,GoodsAmount,Money) values('" + a + "','" + b + "','" + c + "','" + d + "','" + g + "')";
SqlCommand com1 = new SqlCommand (sql, connection);
connection.Open();
SqlDataReader dr = com1.ExecuteReader();
dr.Read();
connection.Close();
}
然后存进去了,不过结果是这个样子,好像不太对诶
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();楼主多找点例题来做 你错的多了
这么循环,是不是会少那么一条数据的?还是我想多了?
这个我确实做得蛮少的
目前菜鸟一只。。不过话说,我刚刚把datagridView1的获得值的那几句按照你说的那样修改了,那几条都按照string a = dataGridView1.SelectedRows[i]Cells[0].Value.ToString();的格式修改了,然后把断点设置在了string b那句,但是刚刚显示出现了这样的状况。
Cells[0] 代表列的位置 从0开始上面是我打错了