Asp. NET down to SQLServer2008 import file instance operation method _mssql

Source: Internet
Author: User
Tags xsl create database

ASP. NET importing files to SQL Server mainly uses the Filebytes property of the FileUpload control. This property returns a byte array from the file specified by the FileUpload control.
1. Database Preparation
for the convenience of everyone to understand, here we only design two fields, one is the file Type field, the field name is filetype, the other is the file Content field, the field name is Filecontent. Create the database

, the database name is Varfile, and the statement is as follows:
Create DATABASE varfile
Go
creates the table named Fileinout with the following statement:
Use varfile
Go Br>create TABLE Fileintou
(
FileType nvarchar () not NULL,
Filecontent varbinary (max) null
)
2. Add control
run VS2008 and create a new Web site, add a FileUpload control to the page Default.aspx, ID To FileUpload1. Add three button buttons at the same time, with IDs Fileup and Fileload respectively. The Text property is set to "Upload Files" and "Download Files", respectively.

3. Add code

(1) adds a namespace because it is connected to a SQL Server database, so add the using System.Data.Sqlclient and using System.Data namespaces. Also because the HTTP character set for the output stream is "gb2312" character encoding, the using System.Text namespace is added. Also, because the export file is strongly typed as a string, the using System.Collections.Specialized namespace is added.

(2) Adds the event code for the "Upload file" button. When you click the Upload file button, get the file type of the file selected by the FileUpload control and the byte array of the file into the database. Switch to Design view, double-click the "Upload File" button, add the "Upload File" button event code, the following code:

Copy Code code as follows:

protected void Fileup_click (Object Sender,eventargs e)
{
if (fileupload1.filename==string. Empty)
{
Response.Write ("Please select the file to upload" </script>);
Return
}
String Mailto:connstr=@%22data source=69f638102711447\sql2008;initial catalog=varfile;integrated Security=Ture "; Database connection string
String the Selected=fileupload1.filename; Get the suffix name of the uploaded file
String extension=theselected.substring (Theselected.lastindexof (".")). ToLower ();
if (checkfiletype (extension))//If the specified file type is available
{

String contenttype=getcontenttype (extension);
String sqlstr= "insert into fileinout values (@FileType, @FileCount)"; SQL statement for uploading files
String sqlstrclear= "TRUNCATE table Fileinout"; Empty Database SQL statements
SqlConnection con=new SqlConnection (ConnStr); Instantiating a database Connection object
SqlCommand cmd=new SqlCommand (Sqlstr,con); Instantiate upload file SQL command
SqlCommand cmdclear=new SqlCommand (Sqlstrclear,con); Instantiate empty Database SQL command
Define ask Price type parameters
Cmd. Parameters.Add (New SqlParameter ("@FileType", slqdbtype.nvarchar,30));
Cmd. parameters["@FileType"]. Value=contenttype; Define file content Parameters
Cmd. Parameters.Add (New SqlParameter ("@FileCount", sqldbtype.nvarchar,30)); Converts a file to a byte array as a @filecount value
Cmd. parameters["@FileCount"]. Value=fileupload1.filebytes;
Con. Open ();
Cmdclear. ExecuteNonQuery (); To perform an empty database command
Cmd. ExecuteNonQuery (); Execute upload File command
}
}

(3) Add a function method that obtains the file type and obtains the file export way. First, see if the type of file you want to upload is within the specified price type, and if so, you can import the file directly, and then depending on the file type

Get this file out of the way and place it in the FileType field with the following code:
Copy Code code as follows:

public static bool Checkfiletype (String type)
{
StringDictionary sd=new stringdictionary (); Materialized collection StringDictionary class
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); Determine if StringDictionary contains a specific key
}

public static string getContentType (String extension)//Get Output file Way
{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]; Returns the value of the corresponding key
}

(4) Upload files, select a PDF file, click the "Upload File" button, open the Database Fileinout table, as shown in the figure can be seen.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.