就是往一个名为“IcManager”的表中写入TEXTBOX中的数据,点添加按钮来完成;
附代码如下,但就是写不到数据库中,望指点迷津!
protected void Button_tianjia_Click(object sender, EventArgs e)
{
SqlConnection icconn = new SqlConnection("Data Source=GATE\\SQLEXPRESS;Initial
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM [IcManager]", icconn);
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
sda.Fill(ds, "IcManager"); //已经把IcManager表读到了 dataset中。 TextBox_part.Text = ds.Tables[0].Rows[0]["part"].ToString();
TextBox_phone.Text = ds.Tables[0].Rows[0]["phone"].ToString();
TextBox_price.Text = ds.Tables[0].Rows[0]["price"].ToString();
TextBox_packaging.Text = ds.Tables[0].Rows[0]["packaging"].ToString();
TextBox_number.Text = ds.Tables[0].Rows[0]["number"].ToString();
TextBox_manufacturer.Text = ds.Tables[0].Rows[0]["manufacturer"].ToString();
TextBox_id.Text = ds.Tables[0].Rows[0]["id"].ToString();
TextBox_date.Text = ds.Tables[0].Rows[0]["date"].ToString();
TextBox_company.Text = ds.Tables[0].Rows[0]["company"].ToString(); sda.Update(ds, "IcManager");
}
附代码如下,但就是写不到数据库中,望指点迷津!
protected void Button_tianjia_Click(object sender, EventArgs e)
{
SqlConnection icconn = new SqlConnection("Data Source=GATE\\SQLEXPRESS;Initial
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM [IcManager]", icconn);
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
sda.Fill(ds, "IcManager"); //已经把IcManager表读到了 dataset中。 TextBox_part.Text = ds.Tables[0].Rows[0]["part"].ToString();
TextBox_phone.Text = ds.Tables[0].Rows[0]["phone"].ToString();
TextBox_price.Text = ds.Tables[0].Rows[0]["price"].ToString();
TextBox_packaging.Text = ds.Tables[0].Rows[0]["packaging"].ToString();
TextBox_number.Text = ds.Tables[0].Rows[0]["number"].ToString();
TextBox_manufacturer.Text = ds.Tables[0].Rows[0]["manufacturer"].ToString();
TextBox_id.Text = ds.Tables[0].Rows[0]["id"].ToString();
TextBox_date.Text = ds.Tables[0].Rows[0]["date"].ToString();
TextBox_company.Text = ds.Tables[0].Rows[0]["company"].ToString(); sda.Update(ds, "IcManager");
}
{
SqlConnection icconn = new SqlConnection("你的连接字符串");
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM [IcManager]", icconn);
SqlCommandBuilder scb = new SqlCommandBuilder(sda);//自动生成sqlcommand命令 DataSet ds = new DataSet(); sda.Fill(ds, "IcManager"); //已经把IcManager表读到了 dataset中。 TextBox_part.Text = ds.Tables[0].Rows[0]["part"].ToString();
TextBox_phone.Text = ds.Tables[0].Rows[0]["phone"].ToString();
TextBox_price.Text = ds.Tables[0].Rows[0]["price"].ToString();
TextBox_packaging.Text = ds.Tables[0].Rows[0]["packaging"].ToString();
TextBox_number.Text = ds.Tables[0].Rows[0]["number"].ToString();
TextBox_manufacturer.Text = ds.Tables[0].Rows[0]["manufacturer"].ToString();
TextBox_id.Text = ds.Tables[0].Rows[0]["id"].ToString();
TextBox_date.Text = ds.Tables[0].Rows[0]["date"].ToString();
TextBox_company.Text = ds.Tables[0].Rows[0]["company"].ToString(); sda.Update(ds, "IcManager");
}
{
SqlConnection icconn = new SqlConnection("你的连接字符串");
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM [IcManager]", icconn);
SqlCommandBuilder scb = new SqlCommandBuilder(sda);//自动生成sqlcommand命令
DataSet ds = new DataSet(); sda.Fill(ds, "IcManager"); //已经把IcManager表读到了 dataset中。 ds.Tables[0].Rows[0]["part"] = TextBox_part.Text; //用文本框的值给DataSet里的值赋值
//... sda.Update(ds, "IcManager");
}
做修改用2楼代码,做新增要生成一个DataRow,赋值后加到datatable,然后update
这里虽然 执行了更新方法
可以 LZ 把 InsertCommand UpdateCommand DeleteCommand 都准备好吗?我是小菜鸟! 说的不好清见谅, 希望能够帮到你!
protected void Button_tianjia_Click(object sender, EventArgs e)
{
SqlConnection icconn = new SqlConnection("Data Source=GATE\\SQLEXPRESS;Initial
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM [IcManager]", icconn);
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
sda.Fill(ds, "IcManager"); //已经把IcManager表读到了 dataset中。
ds.AcceptChanges(); ds.Tables[0].Rows[0]["part"]=TextBox_part.Text;
ds.Tables[0].Rows[0]["phone"]=TextBox_phone.Text ;
ds.Tables[0].Rows[0]["price"]=TextBox_price.Text ;
其余的代码相应做调整 SqlCommandBuilder scb = new SqlCommandBuilder(sda);//自动生成sqlcommand命令
sda.Update(ds, "IcManager");
}
ds.Tables[0].Rows[0]["part"]=TextBox_part.Text;
这句前要添加如下代码:
DataRow dr=ds.Tables[0].NewRow();
ds.Tables[0].Rows.Add(dr);
请问这句的意义是什么,sql语句不是已经在adapter实例化时就有了吗,我不明白,希望指点一下。