W
wybkl88
Unregistered / Unconfirmed
GUEST, unregistred user!
这是我写的将excel表上传到数据库的程序。
Response.aspx代码:
<%@ Import namespace="System.Data.OleDb"%>
<%@ Import namespace="System.Data"%>
<%@ Import namespace="System.IO"%>
<%@ Page language="c#" Codebehind="Response.aspx.cs" AutoEventWireup="false" Inherits="education.Response" debug="true"%>
<HTML>
<HEAD>
<title>Reading from an Excel Workbook</title>
</HEAD>
<body>
<H3>将 excel 表上传到数据库</H3>
<form id="uploderform" method="post" encType="multipart/form-data" runat="server">
<asp:Label ID="label2" Runat="server" ForeColor=#ff0000></asp:Label>
<asp:Button ID="button3" Visible=False Runat="server" Text="是" OnClick="btn3_Click"></asp:Button>
<asp:Button ID="button4" Visible=False Runat="server" Text="否" OnClick="btn4_Click"></asp:Button>
<table cellSpacing=2 cellPadding=2 border=1>
<tr>
<td>
<h5>选择要上传的文件:</h5></td< tr>
<tr>
<td><input id="myFile" type="file" name="myFile" runat="server"> </td></tr>
<tr>
<td><input id="Button1" type="button" value="上 传" name="Button1" runat="server" OnServerClick="UploadFile">
</td></tr></table></form><br>
<table cellSpacing=2 border=1>
<tr>
<td><b>文件资料</b></td>
<td>&nbsp;</td></tr>
<tr>
<td>文件名 :</td>
<td><asp:label id=fname runat="server" text=""></asp:label></td></tr>
<tr>
<td>文件类型 :</td>
<td><asp:label id=fenc runat="server"></asp:label></td></tr>
<tr>
<td>文件大小 in bytes)</td>
<td><asp:label id=fsize runat="server"></asp:label></td></tr>
</table><br><br><asp:label id=label1 Runat="server"></asp:label>
</FORM>
</body>
</HTML>
Response.aspx.cs的代码:
using System;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using System.ComponentModel;
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.IO;
using System.Data.SqlClient;
namespace education
{
/// <summary>
/// Response 的摘要说明。
/// </summary>
public class Response : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile myFile;
protected System.Web.UI.HtmlControls.HtmlInputButton Button1;
protected System.Web.UI.WebControls.Label fname;
protected System.Web.UI.WebControls.Label fenc;
protected System.Web.UI.WebControls.Label fsize;
protected System.Web.UI.WebControls.Label label1;
protected System.Web.UI.WebControls.Label label2;
protected System.Web.UI.WebControls.Button button3;
protected System.Web.UI.WebControls.Button button4;
protected System.Web.UI.HtmlControls.HtmlGenericControl display;
OleDbDataAdapter objAdapter;
DataSet objDataSet;
string name;
System.Web.HttpPostedFile PostedFile;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
/// <summary>
/// 上传文件的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="E"></param>
public void UploadFile(object sender , EventArgs E)
{
//检查上传文件不为空
if(myFile.PostedFile!=null)
{
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
PostedFile = files[0];
name = System.IO.Path.GetFileName(PostedFile.FileName);
//取得文件名(抱括路径)里最后一个"."的索引
int i= name.LastIndexOf(".");
//取得文件扩展名
string newext =name.Substring(i);
if(newext==".xls"||newext==".XLS")
{
label2.Text="你想将它上传到数据库吗?";
button3.Visible=true;
button4.Visible=true;
fname.Text=myFile.PostedFile.FileName;
fenc.Text=myFile.PostedFile.ContentType ;
fsize.Text=myFile.PostedFile.ContentLength.ToString();
}
else
{
//保存文件到你所要的目录,这里是IIS根目录下的upload目录.你可以改变.
//注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里"/"必须用"//"代替
myFile.PostedFile.SaveAs(Server.MapPath("//upload//"+name));
//得到这个文件的相关属性:文件名,文件类型,文件大小
fname.Text=myFile.PostedFile.FileName;
fenc.Text=myFile.PostedFile.ContentType ;
fsize.Text=myFile.PostedFile.ContentLength.ToString();
}
}
}
/// <summary>
/// 将DataSet数据插入到数据库的UserTable表中
/// </summary>
public void InsertData()
{
label1.Text="I'm very glad!";
String strSQL="INSERT INTO UserTable(UserName,Pass,UserLevel,Forbid,Factory,JoinTime,Sex,Email,RealName,Message) VALUES (@UserName,@Pass,@UserLevel,@Forbid,@Factory,@JoinTime,@Sex,@Email,@RealName,@Message);";
String strConnection = "server=AQ;";
strConnection +="database=gaojiao;uid=sa;password=021302078;";
SqlConnection objConnection = new SqlConnection(strConnection);
foreach(DataRow r in objDataSet.Tables["dtCategories"].Rows)
{
SqlCommand dbComm = new SqlCommand(strSQL,objConnection);
dbComm.Parameters.Add("@UserName",SqlDbType.NVarChar,20);
dbComm.Parameters.Add("@Pass",SqlDbType.NVarChar,50);
dbComm.Parameters.Add("@UserLevel",SqlDbType.NVarChar,50);
dbComm.Parameters.Add("@Forbid",SqlDbType.NVarChar,10);
dbComm.Parameters.Add("@Factory",SqlDbType.NVarChar,50);
dbComm.Parameters.Add("@JoinTime",SqlDbType.DateTime,8);
dbComm.Parameters.Add("@Sex",SqlDbType.NVarChar,10);
dbComm.Parameters.Add("@Email",SqlDbType.NVarChar,80);
dbComm.Parameters.Add("@RealName",SqlDbType.NVarChar,50);
dbComm.Parameters.Add("@Message",SqlDbType.NVarChar,200);
dbComm.Parameters["@UserName"].Value=r["UserName"];
dbComm.Parameters["@Pass"].Value=r["Pass"];
dbComm.Parameters["@UserLevel"].Value=r["UserLevel"];
dbComm.Parameters["@Forbid"].Value=r["Forbid"];
dbComm.Parameters["@Factory"].Value=r["Factory"];
dbComm.Parameters["@JoinTime"].Value=r["JoinTime"];
dbComm.Parameters["@Sex"].Value=r["Sex"];
dbComm.Parameters["@Email"].Value=r["Email"];
dbComm.Parameters["@RealName"].Value=r["RealName"];
dbComm.Parameters["@Message"].Value=r["Message"];
try
{
objConnection.Open();
dbComm.ExecuteScalar();
}
catch(Exception ex)
{
Response.Write(ex.Message);
Response.End();
}
finally
{
if(objConnection.State==ConnectionState.Open)
{
objConnection.Close();
}
}
}
label1.Text="I'm very glad!";
}
/// <summary>
/// 处理用户决定将excel表插入到数据库中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void btn3_Click(object sender, System.EventArgs e)
{
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
PostedFile = files[0];
String strConnection="Provider=Microsoft.Jet.OleDb.4.0;";
strConnection +=@"data source=myFile.PostedFile;";
strConnection +=@"Extended Properties=Excel 8.0;";
OleDbConnection objConnection=new OleDbConnection(strConnection);
String strSQL="SELECT * FROM Items";
objAdapter=new OleDbDataAdapter(strSQL,objConnection);
objDataSet=new DataSet();
objAdapter.Fill(objDataSet,"dtCategories");
InsertData();
}
/// <summary>
/// 处理用户不将EXCEL表插入到数据库中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void btn4_Click(object sender, System.EventArgs e)
{
myFile.PostedFile.SaveAs(Server.MapPath("//upload//"+name));
}
}
}
程序错误:如果决定将表传到数据库,则:System.Data.OleDb.OleDbException: 不能更新。数据库或对象为只读
如果不将表传入数据库,则:System.IO.DirectoryNotFoundException: 未找到路径“F:/新建文件夹/education/upload/”的一部分。
Response.aspx代码:
<%@ Import namespace="System.Data.OleDb"%>
<%@ Import namespace="System.Data"%>
<%@ Import namespace="System.IO"%>
<%@ Page language="c#" Codebehind="Response.aspx.cs" AutoEventWireup="false" Inherits="education.Response" debug="true"%>
<HTML>
<HEAD>
<title>Reading from an Excel Workbook</title>
</HEAD>
<body>
<H3>将 excel 表上传到数据库</H3>
<form id="uploderform" method="post" encType="multipart/form-data" runat="server">
<asp:Label ID="label2" Runat="server" ForeColor=#ff0000></asp:Label>
<asp:Button ID="button3" Visible=False Runat="server" Text="是" OnClick="btn3_Click"></asp:Button>
<asp:Button ID="button4" Visible=False Runat="server" Text="否" OnClick="btn4_Click"></asp:Button>
<table cellSpacing=2 cellPadding=2 border=1>
<tr>
<td>
<h5>选择要上传的文件:</h5></td< tr>
<tr>
<td><input id="myFile" type="file" name="myFile" runat="server"> </td></tr>
<tr>
<td><input id="Button1" type="button" value="上 传" name="Button1" runat="server" OnServerClick="UploadFile">
</td></tr></table></form><br>
<table cellSpacing=2 border=1>
<tr>
<td><b>文件资料</b></td>
<td>&nbsp;</td></tr>
<tr>
<td>文件名 :</td>
<td><asp:label id=fname runat="server" text=""></asp:label></td></tr>
<tr>
<td>文件类型 :</td>
<td><asp:label id=fenc runat="server"></asp:label></td></tr>
<tr>
<td>文件大小 in bytes)</td>
<td><asp:label id=fsize runat="server"></asp:label></td></tr>
</table><br><br><asp:label id=label1 Runat="server"></asp:label>
</FORM>
</body>
</HTML>
Response.aspx.cs的代码:
using System;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using System.ComponentModel;
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.IO;
using System.Data.SqlClient;
namespace education
{
/// <summary>
/// Response 的摘要说明。
/// </summary>
public class Response : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile myFile;
protected System.Web.UI.HtmlControls.HtmlInputButton Button1;
protected System.Web.UI.WebControls.Label fname;
protected System.Web.UI.WebControls.Label fenc;
protected System.Web.UI.WebControls.Label fsize;
protected System.Web.UI.WebControls.Label label1;
protected System.Web.UI.WebControls.Label label2;
protected System.Web.UI.WebControls.Button button3;
protected System.Web.UI.WebControls.Button button4;
protected System.Web.UI.HtmlControls.HtmlGenericControl display;
OleDbDataAdapter objAdapter;
DataSet objDataSet;
string name;
System.Web.HttpPostedFile PostedFile;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
/// <summary>
/// 上传文件的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="E"></param>
public void UploadFile(object sender , EventArgs E)
{
//检查上传文件不为空
if(myFile.PostedFile!=null)
{
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
PostedFile = files[0];
name = System.IO.Path.GetFileName(PostedFile.FileName);
//取得文件名(抱括路径)里最后一个"."的索引
int i= name.LastIndexOf(".");
//取得文件扩展名
string newext =name.Substring(i);
if(newext==".xls"||newext==".XLS")
{
label2.Text="你想将它上传到数据库吗?";
button3.Visible=true;
button4.Visible=true;
fname.Text=myFile.PostedFile.FileName;
fenc.Text=myFile.PostedFile.ContentType ;
fsize.Text=myFile.PostedFile.ContentLength.ToString();
}
else
{
//保存文件到你所要的目录,这里是IIS根目录下的upload目录.你可以改变.
//注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里"/"必须用"//"代替
myFile.PostedFile.SaveAs(Server.MapPath("//upload//"+name));
//得到这个文件的相关属性:文件名,文件类型,文件大小
fname.Text=myFile.PostedFile.FileName;
fenc.Text=myFile.PostedFile.ContentType ;
fsize.Text=myFile.PostedFile.ContentLength.ToString();
}
}
}
/// <summary>
/// 将DataSet数据插入到数据库的UserTable表中
/// </summary>
public void InsertData()
{
label1.Text="I'm very glad!";
String strSQL="INSERT INTO UserTable(UserName,Pass,UserLevel,Forbid,Factory,JoinTime,Sex,Email,RealName,Message) VALUES (@UserName,@Pass,@UserLevel,@Forbid,@Factory,@JoinTime,@Sex,@Email,@RealName,@Message);";
String strConnection = "server=AQ;";
strConnection +="database=gaojiao;uid=sa;password=021302078;";
SqlConnection objConnection = new SqlConnection(strConnection);
foreach(DataRow r in objDataSet.Tables["dtCategories"].Rows)
{
SqlCommand dbComm = new SqlCommand(strSQL,objConnection);
dbComm.Parameters.Add("@UserName",SqlDbType.NVarChar,20);
dbComm.Parameters.Add("@Pass",SqlDbType.NVarChar,50);
dbComm.Parameters.Add("@UserLevel",SqlDbType.NVarChar,50);
dbComm.Parameters.Add("@Forbid",SqlDbType.NVarChar,10);
dbComm.Parameters.Add("@Factory",SqlDbType.NVarChar,50);
dbComm.Parameters.Add("@JoinTime",SqlDbType.DateTime,8);
dbComm.Parameters.Add("@Sex",SqlDbType.NVarChar,10);
dbComm.Parameters.Add("@Email",SqlDbType.NVarChar,80);
dbComm.Parameters.Add("@RealName",SqlDbType.NVarChar,50);
dbComm.Parameters.Add("@Message",SqlDbType.NVarChar,200);
dbComm.Parameters["@UserName"].Value=r["UserName"];
dbComm.Parameters["@Pass"].Value=r["Pass"];
dbComm.Parameters["@UserLevel"].Value=r["UserLevel"];
dbComm.Parameters["@Forbid"].Value=r["Forbid"];
dbComm.Parameters["@Factory"].Value=r["Factory"];
dbComm.Parameters["@JoinTime"].Value=r["JoinTime"];
dbComm.Parameters["@Sex"].Value=r["Sex"];
dbComm.Parameters["@Email"].Value=r["Email"];
dbComm.Parameters["@RealName"].Value=r["RealName"];
dbComm.Parameters["@Message"].Value=r["Message"];
try
{
objConnection.Open();
dbComm.ExecuteScalar();
}
catch(Exception ex)
{
Response.Write(ex.Message);
Response.End();
}
finally
{
if(objConnection.State==ConnectionState.Open)
{
objConnection.Close();
}
}
}
label1.Text="I'm very glad!";
}
/// <summary>
/// 处理用户决定将excel表插入到数据库中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void btn3_Click(object sender, System.EventArgs e)
{
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
PostedFile = files[0];
String strConnection="Provider=Microsoft.Jet.OleDb.4.0;";
strConnection +=@"data source=myFile.PostedFile;";
strConnection +=@"Extended Properties=Excel 8.0;";
OleDbConnection objConnection=new OleDbConnection(strConnection);
String strSQL="SELECT * FROM Items";
objAdapter=new OleDbDataAdapter(strSQL,objConnection);
objDataSet=new DataSet();
objAdapter.Fill(objDataSet,"dtCategories");
InsertData();
}
/// <summary>
/// 处理用户不将EXCEL表插入到数据库中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void btn4_Click(object sender, System.EventArgs e)
{
myFile.PostedFile.SaveAs(Server.MapPath("//upload//"+name));
}
}
}
程序错误:如果决定将表传到数据库,则:System.Data.OleDb.OleDbException: 不能更新。数据库或对象为只读
如果不将表传入数据库,则:System.IO.DirectoryNotFoundException: 未找到路径“F:/新建文件夹/education/upload/”的一部分。