使用datatable更新数据库

使用datatable更新数据库

本文介绍了使用datatable更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚接触.Net这是我的朋友使用c#更新sql数据库中的表所使用的代码:asp.net代码:

im new to .Net this is the code which my friend gave for updating a table in sql database using c#,asp.net code:

{
  string dpt = (string)Session["deptmnt"];
  string con = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
                SqlConnection con1 = new SqlConnection(con);
                con1.Open();
 string qry = @"select * from faculty where ((fid like '%" + dpt + "%') and (fid='"+Label17.Text+"'))";
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = new SqlCommand(qry, con1);

                    string upd = @"update faculty SET fname = '" + TextBox7.Text + "',gender='" + DropDownList10.SelectedValue + "', dept = '" + DropDownList5.SelectedValue + "', maxbooks = '" + DropDownList8.SelectedValue + "' WHERE fid = '" + Label17.Text + "'";
                                      SqlCommandBuilder cb = new SqlCommandBuilder(da);
                    DataSet ds = new DataSet();
                    da.Fill(ds, "faculty");


                    DataTable dt = ds.Tables["faculty"];
                    dt.Rows[1]["fname"] = TextBox7.Text;
                    dt.Rows[1]["gender"] = DropDownList10.SelectedValue;
                    dt.Rows[1]["dept"] = DropDownList5.SelectedValue;
                    dt.Rows[1]["maxbooks"] = DropDownList8.SelectedValue;

                    SqlCommand cmd = new SqlCommand(upd, con1);
                    da.UpdateCommand = cmd;

                    da.Update(ds, "faculty");

                    con1.Close();
}



当我调试代码时,控件不会跟在dt.Rows [1] [fname] = TextBox7之后。文字;

为什么会这样?我无法找到原因。 dt.Rows [number] 代表什么?它是否表示行号或它是什么? plz help


when i debug the code,the control does not go after " dt.Rows[1]["fname"] = TextBox7.Text;"
why is it so? Im unable to find the reason.,what does dt.Rows[number] stand for? does it indicate the row number or what it is? plz help

推荐答案

dt.Rows[0]["fname"] = TextBox7.Text;
dt.Rows[0]["gender"] = DropDownList10.SelectedValue;
dt.Rows[0]["dept"] = DropDownList5.SelectedValue;
dt.Rows[0]["maxbooks"] = DropDownList8.SelectedValue;


这篇关于使用datatable更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 03:00