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

📄 productimport.aspx.cs

📁 商业源码
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using System.Data.OleDb;
using System.IO;

namespace SCard.admin
{
	/// <summary>
	/// productImport 的摘要说明。
	/// </summary>
	public class productImport : System.Web.UI.Page
	{
        protected System.Web.UI.WebControls.Button btnLook;
        protected System.Web.UI.WebControls.Button btnInsert;
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
        protected System.Web.UI.HtmlControls.HtmlInputFile file1;
        protected System.Web.UI.WebControls.Label lblNum;
        protected System.Web.UI.WebControls.Label lblName;
    
		private void Page_Load(object sender, System.EventArgs e)
		{
            //权限检查
            if( Session["adminName"]==null || Session["adminName"].ToString() == String.Empty )
            {
                Response.Write("<font color=#ff0000 style='FONT-SIZE: 12px'>对不起,您没足够权限访问此页!!</font><br>");
                Response.Write("<a href=index.aspx target=_top style='FONT-SIZE: 12px'>重新登陆</a><br>");
                Response.End();
                return;
            }

            if( !IsPostBack )
            {
                if( Request.QueryString["id"] == null)
                {
                    Response.Write("没有这个商品");
                    Response.End();
                }
                string strID = Request.QueryString["id"].ToString().Trim();
                if( strID == String.Empty )
                {
                    strID = "-1";
                }

                //获取ID对应的商品名称
                DBConn myDB1 = new DBConn();
                string sqlP="select PName from Products where PID=" + strID;
                OleDbDataReader dr  = myDB1.getDataReader(sqlP);
            
                if( dr.Read() )
                {
                    lblName.Text = dr["PName"].ToString();
                }
                else
                {
                    Response.Write("没有这个商品");
                    dr.Close();
                    myDB1.Close();
                    Response.End();
                }
                dr.Close();
                myDB1.Close();

                ViewState["PID"] = strID;
            }
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
            this.btnLook.Click += new System.EventHandler(this.btnLook_Click);
            this.btnInsert.Click += new System.EventHandler(this.btnInsert_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
		#endregion

        private void btnLook_Click(object sender, System.EventArgs e)
        {
            string source = file1.Value;
            if( source.Trim() == String.Empty || Path.GetExtension(file1.PostedFile.FileName)!=".xls" )
            {
                Response.Write("<script>");
                Response.Write("alert('请选择要 查看 的文件!!!');");
                Response.Write("</script>");
                return;
            }
            string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + source + ";Extended Properties=Excel 8.0";
            string query = "Select * From [Sheet1$]";

            OleDbConnection conn = new OleDbConnection(ConnStr);
            OleDbDataAdapter ad = new OleDbDataAdapter(query,conn);
            DataSet ds = new DataSet();
            ad.Fill(ds,"Book1");
            DataGrid1.DataSource = ds.Tables["Book1"].DefaultView;
            DataGrid1.DataBind();
            ad.Dispose();
            conn.Close();
            conn=null;

            //-- 查看记录总数

            string mySql = "Select count(*) as [count] From [Sheet1$]";
            OleDbConnection myConn = new OleDbConnection(ConnStr);
            myConn.Open();
            OleDbCommand cmd = new OleDbCommand(mySql, myConn);
            OleDbDataReader mydr = cmd.ExecuteReader();
            if( mydr.Read() )
            {
                lblNum.Text = mydr["count"].ToString();
            }
            mydr.Close();
            myConn.Close();
            myConn=null;
        }

        private void btnInsert_Click(object sender, System.EventArgs e)
        {
            if( ViewState["PID"] == null )
            {
                Response.Write("<script>");
                Response.Write("alert('导入失败!!!');");
                Response.Write("</script>");
                return;
            }
            string strID = ViewState["PID"].ToString();

            string source = file1.Value;
            if( source.Trim() == String.Empty || Path.GetExtension(file1.PostedFile.FileName)!=".xls" )
            {
                Response.Write("<script>");
                Response.Write("alert('请选择要 导入 的文件!!!');");
                Response.Write("</script>");
                return;
            }

            string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + 
                source + ";Extended Properties=Excel 8.0";
            string query = "Select * From [Sheet1$]";

            OleDbConnection conn = new OleDbConnection( ConnStr );
            OleDbDataAdapter ad = new OleDbDataAdapter( query, conn );
            DataSet ds = new DataSet();
            ad.Fill( ds, "Book1" );
            ad.Dispose();
            conn.Close();
            conn=null;
            
            if( ds.Tables["Book1"].Rows.Count > 0 )
            {
                //加密对象
                DCard.Encode myEncode = new DCard.Encode();

                string sql="";
				int iNum = 0;

                DataTable dt = new DataTable();
                dt.Columns.Add("卡号");
                dt.Columns.Add("密码");
                dt = ds.Tables["Book1"];
                DataRow [] rows = dt.Select();
				
				DBConn myDB = new DBConn();
                foreach(DataRow dr in rows)
                {
                    sql = "insert into PList(PID,CardNum,CardPassword) values(" + strID + ",'" + dr[0].ToString() + "','" + myEncode.EncryptString(dr[1].ToString()) + "')";
					iNum += myDB.ExecuteNonQuery( sql );
                }
                
                myDB.Close();

                DBConn myDB2 = new DBConn();
                string mySql = "update Products set PStock=PStock+" + iNum + " where PID=" + strID;
                myDB2.ExecuteNonQuery( mySql );
                myDB2.Close();

                Response.Write("<script>");
                Response.Write("alert('成功导入[ " + iNum + "条 ]数据!!!');");
                Response.Write("</script>");

            }
            else
            {
                Response.Write("<script>");
                Response.Write("alert('表里没记录,导入失败!!!');");
                Response.Write("</script>");
                return;
            }

        }


	}
}

⌨️ 快捷键说明

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