我有两个数据库,都是SQLSERVER2008的,现在有个问题,一个是旧库,一个新库,我要将旧库中某一张表中的数据,插入到新库中同名表中,要求是写一个页面,页面上放一个按钮,点击按钮后,就可以把旧库中表中的数据插入到新库表中,数据不可以重复,但是现在有个问题,当我在旧库中新插入一条数据口,再点击按钮,就不可以往新表中插入数据了,因为数据不能重复,代码如下,我就是想要实现当旧库中插入新的数据后,我点击按钮,就可以直接把新加的数据插入到新表中,我的代码中要加些什么呢?求大神帮忙~
public partial class Move : System.Web.UI.Page
{
protected static string oldconnstr = "server=.;database=sql_chahaer_web;uid=sa;pwd=ok;connection lifetime=5;min pool size=1 ;max pool size=10000;";
protected static string newconnstr = "server=.;database=sql_chahaer_qikan;uid=sa;pwd=ok;connection lifetime=5;min pool size=1 ;max pool size=10000;";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button3_Click(object sender, EventArgs e)
{
//读取旧数据
DataSet dataSet = new DataSet();
string query = "select * from EIS_TECH_INFO";
SqlDataAdapter Adapter = new SqlDataAdapter(query, oldconnstr);
Adapter.Fill(dataSet, "userinfo");
//读取新表中的数据
DataControl DCL = new DataControl();
DataTable newdataTable = new DataTable();
string newquery = "select code,name,depart,nick,pass,flag,jurisdiction,usertype from EIS_TECH_INFO"; SqlDataAdapter sqlDA1 = new SqlDataAdapter(newquery, newconnstr); SqlCommandBuilder sqlCB1 = new SqlCommandBuilder(sqlDA1); sqlDA1.Fill(newdataTable); int i = 0;
foreach (DataRow olddataRow in dataSet.Tables["userinfo"].Rows)
{
DataRow newdataRow = newdataTable.NewRow();
newdataRow["code"] = olddataRow["code"];
newdataRow["name"] = olddataRow["name"];
newdataRow["depart"] = "6";//统一归属信息中心
newdataRow["nick"] = olddataRow["nick"];
newdataRow["pass"] = olddataRow["pass"];
newdataRow["usertype"] = "30"; newdataRow["jurisdiction"] = "";
newdataRow["flag"] = "20";
newdataTable.Rows.Add(newdataRow);
i = i + 1;
} try
{
sqlDA1.Update(newdataTable);
Response.Write("<script>alert('恭喜,成功导入 " + i + " 条记录!');</script>"); }
catch (Exception ex)
{
Response.Write("<script>alert('请确认姓名是否在系统中已存在!')</script>");
} }
}
public partial class Move : System.Web.UI.Page
{
protected static string oldconnstr = "server=.;database=sql_chahaer_web;uid=sa;pwd=ok;connection lifetime=5;min pool size=1 ;max pool size=10000;";
protected static string newconnstr = "server=.;database=sql_chahaer_qikan;uid=sa;pwd=ok;connection lifetime=5;min pool size=1 ;max pool size=10000;";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button3_Click(object sender, EventArgs e)
{
//读取旧数据
DataSet dataSet = new DataSet();
string query = "select * from EIS_TECH_INFO";
SqlDataAdapter Adapter = new SqlDataAdapter(query, oldconnstr);
Adapter.Fill(dataSet, "userinfo");
//读取新表中的数据
DataControl DCL = new DataControl();
DataTable newdataTable = new DataTable();
string newquery = "select code,name,depart,nick,pass,flag,jurisdiction,usertype from EIS_TECH_INFO"; SqlDataAdapter sqlDA1 = new SqlDataAdapter(newquery, newconnstr); SqlCommandBuilder sqlCB1 = new SqlCommandBuilder(sqlDA1); sqlDA1.Fill(newdataTable); int i = 0;
foreach (DataRow olddataRow in dataSet.Tables["userinfo"].Rows)
{
DataRow newdataRow = newdataTable.NewRow();
newdataRow["code"] = olddataRow["code"];
newdataRow["name"] = olddataRow["name"];
newdataRow["depart"] = "6";//统一归属信息中心
newdataRow["nick"] = olddataRow["nick"];
newdataRow["pass"] = olddataRow["pass"];
newdataRow["usertype"] = "30"; newdataRow["jurisdiction"] = "";
newdataRow["flag"] = "20";
newdataTable.Rows.Add(newdataRow);
i = i + 1;
} try
{
sqlDA1.Update(newdataTable);
Response.Write("<script>alert('恭喜,成功导入 " + i + " 条记录!');</script>"); }
catch (Exception ex)
{
Response.Write("<script>alert('请确认姓名是否在系统中已存在!')</script>");
} }
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货