将数据行添加到c#&中的数据表中SQL [英] Adding datarow to the datatable in c# & sql

查看:61
本文介绍了将数据行添加到c#&中的数据表中SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿我在这段代码中有异常,是Sqlserver的新手,任何人都可以帮助我并解释下面的代码并纠正它并解释异常,谢谢...





SqlDataAdapter da = new SqlDataAdapter(select * from mytable,SqlConnection Object);

DataSet ds = new DataSet();

da.Fill(ds);



DataRow row1 = ds.Tables [0] .NewRow();



row1 [ID] = 3;

row1 [名称] =Saaaaadii;



ds.Tables [0] .Rows.Add(row1);



da.Update(ds);







异常发生在:da.Update(ds);



更新需要有效使用新行传递DataRow集合时的InsertCommand。

Hey I have Exception in this code , m new to Sqlserver , can any one help me and explain me the following code and correct it and explain the exception thanks ...


SqlDataAdapter da = new SqlDataAdapter("Select * from mytable", SqlConnection Object);
DataSet ds = new DataSet();
da.Fill(ds);

DataRow row1 = ds.Tables[0].NewRow();

row1["ID"] = 3;
row1["Name"] = "Saaaaadii";

ds.Tables[0].Rows.Add(row1);

da.Update(ds);



Exception occurrs at : da.Update(ds);

Update requires a valid InsertCommand when passed DataRow collection with new rows.

推荐答案

您好,



查看下面的链接.. 。



<啊ref =http://www.codeproject.com/Articles/3805/Inserting-relational-data-using-DataSet-andbecueAd>使用DataSet和DataAdapter插入关系数据 [ ^ ]



问候,

RK
Hi,

Check this link below...

Inserting relational data using DataSet and DataAdapter[^]

Regards,
RK


大家好

运行此代码



SqlDataAdapter da = new SqlDataAdapter(select * from item,con);

con.Open();

试试

{

DataSet ds = new DataSet();

da.Fill(ds);

SqlCommandBuilder cb = new SqlCommandBuilder(da);









DataRow row1 = ds.Tables [0] .NewRow();



row1 [0] = txtaccno.Text;

row1 [1] = txtname.Text;

row1 [2] = txtsalary.Text;

ds.Tables [0] .Rows.Add(row1);

da.Update(ds);

gvShow.DataSource = ds;

gvShow.DataBind();



问候,

Pawan
Hi All
Run this code

SqlDataAdapter da = new SqlDataAdapter("select * from item ", con);
con.Open();
try
{
DataSet ds = new DataSet();
da.Fill(ds);
SqlCommandBuilder cb = new SqlCommandBuilder(da);




DataRow row1 = ds.Tables[0].NewRow();

row1[0] = txtaccno.Text;
row1[1] = txtname.Text;
row1[2] = txtsalary.Text;
ds.Tables[0].Rows.Add(row1);
da.Update(ds);
gvShow.DataSource = ds;
gvShow.DataBind();

Regards,
Pawan


你需要做的事情很少

1)表 MYTable 是否有主键如果没有先创建它,

2)您需要使用 commandbuilder [ ^ ]



希望你得到它
Few thing you need to do
1) Is the table MYTable has a primary key if not first create it,
2) you need to use a commandbuilder [^]

hope you got it


这篇关于将数据行添加到c#&amp;中的数据表中SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆