问题描述
这是我写的一段代码,求高手帮忙看看,目的是要在老数据库中的内容插到新数据库中,可是两个数据库对应的id不一样,所以请帮忙看看吧namespaceConsoleApplication1{classProgram{privatestaticreadonlystringServerStr=ConfigurationManager.ConnectionStrings["ServerStr"].ConnectionString;privatestaticreadonlystringManageStr=ConfigurationManager.ConnectionStrings["ManageStr"].ConnectionString;privatestaticinttd=0;privatestaticintrs=0;privatestaticStringDataGridBind(intid){stringareaname=null;using(SqlConnectionconn=newSqlConnection(ServerStr)){conn.Open();stringstrSQL="SELECT*fromarea_Infowhereid="+id;using(SqlCommandcmd=newSqlCommand(strSQL,conn)){SqlDataReaderdr=cmd.ExecuteReader();while(dr.Read()){areaname=dr["areaname"].ToString();}}}returnareaname;}privatestaticintDataGridBind1(stringareaname){intid=0;using(SqlConnectionconn=newSqlConnection(ServerStr)){conn.Open();stringstrSQL="SELECT*fromarea_Infowhereareaname=@areaname";using(SqlCommandcmd=newSqlCommand(strSQL,conn)){SqlDataReaderdr=cmd.ExecuteReader();while(dr.Read()){id=Convert.ToInt32(dr["id"].ToString());}}}returnid;}staticvoidMain(string[]args){inti=0;Console.WriteLine("开始执行");using(SqlConnectionconn=newSqlConnection(ServerStr)){conn.Open();Console.WriteLine("server数据库已经打开");using(SqlConnectioncon=newSqlConnection(ManageStr)){con.Open();Console.WriteLine("manage数据库已经打开");using(SqlCommandcmd=conn.CreateCommand()){cmd.CommandText="select*fromBusiness_Infoorderbyidasc";SqlDataReaderdr=cmd.ExecuteReader();while(dr.Read()){i++;stringstrSQL="InsertintoBusiness_Info_WinLing(Status,Workorder,Account,[Action],Username,Telephone,Address,StartTime,Demandtime,[Send],CompleteTime,SetupTime,ChargeTime,[Type],IP,MAC,Result,AdminComment,TownComment,Admin,Town,Accept,Timeout)"+"Values(@Status,@Workorder,@Account,@Action,@Username,@Telephone,@Address,@StartTime,@Demandtime,@Send,@CompleteTime,@SetupTime,@ChargeTime,@Type,@IP,@MAC,@Result,@AdminComment,@TownComment,@Admin,@Town,@Accept,@Timeout)";using(SqlCommandcmdd=newSqlCommand(strSQL,con)){cmdd.Parameters.Add(newSqlParameter("@Status",Convert.ToInt32(dr["Status"])));cmdd.Parameters.Add(newSqlParameter("@Workorder",dr["Workorder"].ToString()));cmdd.Parameters.Add(newSqlParameter("@Account",dr["Account"].ToString()));cmdd.Parameters.Add(newSqlParameter("@Action",dr["Action"].ToString()));cmdd.Parameters.Add(newSqlParameter("@Username",dr["Username"].ToString()));cmdd.Parameters.Add(newSqlParameter("@Telephone",dr["Telephone"].ToString()));cmdd.Parameters.Add(newSqlParameter("@Address",dr["Address"].ToString()));cmdd.Parameters.Add(newSqlParameter("@StartTime",dr["StartTime"].ToString()));if(dr["StartTime"]!=DBNull.Value&&dr["EndTime"]!=DBNull.Value){DateTimeStartTime=Convert.ToDateTime(dr["StartTime"].ToString());DateTimeEndTime=Convert.ToDateTime(dr["endtime"].ToString());TimeSpandt=(TimeSpan)Convert.ToDateTime(StartTime).Subtract(Convert.ToDateTime(EndTime));td=System.Math.Abs(dt.Days)*24+System.Math.Abs(dt.Hours);}cmdd.Parameters.Add(newSqlParameter("@Demandtime",td==0?72:td));cmdd.Parameters.Add(newSqlParameter("@Send",dr["Send"].ToString()));cmdd.Parameters.Add(newSqlParameter("@CompleteTime",dr["CompleteTime"].ToString()));cmdd.Parameters.Add(newSqlParameter("@SetupTime",dr["SetupTime"].ToString()));cmdd.Parameters.Add(newSqlParameter("@ChargeTime",dr["ChargeTime"].ToString()));if(dr["Type"]!=DBNull.Value){cmdd.Parameters.Add(newSqlParameter("@Type",Convert.ToInt32(dr["type"])));}else{cmdd.Parameters.Add(newSqlParameter("@Type",rs));}cmdd.Parameters.Add(newSqlParameter("@IP",dr["IP"].ToString()));cmdd.Parameters.Add(newSqlParameter("@MAC",dr["MAC"].ToString()));if(dr["Result"]!=DBNull.Value){cmdd.Parameters.Add(newSqlParameter("@Result",Convert.ToInt32(dr["Result"])));}else{cmdd.Parameters.Add(newSqlParameter("@Result",rs));}cmdd.Parameters.Add(newSqlParameter("@AdminComment",dr["AdminComment"].ToString()));cmdd.Parameters.Add(newSqlParameter("@TownComment",dr["TownComment"].ToString()));cmdd.Parameters.Add(newSqlParameter("@Admin",dr["Admin"].ToString()));cmdd.Parameters.Add(newSqlParameter("@Town",Convert.ToInt32(dr["Town"])));cmdd.Parameters.Add(newSqlParameter("@Accept",dr["Accept"].ToString()));cmdd.Parameters.Add(newSqlParameter("@Timeout",rs));try{if(cmdd.ExecuteNonQuery()>0){Console.WriteLine("正在插入第"+(i).ToString()+"条,请稍候.........");}}catch{Console.WriteLine("Status="+Convert.ToInt32(dr["Status"]));Console.WriteLine("Workorder="+(dr["Workorder"]));Console.WriteLine("Account="+(dr["Account"]));Console.WriteLine("Action="+(dr["Action"]));Console.WriteLine("Username="+(dr["Username"]));Console.WriteLine("Telephone="+(dr["Telephone"]));Console.WriteLine("Address="+(dr["Address"]));Console.WriteLine("StartTime="+(dr["StartTime"]));Console.WriteLine("Send="+(dr["Send"]));Console.WriteLine("CompleteTime="+(dr["CompleteTime"]));Console.WriteLine("SetupTime="+(dr["SetupTime"]));Console.WriteLine("ChargeTime="+(dr["ChargeTime"]));Console.WriteLine("Type="+Convert.ToInt32(dr["Type"]));Console.WriteLine("IP="+(dr["IP"]));Console.WriteLine("MAC="+(dr["MAC"]));Console.WriteLine("Result="+Convert.ToInt32(dr["Result"]));Console.WriteLine("AdminCommen="+(dr["AdminCommen"]));Console.WriteLine("TownComment="+(dr["TownComment"]));Console.WriteLine("Admin="+(dr["Admin"]));Console.WriteLine("Town="+(dr["Town"]));Console.WriteLine("Accept="+(dr["Accept"]));Console.WriteLine("Timeout="+(dr["Timeout"]));Thread.Sleep(10000);}}}con.Close();con.Dispose();}conn.Close();conn.Dispose();Console.ReadKey();}}}}}
解决方案
解决方案二:
那肯定要找能够代表唯一性的元素呀