亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

源數據庫到目標數據庫的插入和更新

源數據庫到目標數據庫的插入和更新

慕妹3242003 2018-12-07 12:09:48
想把一個SQL的查詢結果寫到新的目標數據里。代碼如下: 其中command.text 就是 Select * from Region; 然后目標數據庫里,表結構與源表一樣,只有一行記錄,然后設置了主鍵自動增加。 但是現在的代碼不能把源數據寫進去。 求教如何改正? ? private?void?btnSave_Click(object?sender,?EventArgs?e) { ? string?serverName = textBoxTargetServer.Text; ? var?dbName = comboBoxTargetDatabase.SelectedItem?as?string; ? string?conStr=?"Data Source= "?+ serverName +?";Initial Catalog="?+ dbName +?";Integrated Security=True"; ? SqlCommand?cmd =?new?SqlCommand(); ? using?(SqlConnection?conn =?new?SqlConnection(conStr)) { cmd.Connection = conn; cmd.CommandType =?CommandType.Text; cmd.CommandText = textBox1.Text; cmd.Connection.Open(); SqlDataAdapter?sda =?new?SqlDataAdapter(textBox1.Text, conn); SqlCommandBuilder?cb =?new?SqlCommandBuilder(sda); sda.InsertCommand = cb.GetInsertCommand(); sda.DeleteCommand = cb.GetDeleteCommand(); sda.UpdateCommand = cb.GetUpdateCommand(); targetDataSet =?new?DataSet(); sda.Fill(targetDataSet); ? DataTable?dtTarget = targetDataSet.Tables[0]; ? DataTable?dtSource = sourceDataSet.Tables[0]; ? for?(int?i = 0; i < sourceDataSet.Tables[0].Rows.Count; i++) { ? DataRow?sourceRow = sourceDataSet.Tables[0].Rows[i]; dtTarget.ImportRow(sourceRow); } sda.Update(targetDataSet); } cmd.Parameters.Clear(); cmd.Connection.Close();
查看完整描述

2 回答

?
米琪卡哇伊

TA貢獻1998條經驗 獲得超6個贊

在?dtTarget.ImportRow(sourceRow); 之后加上:

dtTarget.Rows(i).SetAdded();

參考:update is not working with importrow

查看完整回答
反對 回復 2019-01-07
?
MM們

TA貢獻1886條經驗 獲得超2個贊

我認為 在原表Region查詢時 排出主鍵就沒問題了

查看完整回答
反對 回復 2019-01-07
  • 2 回答
  • 0 關注
  • 869 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號