怎样用DataSet复制表 我想完成这样一个功能,先用DataSet打开一个表,把这样DataSet中的表更新到另一个数据库表中,我记得可能这样操作,请高手指点! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 SqlStr="............你的SQL指令........";this.iobj_sqlcomm = new SqlCommand(this.SqlStr, this.iobj_connection); this.iobj_sqlcomm.ExecuteNonQuery();===================================this.iobj_connection為SqlConnection 我写一个将DataSet更新到数据库表中的函数,仅供参考:使用方法:UpdateByDataSet(Connection,dataset, "Ganta", "ganta_id","ganta_bianhao");注意事项:DataSet的TableName必须和SelectCommand的TSQL中的表名称相同SelectCommand 还必须至少返回一个主键列或唯一的列。如果什么都没有返回,就会产生 InvalidOperation 异常,不生成命令。 一次只能将一个 SqlDataAdapter 与一个 SqlCommandBuilder 对象(或相反)互相关联。为了生成 INSERT、UPDATE 或 DELETE 语句,SqlCommandBuilder 会自动使用 SelectCommand 属性来检索所需的元数据集。如果在检索元数据后(例如在第一次更新后)更改 SelectCommand,则应调用 RefreshSchema 方法来更新元数据。 public static bool UpdateByDataSet(SqlConnection conn, DataSet ds, string strTblName, params String[] strFieldNames) { if (strTblName == null || strTblName.Length == 0) { throw new ArgumentNullException("表名不能为空"); return false; } String strSQL = "select "; for (int i = 0; i < strFieldNames.Length; i++) { strSQL += strFieldNames[i] + ","; } //去掉最后一个逗号 strSQL = strSQL.TrimEnd(','); using (SqlCommand myCommand = new SqlCommand(strSQL + " from " + strTblName, (SqlConnection)conn)) { using (SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand)) { using (SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter)) { myAdapter.InsertCommand = myCommandBuilder.GetInsertCommand(); myAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand(); myAdapter.DeleteCommand = myCommandBuilder.GetDeleteCommand(); try { myAdapter.Update(ds, strTblName); } catch (Exception err) { return false; } } } } return true; } WCF中数据的传输问题 C#.net IIS 发布网站的问题 如何清0一个数组? 全局变量奇怪的问题 移动仿真设备调用WebService时,报WebException ! <base target=_self />问题```请进`` 把DataTable中的数据插入到数据表的问题,急!~~~~~~~~~~~~~~~~ COM与.NET dll的不同之处? listview清空问题~高手帮忙!!! vc#.net求救 从未见过的一个GDI+绘图问题! writeXml的问题
this.iobj_sqlcomm = new SqlCommand(this.SqlStr, this.iobj_connection);
this.iobj_sqlcomm.ExecuteNonQuery();
===================================
this.iobj_connection為SqlConnection
使用方法:UpdateByDataSet(Connection,dataset, "Ganta", "ganta_id","ganta_bianhao");
注意事项:DataSet的TableName必须和SelectCommand的TSQL中的表名称相同
SelectCommand 还必须至少返回一个主键列或唯一的列。如果什么都没有返回,就会产生 InvalidOperation 异常,不生成命令。
一次只能将一个 SqlDataAdapter 与一个 SqlCommandBuilder 对象(或相反)互相关联。
为了生成 INSERT、UPDATE 或 DELETE 语句,SqlCommandBuilder 会自动使用 SelectCommand 属性来检索所需的元数据集。如果在检索元数据后(例如在第一次更新后)更改 SelectCommand,则应调用 RefreshSchema 方法来更新元数据。
public static bool UpdateByDataSet(SqlConnection conn, DataSet ds, string strTblName, params String[] strFieldNames)
{
if (strTblName == null || strTblName.Length == 0)
{
throw new ArgumentNullException("表名不能为空");
return false;
}
String strSQL = "select ";
for (int i = 0; i < strFieldNames.Length; i++)
{
strSQL += strFieldNames[i] + ",";
} //去掉最后一个逗号
strSQL = strSQL.TrimEnd(',');
using (SqlCommand myCommand = new SqlCommand(strSQL + " from " + strTblName, (SqlConnection)conn))
{
using (SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand))
{
using (SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter))
{
myAdapter.InsertCommand = myCommandBuilder.GetInsertCommand();
myAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand();
myAdapter.DeleteCommand = myCommandBuilder.GetDeleteCommand();
try
{
myAdapter.Update(ds, strTblName);
} catch (Exception err)
{
return false;
}
}
}
}
return true;
}