BasicImportDB.aspx.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.OleDb;
  9. using System.Data.Sql;
  10. using System.Data.SqlClient;
  11. public partial class Admin_BasicImportDB : System.Web.UI.Page
  12. {
  13. string filename;
  14. LYS.UserData1 myud = new LYS.UserData1();
  15. //先要确定三个参数:表名: Session["ImportTable"] = "Student"; 两个关键字段 Session["KeyField1"] = "StudentID"; Session["KeyField2"] = "Stud_name";
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18. Session["ImportTable"] = "UserS";
  19. Session["KeyField1"] = "用户名";
  20. Session["KeyField2"] = "密码";
  21. if (Session["ImportTable"] == null || Session["KeyField1"] == null || Session["KeyField2"] == null)
  22. {
  23. Response.Write("<script> alert('程序非法调用!!') </script>");
  24. Response.End();
  25. }
  26. Panel2.Visible = false;
  27. Panel3.Visible = false;
  28. Panel4.Visible = false;
  29. }
  30. protected void input_Click(object sender, EventArgs e)
  31. {
  32. Panel1.Visible = false;
  33. Panel2.Visible = true;
  34. if (File1.PostedFile != null)
  35. {
  36. filename = File1.FileName;
  37. Session["file"] = filename;
  38. Session["conn"] = myud.ConnExcelString("file", Session["file"].ToString());
  39. File1.PostedFile.SaveAs(Server.MapPath("file/") + filename);
  40. //myud.GetSQLTableName(myud.ConnString(), DropDownList1); 获得获得SQL表名
  41. myud.GetExcelTable(Session["conn"].ToString(), DropDownList1);
  42. }
  43. }
  44. protected void Button1_Click(object sender, EventArgs e)
  45. {
  46. Panel1.Visible = false;
  47. Panel3.Visible = true;
  48. string Sql = "select ";
  49. for (int i = 0; i < CheckBoxList1.Items.Count; i++)
  50. if (CheckBoxList1.Items[i].Selected)
  51. Sql += CheckBoxList1.Items[i].Value + ",";
  52. Sql = Sql.Substring(0, Sql.Length - 1) + " from [" + DropDownList1.SelectedValue + "]"; //EXCEL SQL 语句
  53. // Response.Write(Sql);
  54. Session["getsql"] = Sql;
  55. //Response.Write(Sql);
  56. Literal1.Text = myud.DispOleDbLineDataJTExcel(Sql, Session["conn"].ToString()).ToString(); //显示EXCEL数据
  57. }
  58. protected void Button2_Click(object sender, EventArgs e)
  59. {
  60. Panel4.Visible = true;
  61. string Sql = Session["getsql"].ToString();
  62. DataTable myTable = myud.getExcelds(Session["conn"].ToString(), Sql);
  63. // myud.ExcelToSQL(tablename, Session["conn"].ToString(), myTable);
  64. myud.ExcelToSQLKey(Session["ImportTable"].ToString(), Session["conn"].ToString(), myTable, Session["KeyField1"].ToString(), Session["KeyField2"].ToString(), Literal3); //不同的表需要更改两个要检查的关键字段名StudentID Stud_name
  65. System.IO.File.Delete(Server.MapPath("file/") + Session["file"].ToString());
  66. // Response.Write("<script> alert('导入成功') </script>");
  67. Literal2.Text = myud.DispLineDataModi("Select Top 10 * from " + Session["ImportTable"].ToString() + " order by ID DESC", "").ToString();
  68. }
  69. protected void Button3_Click(object sender, EventArgs e)
  70. {
  71. Panel2.Visible = true;
  72. Button1.Visible = true;
  73. CheckBoxList1.Items.Clear();
  74. string tablename = DropDownList1.SelectedValue;
  75. string sql = "select * from [" + tablename + "]";
  76. DataTable myTable = myud.getExcelds(Session["conn"].ToString(), sql);
  77. for (int i = 0; i <= myTable.Columns.Count - 1; i++)
  78. {
  79. ListItem myit = new ListItem();
  80. myit.Text = myTable.Columns[i].Caption;
  81. myit.Value = myTable.Columns[i].Caption;
  82. CheckBoxList1.Items.Add(myit);
  83. }
  84. }
  85. }