⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 index.aspx.cs

📁 .net下一个数据库数据互换
💻 CS
📖 第 1 页 / 共 2 页
字号:
						//////////////////////////////////////////////////////////////////
						newds.Tables[0].Rows.Add(row);
						Session["State"] = n;
						newAdp.Update(newds);
					}
				}
			}
			catch(Exception ex)
			{
				Session["err"]="更新用户表失败,请检查原数据表eUsers和新数据表zUsers表和数据时候正确!\n"+ex.Message;
				Session["State"] = 100;
				return;
			}

			
			//更新资源表
			try
			{
				int n=0;
				sql="delete from eDocument  delete from eAnnex";
				System.Data.SqlClient.SqlCommand newcmd=new SqlCommand(sql,newConn);
				newConn.Open();
				newcmd.ExecuteNonQuery();
				newConn.Close();
				//取旧表数据
				sql="select a.*,b.filename,b.fileext,b.filesize,c.content from eDocument a left join eUpload b on a.id=b.pid  left join eScienceWord c on a.id=c.pid";
				System.Data.SqlClient.SqlDataAdapter oldAdp=new SqlDataAdapter(sql,oldConn);
				System.Data.DataTable olddt=new DataTable();
				oldAdp.Fill(olddt);
				//取新表
				sql="select * from eDocument";
				SqlCommand cmd=new SqlCommand(sql,newConn);
				System.Data.SqlClient.SqlDataAdapter newAdp=new SqlDataAdapter(sql,newConn);
				newAdp.SelectCommand=cmd;
				SqlCommandBuilder cmdb = new SqlCommandBuilder(newAdp);
				System.Data.DataSet newds =new DataSet();
				newAdp.Fill(newds);
				//取新表
				sql="select * from eAnnex";
				SqlCommand cmd2=new SqlCommand(sql,newConn);
				System.Data.SqlClient.SqlDataAdapter newAdp2=new SqlDataAdapter(sql,newConn);
				newAdp2.SelectCommand=cmd2;
				SqlCommandBuilder cmdb2 = new SqlCommandBuilder(newAdp2);
				System.Data.DataSet newds2 =new DataSet();
				newAdp2.Fill(newds2);

				string eid="",aid="",filename="",paths="",phs="",ph="";

				ph=ph1.Text;
				if(ph.Substring(ph.Length-1,1)!="\\")
					ph+="\\";
				if(olddt.Rows.Count>0)
				{
					for(int i=0;i<olddt.Rows.Count;i++)
					{
						n=(int)(i*99/olddt.Rows.Count);
						eid=olddt.Rows[i]["id"].ToString();
						if(olddt.Rows[i]["DType"].ToString().Trim().Equals("Upload"))
							filename=olddt.Rows[i]["filename"].ToString();
						else
							filename="ScienceWord.sdc";

						paths=ph+"document\\"+eid+"\\"+filename.Trim();
						
						if((System.IO.File.Exists(paths) && olddt.Rows[i]["DType"].ToString().Trim().Equals("Upload")) || olddt.Rows[i]["DType"].ToString().Trim().Equals("ScienceWord"))
						{
							DataRow row=newds.Tables[0].NewRow();
							///////////////////////////////////////////////////////////////////
							//更改自动标号
							int newid=(int)olddt.Rows[i]["id"]-1;
							sql="DBCC CHECKIDENT(eDocument,RESEED,"+newid.ToString()+")";
							SqlCommand cmdid=new SqlCommand(sql,newConn);
							newConn.Open();
							cmdid.ExecuteNonQuery();
							newConn.Close();
							//其他字段
							row["gid"]=olddt.Rows[i]["gid"];
							row["sid"]=olddt.Rows[i]["sid"];
							row["Title"]=olddt.Rows[i]["Title"];
							row["DType"]=olddt.Rows[i]["DType"].ToString().Trim().Equals("Upload")?"上传的文件":olddt.Rows[i]["DType"].ToString().Trim().Equals("ScienceWord")?"ScienceWord":"资源库文件";
							row["Author"]=olddt.Rows[i]["Author"];
							//取用户名
							sql="select * from zUsers where account='"+olddt.Rows[i]["Author"].ToString()+"'";
							System.Data.DataTable dtu=new DataTable();
							System.Data.SqlClient.SqlDataAdapter adpu=new SqlDataAdapter(sql,newConn);
							adpu.Fill(dtu);
							if(dtu.Rows.Count>0)
								row["AuthorName"]=dtu.Rows[0]["Nick"];
							else
								row["AuthorName"]="";
							row["PostDate"]=olddt.Rows[i]["PostDate"];
							row["ClickNum"]=olddt.Rows[i]["ClickNum"];
							row["Status"]=1;
							row["pos"]=1;
							//////////////////////////////////////////////////////////////////
							newds.Tables[0].Rows.Add(row);
							newAdp.Update(newds);
							////================================////////////
							
							DataRow rw=newds2.Tables[0].NewRow();
							rw["DocID"]=olddt.Rows[i]["id"];
							rw["Title"]=olddt.Rows[i]["Title"];
							rw["FileName"]=filename;
							if(filename=="ScienceWord.sdc")
							{
								rw["FileExt"]=DBNull.Value;
								rw["FileSize"]=DBNull.Value;
							}
							else
							{
								rw["FileExt"]=olddt.Rows[i]["FileExt"];
								rw["FileSize"]=olddt.Rows[i]["FileSize"];
							}
							rw["DType"]=olddt.Rows[i]["DType"].ToString().Trim().Equals("Upload")?"上传的文件":olddt.Rows[i]["DType"].ToString().Trim().Equals("ScienceWord")?"ScienceWord":"资源库文件";
							rw["DocType"]=0;
							//如果是上传的
							if(olddt.Rows[i]["DType"].ToString().Trim().Equals("Upload"))
							{
								System.IO.FileStream fs=new System.IO.FileStream(paths, System.IO.FileMode.Open, System.IO.FileAccess.Read);
								string fileextname=olddt.Rows[i]["FileExt"].ToString().Trim();
								string DEFAULT_CONTENT_TYPE = "application/unknown";
								RegistryKey regkey,fileextkey;
								string filecontenttype;
								try 
								{                
									regkey=Registry.ClassesRoot;                
									fileextkey=regkey.OpenSubKey(fileextname);                
									filecontenttype=fileextkey.GetValue("Content Type",DEFAULT_CONTENT_TYPE).ToString();
								}
								catch
								{
									filecontenttype=DEFAULT_CONTENT_TYPE;
								}       								
								rw["contenttype"]=filecontenttype;
								//*****************
								long fsn=fs.Length;
								byte[] buffer=new byte[fsn];
								int sn=0,en=10240;
								while(sn+en-10240<fs.Length)
								{
									if(fs.Length-sn>=10240)
										fs.Read(buffer,sn,en);
									else
										fs.Read(buffer,sn,(int)(fs.Length-sn-1));
									sn=sn+en;
								}
								fs.Close();
								rw["content"]=buffer;
								rw["sc"]=DBNull.Value;
							}
							//教案
							else if(olddt.Rows[i]["DType"].ToString().Trim().Equals("ScienceWord"))
							{
								rw["contenttype"]=DBNull.Value;
								rw["content"]=DBNull.Value;
								rw["sc"]=olddt.Rows[i]["content"];
							}
							//其他的
							else
							{
								rw["contenttype"]=DBNull.Value;
								rw["content"]=DBNull.Value;
								rw["sc"]=DBNull.Value;
							}
							newds2.Tables[0].Rows.Add(rw);
							newAdp2.Update(newds2);
							//////........./////////////////////
							//处理附件
							sql="select * from eAnnex where pid="+eid;
							System.Data.SqlClient.SqlDataAdapter oldAdpx=new SqlDataAdapter(sql,oldConn);
							System.Data.DataTable olddtx=new DataTable();
							oldAdpx.Fill(olddtx);
							if(olddtx.Rows.Count>0)
							{
								for(int ii=0;ii<olddtx.Rows.Count;ii++)
								{
									aid=olddtx.Rows[ii]["id"].ToString();
									filename=olddtx.Rows[ii]["filename"].ToString();
									phs=ph+"annex\\"+aid+"\\"+filename.Trim();
									if(System.IO.File.Exists(phs))
									{
										DataRow rw2=newds2.Tables[0].NewRow();
										rw2["DocID"]=olddtx.Rows[ii]["pid"];
										rw2["Title"]=olddtx.Rows[ii]["Title"];
										rw2["FileName"]=filename;
										rw2["FileExt"]=olddtx.Rows[ii]["FileExt"];
										rw2["FileSize"]=olddtx.Rows[ii]["FileSize"];

										rw2["DType"]="上传的文件";
										rw2["DocType"]=1;
										//是上传的
										System.IO.FileStream fs=new System.IO.FileStream(phs, System.IO.FileMode.Open, System.IO.FileAccess.Read);
										string fileextname=olddtx.Rows[ii]["FileExt"].ToString().Trim();
										string DEFAULT_CONTENT_TYPE = "application/unknown";
										RegistryKey regkey,fileextkey;
										string filecontenttype;
										try 
										{                
											regkey=Registry.ClassesRoot;                
											fileextkey=regkey.OpenSubKey(fileextname);                
											filecontenttype=fileextkey.GetValue("Content Type",DEFAULT_CONTENT_TYPE).ToString();
										}
										catch
										{
											filecontenttype=DEFAULT_CONTENT_TYPE;
										}       								
										rw2["contenttype"]=filecontenttype;
										//*****************
										long fsn=fs.Length;
										byte[] buffer=new byte[fsn];
										int sn=0,en=10240;
										while(sn+en-10240<fs.Length)
										{
											if(fs.Length-sn>=10240)
												fs.Read(buffer,sn,en);
											else
												fs.Read(buffer,sn,(int)(fs.Length-sn-1));
											sn=sn+en;
										}
										fs.Close();
										rw2["content"]=buffer;
										rw2["sc"]=DBNull.Value;

										newds2.Tables[0].Rows.Add(rw2);
										newAdp2.Update(newds2);
									}
								}
							}

						}
						Session["State"] = n;
					}
				}
			}
			catch(Exception ex)
			{
				Session["err"]="更新用户表失败,请检查zUsers表和数据时候正确!\n"+ex.Message;
				Session["State"] = 100;
				return;
			}
			//任务结束
			Session["err"]="";
			Session["State"] = 100;
			//errL.Text="操作完成!";
		}

		private string getPath(SqlConnection conn,string kid)
		{
			string ph=kid+"\\";
			string sql="select * from zKnowledge where kid="+kid;
			System.Data.DataTable dt=new DataTable();
			System.Data.SqlClient.SqlDataAdapter adp=new SqlDataAdapter(sql,conn);
			adp.Fill(dt);
			if(dt.Rows.Count>0)
			{
				string sid=dt.Rows[0]["sid"].ToString();
				string pid=dt.Rows[0]["pid"].ToString();
				while(pid!="0")
				{
					ph=pid+"\\"+ph;
					sql="select * from zKnowledge where kid="+pid;
					System.Data.DataTable dt2=new DataTable();
					System.Data.SqlClient.SqlDataAdapter adp2=new SqlDataAdapter(sql,conn);
					adp2.Fill(dt2);
					sid=dt2.Rows[0]["sid"].ToString();
					pid=dt2.Rows[0]["pid"].ToString();
				}
				sql="select * from zSubject where sid="+sid;
				System.Data.DataTable dt3=new DataTable();
				System.Data.SqlClient.SqlDataAdapter adp3=new SqlDataAdapter(sql,conn);
				adp3.Fill(dt3);
				sid=dt3.Rows[0]["sid"].ToString();
				string gid=dt3.Rows[0]["gid"].ToString();
				ph=sid+"\\"+ph;
				ph=gid+"\\"+ph;
			}

			return ph;

		}
		private void LongTask1()
		{
			//模拟长时间任务
			//每个循环模拟任务进行到不同的阶段
			for(int i=0;i<11;i++)
			{
				System.Threading.Thread.Sleep(1000);
				//设置每个阶段的state值,用来显示当前的进度
				Session["State"] = i+1;
			}
			//任务结束
			Session["State"] = 100;

		}


		public static void OpenProgressBar(System.Web.UI.Page Page)
		{
			StringBuilder sbScript = new StringBuilder();

			sbScript.Append("<script language='JavaScript' type='text/javascript'>\n");
			sbScript.Append("<!--\n");
			//需要IE5.5以上支持
			sbScript.Append("window.showModalDialog('Progress.aspx','','dialogHeight: 100px; dialogWidth: 350px; edge: Raised; center: Yes; help: No; resizable: No; status: No;scroll:No;');\n");
			//IE5.5以下使用window.open
			//sbScript.Append("window.open('Progress.aspx','', 'height=100, width=350, toolbar =no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');\n");
			sbScript.Append("// -->\n");
			sbScript.Append("</script>\n");

			Page.RegisterClientScriptBlock("OpenProgressBar", sbScript.ToString());
		}

		private void Button1_Click(object sender, System.EventArgs e)
		{
			errL.Text="";
			Session["err"]="";
			if(ph1.Text.Trim()=="")
			{
				errL.Text="请先指定资源的基础路径!";
				return;
			}
			System.Threading.Thread thread=new System.Threading.Thread(new System.Threading.ThreadStart(LongTask));
			thread.Start();

			Session["State"]=1;
			OpenProgressBar(this.Page);
			errL.Text=Session["err"].ToString();
			if(errL.Text=="")
				errL.Text="转换完成!";
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -