如何循环取出GridView里的绑定的数据,然后通过一个按钮将其插入到数据中,本人新手急求!

安小武 2013-10-16 09:55:29




我想用一个button事件,点击导入,可是呢个插入怎么写,是不是的先从girdview里取出值来、具体怎么写
...全文
465 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq136097742 2013-10-17
  • 打赏
  • 举报
回复
for(int i =0;i<Gridview.Rows.Count;i++) { String insert=" insert into 表 (XXX,XXX,XXX...) values( "'"+Gridview.Rows[i].Cell[0].text"'", "'"+Gridview.Rows[i].Cell[1].text"'", "'"+Gridview.Rows[i].Cell[2].text"'" .....)" 然后执行. }
安小武 2013-10-16
  • 打赏
  • 举报
回复
引用 21 楼 yx137719427 的回复:
for(int i=0;i<ds.Tables[0].Rows.Count;i++) { SqlCommand sqlcommandinsert= new SqlCommand(); sqlcommandinsert.CommandText("insert into t(epid) values(@epid)"); sqlcommandinsert.Parameters.Add("@epid", SqlDbType.VarChar, 10); sqlcommandinsert.Parameters["@epid"].Value = ds.Tables[0].Rows[i]["epid"].ToString(); } 大致就这样。
你的意思是,剩下的字段,就这么写呗
men52676521 2013-10-16
  • 打赏
  • 举报
回复
你想要的数据都有了,那就简单的循环插入呗!
Dotar 2013-10-16
  • 打赏
  • 举报
回复
for (int i = 0; i < GridView1.Rows.Count; i++) { StringBuilder query = new StringBuilder(); GridViewRow row = GridView1.Rows[i]; string empID = ((TextBox)row.Cells[0].FindControl("txtID")).Text.Replace("'", ""); string empRealName = ((TextBox)row.Cells[0].FindControl("txtRealName")).Text.Replace("'", ""); string empSex = ((DropDownList)row.Cells[0].FindControl("ddlSex")).SelectedValue; string empAddress = ((TextBox)row.Cells[0].FindControl("txtAddress")).Text.Replace("'", ""); query.Append("update Employee set EmpID='" + empID + "',EmpRealName='" + empRealName + "',EmpSex='" + empSex + "',EmpAddress='" + empAddress + "' where ID=" + GridView1.DataKeys[i].Value + ""); Common.ExecuteSql(query.ToString()); } 参考
biind 2013-10-16
  • 打赏
  • 举报
回复
搜索:SqlBulkCopy ,参数是dataread,直接writetoserver的方法,很好用。
熙风 2013-10-16
  • 打赏
  • 举报
回复
引用 20 楼 u010686872 的回复:
[quote=引用 18 楼 happy09li 的回复:] [quote=引用 17 楼 u010686872 的回复:] [quote=引用 16 楼 happy09li 的回复:] 写法没错,但是你sql语句有问题,把里面@什么的换成对应ds里面的字段
我试试,你一直都在吧。。。不行。手机加我QQ也行。。695422118[/quote] 手机QQ我从来都不上[/quote] 微信呢[/quote] 骚年,不要太依赖手机,手机就是用来打电话,发短信的,偶尔玩玩游戏,看看网页。
yx137719427 2013-10-16
  • 打赏
  • 举报
回复
for(int i=0;i<ds.Tables[0].Rows.Count;i++) { SqlCommand sqlcommandinsert= new SqlCommand(); sqlcommandinsert.CommandText("insert into t(epid) values(@epid)"); sqlcommandinsert.Parameters.Add("@epid", SqlDbType.VarChar, 10); sqlcommandinsert.Parameters["@epid"].Value = ds.Tables[0].Rows[i]["epid"].ToString(); } 大致就这样。
安小武 2013-10-16
  • 打赏
  • 举报
回复
引用 18 楼 happy09li 的回复:
[quote=引用 17 楼 u010686872 的回复:] [quote=引用 16 楼 happy09li 的回复:] 写法没错,但是你sql语句有问题,把里面@什么的换成对应ds里面的字段
我试试,你一直都在吧。。。不行。手机加我QQ也行。。695422118[/quote] 手机QQ我从来都不上[/quote] 微信呢
熙风 2013-10-16
  • 打赏
  • 举报
回复
你再别截图,把代码贴出来就行了
熙风 2013-10-16
  • 打赏
  • 举报
回复
引用 17 楼 u010686872 的回复:
[quote=引用 16 楼 happy09li 的回复:] 写法没错,但是你sql语句有问题,把里面@什么的换成对应ds里面的字段
我试试,你一直都在吧。。。不行。手机加我QQ也行。。695422118[/quote] 手机QQ我从来都不上
安小武 2013-10-16
  • 打赏
  • 举报
回复
引用 16 楼 happy09li 的回复:
写法没错,但是你sql语句有问题,把里面@什么的换成对应ds里面的字段
我试试,你一直都在吧。。。不行。手机加我QQ也行。。695422118
熙风 2013-10-16
  • 打赏
  • 举报
回复


写法没错,但是你sql语句有问题,把里面@什么的换成对应ds里面的字段
安小武 2013-10-16
  • 打赏
  • 举报
回复
引用 10 楼 happy09li 的回复:
你把你截图的代码贴出来
这条sql语句是不是应该赋值啊
安小武 2013-10-16
  • 打赏
  • 举报
回复
引用 12 楼 happy09li 的回复:
namespace QYMessage
{
public partial class Message : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
CommandConnection();
}
}
private DataSet ds=null;
private void CommandConnection()
{
try
{
SqlConnection sqlconn = new SqlConnection();
sqlconn.ConnectionString = "Data Source=PC2013051514PMC;Initial Catalog=QYMessage;Integrated Security=True";
SqlCommand sqlComand = new SqlCommand();
sqlComand.CommandType = CommandType.Text;
sqlComand.Connection = sqlconn;
SqlDataReader sqlDr = sqlComand.ExecuteReader();
string sqlstr = "select RZXQ.id,RZXQ.a7,RZXQ.a8,dbsmeqdEManage.dbo.tbCPerPostion.ppid,RZXQ.a10,RZXQ.a11,RZXQ.a1,RZXQ.a3,RZXQ.a4,dbsmeqdEManage.dbo.tbCAreaCity.acid"
+" from RZXQ left outer join dbsmeqdEManage.dbo.tbCPerPostion on RZXQ.a9 = dbsmeqdEManage.dbo.tbCPerPostion.ppname "
+"left outer join dbsmeqdEManage.dbo.tbCAreaCity on RZXQ.a2 = dbsmeqdEManage.dbo.tbCAreaCity.acname";
SqlDataAdapter sda = new SqlDataAdapter(sqlstr,sqlconn);
ds= new DataSet();
sda.Fill(ds, "qymessage");
gwmessage.DataSource = ds.Tables[0];
gwmessage.DataBind();
sqlDr.Close();
sqlComand.Dispose();
sqlconn.Close();
}
catch (Exception e)
{
throw e;
}
}

protected void Button1_Click(object sender, EventArgs e)
{

try
{
SqlConnection sqlconn = new SqlConnection();
sqlconn.ConnectionString = "Data Source=PC2013051514PMC;Initial Catalog=dbsmeqdEManage;Integrated Security=True";
for (int i = 0; i < ds.Tables[0].Rows.Count;i++ )
{
//插入sql,执行
}
}
catch(Exception ex)
{
throw ex;
}
}
}
}



安小武 2013-10-16
  • 打赏
  • 举报
回复
引用 12 楼 happy09li 的回复:
namespace QYMessage { public partial class Message : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { CommandConnection(); } } private DataSet ds=null; private void CommandConnection() { try { SqlConnection sqlconn = new SqlConnection(); sqlconn.ConnectionString = "Data Source=PC2013051514PMC;Initial Catalog=QYMessage;Integrated Security=True"; SqlCommand sqlComand = new SqlCommand(); sqlComand.CommandType = CommandType.Text; sqlComand.Connection = sqlconn; SqlDataReader sqlDr = sqlComand.ExecuteReader(); string sqlstr = "select RZXQ.id,RZXQ.a7,RZXQ.a8,dbsmeqdEManage.dbo.tbCPerPostion.ppid,RZXQ.a10,RZXQ.a11,RZXQ.a1,RZXQ.a3,RZXQ.a4,dbsmeqdEManage.dbo.tbCAreaCity.acid" +" from RZXQ left outer join dbsmeqdEManage.dbo.tbCPerPostion on RZXQ.a9 = dbsmeqdEManage.dbo.tbCPerPostion.ppname " +"left outer join dbsmeqdEManage.dbo.tbCAreaCity on RZXQ.a2 = dbsmeqdEManage.dbo.tbCAreaCity.acname"; SqlDataAdapter sda = new SqlDataAdapter(sqlstr,sqlconn); ds= new DataSet(); sda.Fill(ds, "qymessage"); gwmessage.DataSource = ds.Tables[0]; gwmessage.DataBind(); sqlDr.Close(); sqlComand.Dispose(); sqlconn.Close(); } catch (Exception e) { throw e; } } protected void Button1_Click(object sender, EventArgs e) { try { SqlConnection sqlconn = new SqlConnection(); sqlconn.ConnectionString = "Data Source=PC2013051514PMC;Initial Catalog=dbsmeqdEManage;Integrated Security=True"; for (int i = 0; i < ds.Tables[0].Rows.Count;i++ ) { //插入sql,执行 } } catch(Exception ex) { throw ex; } } } }
你是说应该这样吗?里面呢个insert语句,是不是应该赋值。。。
熙风 2013-10-16
  • 打赏
  • 举报
回复
namespace QYMessage { public partial class Message : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { CommandConnection(); } } private DataSet ds=null; private void CommandConnection() { try { SqlConnection sqlconn = new SqlConnection(); sqlconn.ConnectionString = "Data Source=PC2013051514PMC;Initial Catalog=QYMessage;Integrated Security=True"; SqlCommand sqlComand = new SqlCommand(); sqlComand.CommandType = CommandType.Text; sqlComand.Connection = sqlconn; SqlDataReader sqlDr = sqlComand.ExecuteReader(); string sqlstr = "select RZXQ.id,RZXQ.a7,RZXQ.a8,dbsmeqdEManage.dbo.tbCPerPostion.ppid,RZXQ.a10,RZXQ.a11,RZXQ.a1,RZXQ.a3,RZXQ.a4,dbsmeqdEManage.dbo.tbCAreaCity.acid" +" from RZXQ left outer join dbsmeqdEManage.dbo.tbCPerPostion on RZXQ.a9 = dbsmeqdEManage.dbo.tbCPerPostion.ppname " +"left outer join dbsmeqdEManage.dbo.tbCAreaCity on RZXQ.a2 = dbsmeqdEManage.dbo.tbCAreaCity.acname"; SqlDataAdapter sda = new SqlDataAdapter(sqlstr,sqlconn); ds= new DataSet(); sda.Fill(ds, "qymessage"); gwmessage.DataSource = ds.Tables[0]; gwmessage.DataBind(); sqlDr.Close(); sqlComand.Dispose(); sqlconn.Close(); } catch (Exception e) { throw e; } } protected void Button1_Click(object sender, EventArgs e) { try { SqlConnection sqlconn = new SqlConnection(); sqlconn.ConnectionString = "Data Source=PC2013051514PMC;Initial Catalog=dbsmeqdEManage;Integrated Security=True"; for (int i = 0; i < ds.Tables[0].Rows.Count;i++ ) { //插入sql,执行 } } catch(Exception ex) { throw ex; } } } }
安小武 2013-10-16
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using QYMessage.Mode;

namespace QYMessage
{
    public partial class Message : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                CommandConnection();
            }
        }

        private void CommandConnection() 
        {
            try
            {
                SqlConnection sqlconn = new SqlConnection();
                sqlconn.ConnectionString = "Data Source=PC2013051514PMC;Initial Catalog=QYMessage;Integrated Security=True";
                SqlCommand sqlComand = new SqlCommand();
                sqlComand.CommandType = CommandType.Text;
                sqlComand.Connection = sqlconn;
                SqlDataReader sqlDr = sqlComand.ExecuteReader();
                string sqlstr = "select RZXQ.id,RZXQ.a7,RZXQ.a8,dbsmeqdEManage.dbo.tbCPerPostion.ppid,RZXQ.a10,RZXQ.a11,RZXQ.a1,RZXQ.a3,RZXQ.a4,dbsmeqdEManage.dbo.tbCAreaCity.acid"
                +" from RZXQ left outer join dbsmeqdEManage.dbo.tbCPerPostion on RZXQ.a9 = dbsmeqdEManage.dbo.tbCPerPostion.ppname "
                +"left outer join dbsmeqdEManage.dbo.tbCAreaCity on RZXQ.a2 = dbsmeqdEManage.dbo.tbCAreaCity.acname";
                SqlDataAdapter sda = new SqlDataAdapter(sqlstr,sqlconn);
                DataSet ds = new DataSet();
                sda.Fill(ds, "qymessage");
                gwmessage.DataSource = ds.Tables[0];
                gwmessage.DataBind();
                sqlDr.Close();
                sqlComand.Dispose();
                sqlconn.Close();
            }
            catch (Exception e) 
            {
                throw e;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
            try 
            {
                SqlConnection sqlconn = new SqlConnection();
                sqlconn.ConnectionString = "Data Source=PC2013051514PMC;Initial Catalog=dbsmeqdEManage;Integrated Security=True";
                DataSet ds = new DataSet();
                for (int i = 0; i < ds.Tables[0].Rows.Count;i++ )
                {

                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
    }
}
引用 10 楼 happy09li 的回复:
你把你截图的代码贴出来
熙风 2013-10-16
  • 打赏
  • 举报
回复
你把你截图的代码贴出来
安小武 2013-10-16
  • 打赏
  • 举报
回复
引用 8 楼 happy09li 的回复:
MSN
这个我没有耶。。什么人人网,歪歪,都行啊
熙风 2013-10-16
  • 打赏
  • 举报
回复
MSN
加载更多回复(7)

110,566

社区成员

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

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

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