Silverlight4.0上传Excel文件并将数据插入到SQL数据库中
环境:vs2010+SQL2008+ Silverlight4.0(当然,SQL2005也行)
步骤:
准备工作:
首先在SQL数据库中建立一张表用于要插入的数据存放,例如我是建立的xueji表,四个字段,xm,xb,xh,bc;然后建立一个excel2003版本的表
如下图:
1、 在MainPage.xaml中添加三个控件:两个按钮控件Button,一个列表控件ListBox
如图:
2、 在MainPage.xaml中输入如下引用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
在 public partial class MainPage : UserControl
{
//在此先定义一个List;
List<FileInfo> filesToUpload;
//在“选择本地Excel文件”按钮单击事件中输入如下代码:
private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Multiselect = true;
fileDialog.Filter = "All files (*.*)|*.*|Jpg files (*.jpg)|*.jpg|Word Files(*.docx)|*.docx";
bool? result = fileDialog.ShowDialog();
if (result != null)
{
filesToUpload = fileDialog.Files.ToList();
listBox1.ItemsSource = filesToUpload;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//并添加如下方法:
private void UploadFileData(Stream inputFile, Stream resultFile)
{
byte[] fileData = new byte[4096];
int fileDataToRead;
while ((fileDataToRead = inputFile.Read(fileData, 0, fileData.Length)) != 0)
{
resultFile.Write(fileData, 0, fileDataToRead);
}
}
//在“确定上传”按钮单击事件中输入如下代码:
private void OKButton_Click(object sender, RoutedEventArgs e)
{
try
{
foreach (FileInfo file in filesToUpload)
{
//Define the Url object for the Handler
UriBuilder handlerUrl = new UriBuilder("localhost:3244/UploadFileHandler.ashx");//请注意这里要改成你自己的端口哦
//Set the QueryString
handlerUrl.Query = "InputFile=" + file.Name;
FileStream FsInputFile = file.OpenRead();
//Define the WebClient for Uploading the Data
WebClient webClient = new WebClient();
//Now make an async class for writing the file to the server
//Here I am using Lambda Expression
webClient.OpenWriteCompleted += (s, evt) =>
{
UploadFileData(FsInputFile, evt.Result);
evt.Result.Close();
FsInputFile.Close();
};
webClient.OpenWriteAsync(handlerUrl.Uri);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
3、 在网站根目录下建立一个FilesServer文件夹用于存放Excel文件;下面是我做的一个测试系统的界面示意图
4、 在根目录中新建UploadFileHandler.ashx文件;如下图;
5、 在UploadFileHandler.ashx.cs中输入如下代码:
using System.Web;
using System.IO;
using System.Data.OleDb;
using System.Data;
usingexcel连接sql数据库教程 System.Data.SqlClient;
namespace xueji_kaoshi.Web
{
/// <summary>
/// UploadFileHandler的摘要说明
/// </summary>
public class UploadFileHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string filename = context.Request.QueryString["InputFile"].ToString();
using (FileStream fileStream = File.Create(context.Server.MapPath("~/FilesServer/" + filename)))
{
byte[] bufferData = new byte[4096];
int bytesToBeRead;
while ((bytesToBeRead = context.Request.InputStream.Read(bufferData, 0, bufferData.Length)) != 0)
{
fileStream.Write(bufferData, 0, bytesToBeRead);
}
fileStream.Close();
}
//===========用于对上传的EXCEL文件插入到SQL数据库中===============
string strPath = context.Server.MapPath("~/FilesServer/" + filename);
string mystring = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '" + strPath + "';Extended Properties=Excel 8.0";
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论