[请教]更新数据库

mndn_nana 2007-04-18 10:04:32
public Form1()
{
InitializeComponent();
SqlConnection con = new SqlConnection("server=.;database=test;uid=sa;pwd=");
con.Open();
da = new SqlDataAdapter("select * from tb_EmployeeInfo", con);
ds = new DataSet();
da.Fill(ds, "tb_EmployeeInfo");
this.gridControl1.DataSource = ds.Tables["tb_EmployeeInfo"];
}
private void button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("insert into tb_EmployeeInfo values('hoho',default,25)");
da.InsertCommand = cmd;
da.Update(ds.Tables["tb_EmployeeInfo"]);
}
点击button1,插入一条语句,没有报错,可是没有更新成功,请问问题在哪里?
...全文
196 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
mndn_nana 2007-04-19
  • 打赏
  • 举报
回复
感谢likevs() 插入成功
不过还想问的是,SqlCommandBuilder commandbuilder1=new SqlCommandBuilder(this.da);//让数据集自动生成SQL语句 它和手动写的InsertCommand语句效果一样?如果是更新和删除也是这样吗?

比如:SqlCommand cmd=new SqlCommand("insert into tb_EmployeeInfo values(……)");
da.InsertCommand=cmd;
效果可以一样吗?我试过了,其中SqlCommand cmd=new SqlCommand("insert into tb_EmployeeInfo values(……)");很难写
我写的是:DataRow newrow=this.ds.Tables["tb_EmployeeInfo"].NewRow();
newrow["EmployeeInfo_Name"] = "nana";
newrow["EmployeeInfo_Gendar"] = 0;
newrow["EmployeeInfo_Age"] = 25;
string dc1=newrow["EmployeeInfo_Name"].ToString();
Byte dc2 = Convert.ToSByte(newrow["EmployeeInfo_Gendar"]);
int dc3 = Convert.ToUInt32(newrow["EmployeeInfo_Age"]);
this.ds.Tables["tb_EmployeeInfo"].Rows.Add(newrow);
SqlCommand cmd = new SqlCommand("insert into tb_EmployeeInfo values(dc1,dc2,dc3)");
我的EmployeeInfo_Name是varchar(20),EmployeeInfo_Gendar是bit,EmployeeInfo_Age是int
类型怎样转换?请各位赐教

另外gjf2007()朋友,this.gridControl1.DataBind();//一定要使用这句,才可使数据库中信息与本地DATASET中数据同步,并显示出来。 这句的.DataBind()方法我没有找到,是我.net的问题吗?请详细赐教,感谢了!

由于我是刚注册不久,第一次就给了100分,答帖子也不多,所以连一份也没有了,我会努力赚分的,忘广大朋友多多帮助,再次感谢!


gjf2007 2007-04-19
  • 打赏
  • 举报
回复
兄弟,你可以试一下这一些代码。
SqlConnection con = new SqlConnection("server=.;database=test;uid=sa;pwd=");
con.Open();
SqlDateAdapter da = new SqlDataAdapter("select * from tb_EmployeeInfo", con);
DataSet ds = new DataSet();
da.Fill(ds, "tb_EmployeeInfo");
this.gridControl1.DataSource = ds.Tables["tb_EmployeeInfo"];
this.gridControl1.DataBind();//一定要使用这句,才可使数据库中信息与本地DATASET中数据同步,并显示出来。

}
private void button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("insert into tb_EmployeeInfo values('hoho',default,25)");
int count;
count=Int32.Parse( cmd.ExecuteScalar());
if(count>0)
{Response.Write("记录插入成功");
}
else
{
Response.Write("记录插入不成功");
}
likevs 2007-04-19
  • 打赏
  • 举报
回复
又错啦,晕
this.ds.Tables["tb_EmployeeInfo"].Rows.Add(newrow);

likevs 2007-04-19
  • 打赏
  • 举报
回复
this.ds.Tables["tb_EmployeeInfo"].Add(newrow);

记得要用这个向数据集加入新记录,哈,昨晚上忘了这句,然后才更新到数据源.


然后 SqlCommandBuilder commandbuilder1=new SqlCommandBuilder(this.da);//让数据集自动生成SQL语句
this.sqlDataAdapter1.Update(this.ds,"tb_EmployeeInfo");
farsear 2007-04-18
  • 打赏
  • 举报
回复
或者直接cmd.excutequery()就可以了
likevs 2007-04-18
  • 打赏
  • 举报
回复
楼主,建议你先在数据集里的表里操作,再更新到数据源吧.

先DataRow newrow=this.ds.Tables["tb_EmployeeInfo"].NewRow();//生成新行

然后再对新行的列值赋值,如:
newrow["??"]=??;
newrow["??"]=??;
newrow["??"]=??;

然后 SqlCommandBuilder commandbuilder1=new SqlCommandBuilder(this.da);//让数据集自动生成SQL语句
this.sqlDataAdapter1.Update(this.ds,"tb_EmployeeInfo");



楼主怎么问问题不给分的吗?

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧