ASP.NET下向SQLServer2008匯入檔案執行個體操作方法_MsSql

來源:互聯網
上載者:User

ASP.NET向SQL Server匯入檔案主要用到FileUpload控制項的FileBytes屬性。該屬性從FileUpload控制項所指定的檔案返回一個位元組數組 。
1.資料庫準備
為了方便大家能夠理解,這裡我們只設計兩個欄位,一個是檔案類型欄位,欄位名為FileType,另一個是存放檔案內容欄位,欄位名為FileContent。建立資料庫

,資料庫名為VarFile,語句如下:
CREATE DATABASE VARFILE
GO
建立表,表名為FileInOut,語句如下:
USE VARFILE
GO
CREATE TABLE FILEINTOU
(
FileType nvarchar(30) not null,
FileContent varbinary(max) null
)
2.添加控制項
運行VS2008並建立一個網站,在頁面Default.aspx中添加一個FileUpload控制項,ID 為FileUpload1.同時添加三個Button按鈕,ID分別為fileUp和fileLoad。Text屬性分別設定為“上傳檔案”和“下載檔案”。

3.添加代碼

(1)添加命名空間,因為和SQL Server資料庫連接,所以添加using System.Data.Sqlclient和using System.Data命名空間。又因為要設定輸出資料流的HTTP的字元集為"gb2312"字元編碼,所以添加using System.Text命名空間。同時又因為要把匯出檔案強型別化為字串,所以添加using System.Collections.Specialized命名空間。

(2)添加“上傳檔案”按鈕的事件代碼。當單擊“上傳檔案”按鈕後,擷取FileUpload控制項所選擇的檔案的檔案類型以及檔案的位元組數組插入資料庫中。切換到設計檢視,雙擊“上傳檔案”按鈕,添加"上傳檔案"按鈕事件代碼,代碼如下:

複製代碼 代碼如下:

protected void fileUp_Click(object sender,EventArgs e)
{
if(FileUpload1.FileName==string.Empty)
{
Response.Write("<script>altert(‘請選擇要上傳的檔案')</script>");
return;
}
string mailto:connstr=@%22Data Source=69F638102711447\SQL2008;Initial Catalog=VarFile;Integrated Security=Ture"; //資料庫連接字串
string the Selected=FileUpload1.FileName; //擷取上傳檔案的尾碼名
string extension=theSelected.Substring(theSelected.LastIndexOf(".")).ToLower();
if(CheckFileType(extension)) //如果有指定的檔案類型
{

string contentType=GetContentType(extension);
string sqlstr="insert into FileInOut values(@FileType,@FileCount)"; //上傳檔案的SQL語句
string sqlstrclear="truncate table FileInOut"; //清空資料庫SQL語句
SqlConnection con=new SqlConnection(connstr); //執行個體化資料庫連接對象
SqlCommand cmd=new SqlCommand(sqlstr,con); //執行個體化上傳檔案SQL命令
SqlCommand cmdclear=new SqlCommand(sqlstrclear,con); //執行個體化清空資料庫SQL命令
//定義問價型別參數
cmd.Parameters.Add(new SqlParameter("@FileType”,SlqDbType.NvarChar,30));
cmd.Parameters["@FileType"].Value=contentType; //定義檔案內容參數
cmd.Parameters.Add(new SqlParameter("@FileCount",SqlDbType.NVarChar,30)); //將檔案轉化為位元組數組作為@FileCount的值
cmd.Parameters["@FileCount"].Value=FileUpload1.FileBytes;
con.Open();
cmdclear.ExecuteNonQuery(); //執行清空資料庫命令
cmd.ExecuteNonQuery(); //執行上傳檔案命令
}
}

(3)添加擷取檔案類型和獲得檔案匯出方式的函數方法。首先查看所要上傳檔案類型是否在指定問價類型內,如果在,則可以直接匯入檔案,然後根據檔案類型

擷取此檔案匯出方式並存放在FileType欄位中,代碼如下:
複製代碼 代碼如下:

public static bool CheckFileType(string type)
{
StringDictionary sd=new StringDictionary(); //執行個體化集合StringDictionary類
sd.Add(".doc","application/msword");
sd.Add(".ppt","application/vnd.ms-powerpoint");
sd.Add(".xsl","application/vnd.ms-excel");
sd.Add(".rtf","application/msword");
sd.Add(".html","text/html");
sd.Add(".htm","text/html");
sd.Add(".txt","text/plain");
sd.Add(".pdf","application/pdf");
return sd.ContainsKey(type); //確定StringDictionary是否包含特定鍵
}

public static string GetContentType(string extension) //擷取輸出檔案方式
{StringDictionary sd=new StringDictionary();
sd.Add(".doc","application/msword");
sd.Add(".ppt","application/vnd.ms-powerpoint");
sd.Add(".xsl","application/vnd.ms-excel");
sd.Add(".rtf","application/msword");
sd.Add(".html","text/html");
sd.Add(".htm","text/html");
sd.Add(".txt","text/plain");
sd.Add(".pdf","application/pdf");
return sd[extension]; //返回對應鍵的值
}

(4)上傳檔案,選擇一個pdf檔案,單擊"上傳檔案"按鈕後,開啟資料庫中的FileInOut表,如圖所示可以看到。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.