一个简单的更新数据库问题,请教!急!

jasonf512 2008-07-08 03:25:32
我先从几个关联表中查找相关数据读取到gridview,并显示,按bottom1实现了,但是,我需要设计一个bottom2,按下bottom2,把gridview中的数据写到数据库的一个新表中,请问代码怎样下?
部分代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["schoolConnectionString"].ConnectionString;
string cmdtxt2 = "select * from Student where Class='" + DropDownList4.Text + "'order ";
SqlConnection Con = new SqlConnection(connStr);
Con.Open();
SqlDataAdapter Da = new SqlDataAdapter(cmdtxt2, Con);
DataSet ds = new DataSet();
Da.Fill(ds, "GridView1");
GridView1.DataSource = ds.Tables["GridView1"].DefaultView;
GridView1.DataBind();
Con.Close();

}

protected void Button2_Click1(object sender, EventArgs e)
{
//这里怎样写啊 ?
}
...全文
156 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
jasonf512 2008-07-15
  • 打赏
  • 举报
回复
我现在写成这样,还是提示插入失败,请问有谁会改一下呢?
protected void Button2_Click1(object sender, EventArgs e)
{

string connStr = ConfigurationManager.ConnectionStrings["schoolConnectionString"].ConnectionString;
string cmdtxt2 = "select Student.*,Class.* ,Course.* from Student INNER JOIN Class ON Student.Class_id=Class.Class_id INNER JOIN Course ON Course.zydm=Class.zydm where Student.Class_id='" + DropDownList4.Text + "'order by Course.term asc";
SqlConnection Con = new SqlConnection(connStr);
Con.Open();
SqlDataAdapter Da = new SqlDataAdapter(cmdtxt2, Con);
DataSet myds = new DataSet();
Da.Fill(myds, "GridView1");
GridView1.DataSource = myds.Tables["GridView1"].DefaultView;
GridView1.DataBind();


try
{

SqlCommand myComm;
foreach (DataTable table in myds.Tables)
foreach (DataRow row in table.Rows)
{
connStr = "Insert Into StudentGrade(Stu_name,Stu_id,nf) values('" + row[0].ToString() + "','" + row[1].ToString() + "','" + row[2].ToString() + "')";

myComm = new SqlCommand(connStr, Con);
int res = myComm.ExecuteNonQuery();
if (res > 0)
{
Label1.Text = "成绩批量更新成功";
}
else
{
Label1.Text = "更新失败";
}
}
}

}
jasonf512 2008-07-15
  • 打赏
  • 举报
回复
up
jasonf512 2008-07-15
  • 打赏
  • 举报
回复
有人会吗?
bj981 2008-07-09
  • 打赏
  • 举报
回复
是一个新的临时表?如果是一次性用的,可以按钮一可以实现了
select * into TempTable from Student where Class='" + DropDownList4.Text + "
如果不是,可以这样用:
《把gridview中的数据写到数据库的一个新表》
将dataset 改为类的公用对象,这样button2还可以继续用dataset,再遍历dataset不就行了吗?
changyuming 2008-07-09
  • 打赏
  • 举报
回复
拉了一句,呵呵,加上sqlSdr.Update(dataset);,这句是执行插入的,不好意思啊
jasonf512 2008-07-08
  • 打赏
  • 举报
回复
是 gridview中的数据,是有关
values('" + row[0].ToString() + "','" + row[1].ToString() + "'
这样的
jasonf512 2008-07-08
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 changyuming 的回复:]
protected void Button2_Click1(object sender, EventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["schoolConnectionString"].ConnectionString;
SqlConnection Con = new SqlConnection(connStr);
OleDbCommand sqlCom = Con.CreateCommand();
SqlDataAdapter sqlSdr = new SqlDataAdapter(sqlCom);
dataset.Clear();
sqlSdr.Fill(GridView1.Ro…
[/Quote]

不行阿!都没有写入数据库语句.......
jasonf512 2008-07-08
  • 打赏
  • 举报
回复
up
changyuming 2008-07-08
  • 打赏
  • 举报
回复
protected void Button2_Click1(object sender, EventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["schoolConnectionString"].ConnectionString;
SqlConnection Con = new SqlConnection(connStr);
OleDbCommand sqlCom = Con.CreateCommand();
SqlDataAdapter sqlSdr = new SqlDataAdapter(sqlCom);
dataset.Clear();
sqlSdr.Fill(GridView1.Rows);
}
试试,我现在试不了了,你要是提示出错就告诉我
jasonf512 2008-07-08
  • 打赏
  • 举报
回复
up
jasonf512 2008-07-08
  • 打赏
  • 举报
回复
我知道方法啊!!!!!我不会写
protected void Button2_Click1(object sender, EventArgs e)
{
//这里怎样写啊 ?
}
这里的代码啊 !有谁能帮下忙?
格拉 2008-07-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jimmy8021 的回复:]
循环遍历GridView,取出你想要更新的数据,然后一次更新到数据库中
[/Quote]管用!
pinhailinfeng 2008-07-08
  • 打赏
  • 举报
回复
用个循环,或遍历,逐行将GridView1中的值添加到新表中
jasonf512 2008-07-08
  • 打赏
  • 举报
回复
各位,方法我知道,我需要代码啊!!请各位吧代码给我好吗?我需要
protected void Button2_Click1(object sender, EventArgs e)
{
//这里怎样写啊 ?
}
jasonf512 2008-07-08
  • 打赏
  • 举报
回复
各位,方法我知道,我需要代码啊!!请各位吧代码给我好吗?我需要
protected void Button2_Click1(object sender, EventArgs e)
{
//这里怎样写啊 ?
}
changyuming 2008-07-08
  • 打赏
  • 举报
回复
你不是可以从gridview里获得dataset么,然后就在你的事件里调用这个方法啊,
用从gridview里获得的dataset做参数,明白了么?
Tracy5453 2008-07-08
  • 打赏
  • 举报
回复
用FOR循环,逐行将GridView1中的值添加到新表中。
hmilj245216 2008-07-08
  • 打赏
  • 举报
回复

SqlCommand cmd = New SqlCommand("insert ......", con)
cmd.ExecuteNonQuery()
jimmy8021 2008-07-08
  • 打赏
  • 举报
回复
循环遍历GridView,取出你想要更新的数据,然后一次更新到数据库中
jasonf512 2008-07-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 changyuming 的回复:]
/// <summary>
/// 用DataSet对象,更新数据库
/// </summary>
void IDbAccess.UpdateDataSet(DataSet dataset)
{
SqlDataAdapter sqlSdr = new SqlDataAdapter(sqlCom);
dataset.Clear();
sqlSdr.Fill(dataset);
}
试试看,不好用再告诉我。我这里好用
[/Quote]


代码放在什么位置啊?
加载更多回复(2)

110,019

社区成员

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

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

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