我想完成这样一个功能,先用DataSet打开一个表,把这样DataSet中的表更新到另一个数据库表中,
我记得可能这样操作,请高手指点!

解决方案 »

  1.   

    SqlStr="............你的SQL指令........";
    this.iobj_sqlcomm = new SqlCommand(this.SqlStr, this.iobj_connection);
                this.iobj_sqlcomm.ExecuteNonQuery();
    ===================================
    this.iobj_connection為SqlConnection
      

  2.   

    我写一个将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;
            }