我先把一个Execle文档的数据读取到dataGridView1 private void button1_Click_1(object sender, EventArgs e)
{
DataSet ds;
OpenFileDialog openFile = new OpenFileDialog();
openFile.Multiselect = false;
openFile.RestoreDirectory = true;
openFile.Filter = "Text files (*.xls)|*.xls|All files (*.*)|*.*"; if (openFile.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFile.FileName;
ds = ImportExcel(openFile.FileName);//获得Excel
//look data have success read?
this.dataGridView1.DataSource = ds.Tables[0];
}
else
{
return;
}
}然后点确认就提交到表中
表名em_transferss_t两列 transfers_id , em_transferss_t.TRANSFERS_OLDLANDNUM private void button4_Click(object sender, EventArgs e)
{
//???怎么写呢
}
{
DataSet ds;
OpenFileDialog openFile = new OpenFileDialog();
openFile.Multiselect = false;
openFile.RestoreDirectory = true;
openFile.Filter = "Text files (*.xls)|*.xls|All files (*.*)|*.*"; if (openFile.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFile.FileName;
ds = ImportExcel(openFile.FileName);//获得Excel
//look data have success read?
this.dataGridView1.DataSource = ds.Tables[0];
}
else
{
return;
}
}然后点确认就提交到表中
表名em_transferss_t两列 transfers_id , em_transferss_t.TRANSFERS_OLDLANDNUM private void button4_Click(object sender, EventArgs e)
{
//???怎么写呢
}
provider=Microsoft.jet.OLEDB.4.0;Data source=" &server.MapPath(db)&" ;Extended Properties=Excel 8.0"
查询语句:strRS = "select * from [Sheet1$]"
其中:Excel 8.0
可以根据需要换成:Excel 9.0
或者你将2003格式转换成2000格式等等
for(int i=0;i<GridView1.Rows.Count)
{
sting id=GridView1.Rows[i].Cells[1].Text;
检索id是否在transfers_id 表中存在,
if(存在)
插入语句;
}
我是菜鸟
是说把EXCEL的数据绑定到GRIDVIEW上,然后再GRIDVIEW上操作之后,把操作后的结果回写到EXCEL么?那么需要问的是:
1,修改是指只在原来的数据内容上修改,是否还会进行删除或增加?
2,回写的数据是否包括了GRIDVIEW上所有的修改内容?包括单纯修改还有减少和增加的。如果是全部增删改的回写,最方便的方法是把原来的EXCEL的数据DELETE掉
再把现有的GRIDVIEW上的数据全部INSERT进去。
如果不是上述的全部回写,而是修改部分的话,需要先在数据表里设定一个主键,比如员工信息的员工编号这样的,修改信息的时候,这个主键是不能修改的,这样在进行判断是否存的时候才有参考的依据。
如代码,我已经实现了Execle文档数据读取到dataGridView1的功能 显示两列 F1,F2 现在要实现的功能 就是 要把dataGridView1 数据根据条件上传到 em_transferss_t表中 em_transferss_t 表有两列 em_transfers_t.TRANSFERS_ID,和 em_transferss_t.TRANSFERS_OLDLANDNUM 条件就是 如果dataGridView1 的 F1列 数据 和em_transfers_t.TRANSFERS_ID 相等,就把 dataGridView1 F2 这列数据添加到em_transferss_t.TRANSFERS_OLDLANDNUM中,
也就是说em_transferss_t这个表里原有的em_transferss_t.TRANSFERS_OLDLANDNUM这个字段的值是从GRIDVIEW这里赋予的是么?那么就不是用INSERT了,用UPDATE就可以了最直接的方式:string strSql = "";
for(int i=0;i<GridView1.Rows.Count)
{
string id=GridView1.Rows[i].Cells[0].Text;
string OLDLANDNUM=GridView1.Rows[i].Cells[1].Text;
strSql += " update em_transferss_t set TRANSFERS_ID ='"+OLDLANDNUM+"' where TRANSFERS_ID='"+id+"' ";
}
然后执行strSql,执行的时候最好用TRANSACTION,万一有一条出错可以ROLLBACK()。p.s.这个方法仅可用于TRANSFERS_ID字段为em_transferss_t表的主键,或者说TRANSFERS_ID必须是唯一值字段才可以,不然的话,需要把where子句里的条件改成唯一值字段。
string strsql = "";
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
string id = dataGridView1.Rows[i].Cells[0].Value;
string oldlandnum = dataGridView1.Rows[i].Cells[1].Value;
strsql += " update em_transferss_t set TRANSFERS_ID ='" + oldlandnum + "' where TRANSFERS_ID='" + id + "' ";
} 错误 1 无法将类型“object”隐式转换为“string”。存在一个显式转换(是否缺少强制转换?) D:\第二次写\update\update\Form1.cs 58 27 update这样写
string id =int.Parse(dataGridView1.Rows[i].Cells[0].Value).ToString();
错误 2 参数“1”: 无法从“object”转换为“string” D:\第二次写\update\update\Form1.cs 58 36 updatestring id =Int32.Parse(dataGridView1.Rows[i].Cells[0].Value).ToString();
参数“1”: 无法从“object”转换为“string麻烦大虾 再看下
string strsql = "";
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
string id = dataGridView1.Rows[i].Cells[0].Value.ToString();
string oldlandnum = dataGridView1.Rows[i].Cells[1].Value.ToString();
strsql += " update em_transferss_t set TRANSFERS_ID ='" + oldlandnum + "' where TRANSFERS_ID='" + id + "' ";
} 因为需要在strsql中拼接字符串的,所以需要把id和oldlandnum都转为STRING形式才能拼接。
string strsql = "";
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
string id = dataGridView1.Rows[i].Cells[0].Text;
string oldlandnum = dataGridView1.Rows[i].Cells[1].Text;
strsql += " update em_transferss_t set TRANSFERS_ID ='" + oldlandnum + "' where TRANSFERS_ID='" + id + "' ";
} 注意:用dataGridView1.Rows[i].Cells[0].Text 和 dataGridView1.Rows[i].Cells[0].Value出现的值有可能是不同的,Text的值是界面上可以看到的值,Value值有可能是后台绑定时候的其他值,于是用Text比较好。
其他的在上面的回复里说了
dataGridView1.Rows[i].Cells[1].Value;
这2个要转换类型,看你datagridview里用的是lable还是textbox来决定转成什么
另外,最好不要用Rows[i].Cells[0],列多的时候数都数不过来,用控件id来匹配是最好的,dataGridView1.Item[i].FindControl(“id”)as 控件类型
dataGridView1.Item[i].FindControl(“id”) 这个方法使用于模板列(列里包含其他控件),如果只是单纯的绑定数据的话,还是只能用dataGridView1.Rows[i].Cells[i].Text这样的形式。
string id = dataGridView1.Rows[i].Cells[0].Text;点不出来.text属性的 你试一试
再把gridview里修改过的数据存到数据库里我写过一个这样的程序,真是很糟糕的设计;从带宽和页面显示两个方面会遇到困难
建议修改一下设计:
1.读 excel 到 db
2.从db取数据,分页显示
3.取页面的修改,更新到db内
4.确认完成时,存到最终目标里(如果开始就存在最终目标里,这个就不要了)很抱歉只有想法没有代码
因为不清楚,LZ的开发工具版本,如果只能用Value的话请直接转成string形式即可,不需要转成int再转成string即:string id = dataGridView1.Rows[i].Cells[0].Value.ToString().Trim();
{
conn.Open();
string strsql = "";
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{ string id = dataGridView1.Rows[i].Cells[0].Value.ToString();
string oldlandnum = dataGridView1.Rows[i].Cells[1].Value.ToString();
strsql += "update em_transferss_t set TRANSFERS_oldlandnum ='" + oldlandnum + "'where TRANSFERS_ID='" + id + "'";
OracleCommand com = new OracleCommand(strsql, conn);
count = com.ExecuteNonQuery(); }
conn.Close();
if (count > 0)
{
MessageBox.Show("更新成功!");
}
else
{
MessageBox.Show("失败!");
}
}
catch (Exception ee)
{ MessageBox.Show(ee.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
conn.Close();
}他第一次运行的时候strsql语句是 update em_transferss_t set TRANSFERS_oldlandnum ='1'where TRANSFERS_ID='101'他循环一次 i=1的时候 strsql语句就成update em_transferss_t set TRANSFERS_oldlandnum ='1'where TRANSFERS_ID='101'update em_transferss_t set TRANSFERS_oldlandnum ='2'where TRANSFERS_ID='102'两次语句加一起了`` 就报错说未正常结束 请问这是这么了?
你仔细看下我给你的代码,我明显是带了空格的,这些空格都是有意义的。按下面的样子写就OK了,空格不要去掉。strsql += " update em_transferss_t set TRANSFERS_ID ='" + oldlandnum + "' where TRANSFERS_ID='" + id + "' ";
以下这个要放在循环体之外:
OracleCommand com = new OracleCommand(strsql, conn);
count = com.ExecuteNonQuery();
我写个完整的:
try
{
conn.Open();
string strsql = "";
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{ string id = dataGridView1.Rows[i].Cells[0].Value.ToString();
string oldlandnum = dataGridView1.Rows[i].Cells[1].Value.ToString();
strsql += " update em_transferss_t set TRANSFERS_oldlandnum ='" + oldlandnum + "' where TRANSFERS_ID='" + id + "' ";
}
OracleCommand com = new OracleCommand(strsql, conn);
count = com.ExecuteNonQuery();
conn.Close();
if (count > 0)
{
MessageBox.Show("更新成功!");
}
else
{
MessageBox.Show("失败!");
}
}
catch (Exception ee)
{ MessageBox.Show(ee.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
conn.Close();
}
另外要注意的是,OracleCommand com = new OracleCommand(strsql, conn);这个最好创建一个transaction(事务),成功的话一起COMMIT(),失败的话rollback(),这样对数据比较安全
strsql += "update em_transferss_t set TRANSFERS_oldlandnum ='" + oldlandnum + "'where TRANSFERS_ID='" + id + "'";i=1的时候 sql因为是+= ,所以就变2句并一起了
count = com.ExecuteNonQuery();放外面的话, 那它的语句不是只执行最后那一句?
就是要并一起的啊,我的目的不是为了一句一句的执行SQL,【当然这样也可以】
我这样做的意义是为了让strSql的形式变成" sql1 sql2 sql3 "这样的形式,然后一次性的执行ExecuteNonQuery()即可。
把i <dataGridView1.Rows.Count 条件换掉
所以我用了+= 而不是 = ,就是为了让所有的SQL叠加起来。
如果sql本身无法识别的话(这个要看sql的版本),安全一点话,就一句一句的执行好了。 还有会超出范围是因为count = com.ExecuteNonQuery();这句有问题。
我看了下count好像没有定义哦,如果要统计的,另外定义个变量。这部分的更改如下:
string strsql = "";
for (int i = 0,int iCount = 0; i < dataGridView1.Rows.Count; i++)
{ string id = dataGridView1.Rows[i].Cells[0].Value.ToString();
string oldlandnum = dataGridView1.Rows[i].Cells[1].Value.ToString();
strsql = " update em_transferss_t set TRANSFERS_oldlandnum ='" + oldlandnum + "' where TRANSFERS_ID='" + id + "' ";
OracleCommand com = new OracleCommand(strsql, conn);
if(com.ExecuteNonQuery()>0) iCount++;
}
换成for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
是解决你多循环一次的问题你strsql可以+=,但OracleCommand com = new OracleCommand(strsql, conn);
count = com.ExecuteNonQuery();
一定要放循环外面去
那句我已经复制了好几遍了,你没有加空格,我说过了,直接复制我的就好了,我再复制一下:strsql += " update em_transferss_t set TRANSFERS_oldlandnum ='" + oldlandnum + "' where TRANSFERS_ID='" + id + "' ";你写的如下:strsql += "update em_transferss_t set TRANSFERS_oldlandnum ='" + oldlandnum + "'where TRANSFERS_ID='" + id + "'";看出来区别了么?好了略过这个问题,i的数值超过数组最大值的问题,LS一个TX说的对,应该是:for (int i = 0,int iCount = 0; i < dataGridView1.Rows.Count-1; i++)
try
{
conn.Open();
string strsql = "";
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{ string id = dataGridView1.Rows[i].Cells[0].Value.ToString();
string oldlandnum = dataGridView1.Rows[i].Cells[1].Value.ToString();
strsql += " update em_transferss_t set TRANSFERS_oldlandnum ='" + oldlandnum + "' where TRANSFERS_ID='" + id + "' ";
}
OracleCommand com = new OracleCommand(strsql, conn);
int count = com.ExecuteNonQuery();
conn.Close();
if (count > 0)
{
MessageBox.Show("更新成功!");
}
else
{
MessageBox.Show("失败!");
}
}
catch (Exception ee)
{ MessageBox.Show(ee.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
conn.Close();
}
{
int ii = dataGridView1.Rows.Count;
int count = 0;
try
{
conn.Open();
string strsql = "";
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
string id = dataGridView1.Rows[i].Cells[0].Value.ToString();
string oldlandnum = dataGridView1.Rows[i].Cells[1].Value.ToString();
strsql = " update em_transferss_t set TRANSFERS_oldlandnum ='" + oldlandnum + "' where TRANSFERS_ID='" + id + "' ";
OracleCommand com = new OracleCommand(strsql, conn);
count = com.ExecuteNonQuery();
commCommit.ExecuteNonQuery(); //好像说orecle的命令都要手动commit才可以
}
conn.Close(); if (count > 0)
{
MessageBox.Show("更新成功!");
}
else
{
MessageBox.Show("失败!");
}它现在是说失败了 ,明天数据读取到了 循环了 也执行了啊 sql语句单独放到orecle中也是可以成功的 麻烦各位了 30分 回帖到了50 ``` 我觉得我很赚,很对不住大家``
update em_transferss_t set TRANSFERS_oldlandnum ='2' where TRANSFERS_ID='102'
update em_transferss_t set TRANSFERS_oldlandnum ='3' where TRANSFERS_ID='103'
update em_transferss_t set TRANSFERS_oldlandnum ='4' where TRANSFERS_ID='104'
update em_transferss_t set TRANSFERS_oldlandnum ='5' where TRANSFERS_ID='105'
update em_transferss_t set TRANSFERS_oldlandnum ='6' where TRANSFERS_ID='106'
update em_transferss_t set TRANSFERS_oldlandnum ='7' where TRANSFERS_ID='107' 运行到 int count = com.ExecuteNonQuery();sql语句未正常结束
按LZ的情况看,LZ看下是否连接数据库的地方有问题,比如数据库连接字符串之类的,这个解决的话应该就OK了。
{
int ii = dataGridView1.Rows.Count;
int count = 0;
try
{
conn.Open();
string strsql = "";
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
string id = dataGridView1.Rows[i].Cells[0].Value.ToString();
string oldlandnum = dataGridView1.Rows[i].Cells[1].Value.ToString();
strsql = " update em_transferss_t set TRANSFERS_oldlandnum ='" + oldlandnum + "' where TRANSFERS_ID='" + id + "' ";
OracleCommand com = new OracleCommand(strsql, conn);
count = com.ExecuteNonQuery();
commCommit.ExecuteNonQuery(); //好像说orecle的命令都要手动commit才可以
}
conn.Close(); if (count > 0)
{
MessageBox.Show("更新成功!");
}
else
{
MessageBox.Show("失败!");
}为什么会失败? 数据读取到了 循环了 也执行了啊 sql语句单独放到orecle中也是可以成功的