C#中如何通过gridview更新数据库中的数据 本帖最后由 think_1987 于 2009-11-24 15:43:35 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 显示:Cnt.openConn(); conn = Cnt.getConn(); OracleCommand ocd = conn.CreateCommand(); ocd.CommandText = strsql; OracleDataReader ord = ocd.ExecuteReader(); while (ord.Read()) { dataGridView_dcgrfhmx.Rows.Add(Convert.ToString(rowCount++), ord[1].ToString(), ord[2].ToString(), ord[3].ToString(), ord[4].ToString(), ord[0].ToString()); } 修改: private void submit_Click(object sender, EventArgs e) { int rowCount;//统计记录的条数,显示序列号 rowCount = dataGridView_dcgrfhmx.Rows.Count; OracleConnection conn = new OracleConnection(); //创建一个新连接 string strsql; try { Cnt.openConn(); conn = Cnt.getConn(); OracleCommand ocd = conn.CreateCommand(); ocd.CommandType = CommandType.Text; for (int i = 0; i < rowCount; i++) { if (dataGridView_dcgrfhmx.Rows[i].Cells[2].Value == null) dataGridView_dcgrfhmx.Rows[i].Cells[2].Value = ""; if (dataGridView_dcgrfhmx.Rows[i].Cells[3].Value == null) dataGridView_dcgrfhmx.Rows[i].Cells[3].Value = ""; if (dataGridView_dcgrfhmx.Rows[i].Cells[4].Value == null) dataGridView_dcgrfhmx.Rows[i].Cells[4].Value = ""; strsql = "update tbl_dcgrfhmxb set HTPJGRL='"+dataGridView_dcgrfhmx.Rows[i].Cells[2].Value.ToString()+"', GRMJ='"+dataGridView_dcgrfhmx.Rows[i].Cells[3].Value.ToString()+"', BZ='"+dataGridView_dcgrfhmx.Rows[i].Cells[4].Value.ToString()+"' where DCBM='"+dataGridView_dcgrfhmx.Rows[i].Cells[5].Value.ToString().Trim()+"'"; ocd.CommandText = strsql; ocd.ExecuteNonQuery(); } ocd.Dispose(); Cnt.closeConn();//关闭连接 MessageBox.Show("修改完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ee) { MessageBox.Show(ee.Message); } }这是oracle的代码,原理跟sql差不多,你借鉴下吧。 mschart局部选择放大 请问怎么通过模拟按键调用第三方程序右键菜单里的某个按钮? c# 去除html代码 保存<p> 正则式该怎么写? 成员变量初始化顺序及数据结构大小问题,请解答 已知页面上一个对象名称,FindControl得不到对象 C#读取excel,行数问题 关于c#编写存储过程添加引用的问题。 Windows form刷新的问题,大家进来帮忙看看 如何獲得當前鍵盤的NumLock,CapsLock,Insert等的狀態? C# 物件拖曳 log4net自定义日志 告诉我如何修改这个XML
Cnt.openConn();
conn = Cnt.getConn();
OracleCommand ocd = conn.CreateCommand();
ocd.CommandText = strsql;
OracleDataReader ord = ocd.ExecuteReader();
while (ord.Read())
{
dataGridView_dcgrfhmx.Rows.Add(Convert.ToString(rowCount++), ord[1].ToString(), ord[2].ToString(), ord[3].ToString(), ord[4].ToString(), ord[0].ToString());
}
修改:
private void submit_Click(object sender, EventArgs e)
{
int rowCount;//统计记录的条数,显示序列号
rowCount = dataGridView_dcgrfhmx.Rows.Count;
OracleConnection conn = new OracleConnection(); //创建一个新连接
string strsql;
try
{
Cnt.openConn();
conn = Cnt.getConn();
OracleCommand ocd = conn.CreateCommand();
ocd.CommandType = CommandType.Text;
for (int i = 0; i < rowCount; i++)
{
if (dataGridView_dcgrfhmx.Rows[i].Cells[2].Value == null)
dataGridView_dcgrfhmx.Rows[i].Cells[2].Value = "";
if (dataGridView_dcgrfhmx.Rows[i].Cells[3].Value == null)
dataGridView_dcgrfhmx.Rows[i].Cells[3].Value = "";
if (dataGridView_dcgrfhmx.Rows[i].Cells[4].Value == null)
dataGridView_dcgrfhmx.Rows[i].Cells[4].Value = "";
strsql = "update tbl_dcgrfhmxb set HTPJGRL='"+dataGridView_dcgrfhmx.Rows[i].Cells[2].Value.ToString()+"', GRMJ='"+dataGridView_dcgrfhmx.Rows[i].Cells[3].Value.ToString()+"', BZ='"+dataGridView_dcgrfhmx.Rows[i].Cells[4].Value.ToString()+"' where DCBM='"+dataGridView_dcgrfhmx.Rows[i].Cells[5].Value.ToString().Trim()+"'";
ocd.CommandText = strsql;
ocd.ExecuteNonQuery();
}
ocd.Dispose();
Cnt.closeConn();//关闭连接 MessageBox.Show("修改完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); }
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}这是oracle的代码,原理跟sql差不多,你借鉴下吧。