c#datagridview的数据存到数据库,但是dt没数据

图片说明

             private void button4_Click(object sender, EventArgs e)
    {


        try
        {
            SqlConnection conn = new SqlConnection("Server=.;Database=gaunli;Trusted_Connection=SSPI");
            conn.Open();
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {    
                string xm = dataGridView1.Rows[i].Cells["id"].EditedFormattedValue.ToString();
                string id = dataGridView1.Rows[i].Cells["name"].EditedFormattedValue.ToString();
                string name = dataGridView1.Rows[i].Cells["age"].EditedFormattedValue.ToString();
                string age = dataGridView1.Rows[i].Cells["address"].EditedFormattedValue.ToString();
                string address = dataGridView1.Rows[i].Cells["phone"].EditedFormattedValue.ToString();
                string str = "update Guanliyuan set name='" + id + "',age='" + name + "',address='" + age + "',phone='"+address+"' where id='" + xm + "'";

                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter { SelectCommand = new SqlCommand(str, conn) };
                da.Fill(dt);
                this.dataGridView1.DataSource = dt;
                SqlCommandBuilder scb = new SqlCommandBuilder(da);
                da.Update(dt);
            }            
            conn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

    }

2个回答

SqlDataAdapter da = new SqlDataAdapter { SelectCommand = new SqlCommand(str, conn) };
你的str是之前的update,所以没有结果
你可以再写一条
string str1 = "select * from Guanliyuan";
SqlDataAdapter da = new SqlDataAdapter { SelectCommand = new SqlCommand(str1, conn) };

qq_43356549
qq_43356549 是我sql语句写错了吗
12 个月之前 回复
qq_43356549
qq_43356549 我加了之后修改的数据还是没有更新进去。
12 个月之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复qq_43356549: 那当然,你update没有执行,在前面加上SqlCommand cmd = new SqlCommand(str, conn); cmd.ExecuteNoQuary();
12 个月之前 回复
qq_43356549
qq_43356549 改了代码但是查询到的是没更改的数据表
12 个月之前 回复

try
{
SqlConnection conn = new SqlConnection("Server=.;Database=gaunli;Trusted_Connection=SSPI");
conn.Open();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{

string xm = dataGridView1.Rows[i].Cells["id"].EditedFormattedValue.ToString();
string id = dataGridView1.Rows[i].Cells["name"].EditedFormattedValue.ToString();
string name = dataGridView1.Rows[i].Cells["age"].EditedFormattedValue.ToString();
string age = dataGridView1.Rows[i].Cells["address"].EditedFormattedValue.ToString();
string address = dataGridView1.Rows[i].Cells["phone"].EditedFormattedValue.ToString();
string str = "update Guanliyuan set name='" + id + "',age='" + name + "',address='" + age + "',phone='"+address+"' where id='" + xm + "'";
string str = "select * from Guanliyuan where id='" + xm + "'";

            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter { SelectCommand = new SqlCommand(str, conn) };
            da.Fill(dt);
            dt.Rows[0]["name"] = id;
                            dt.Rows[0]["age"] = name;
                            ....
            da.Update(dt);
        }            
        conn.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }

   需要首先查询出记录,然后才能更新。
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!