How to import Excel data into an SQL2008 database instance method _mssql2008

Source: Internet
Author: User
Tags bulk insert instance method

Copy Code code as follows:

private void Addmanydata_click (object sender, RoutedEventArgs e)
{
OpenFileDialog OpenFileDialog = new OpenFileDialog ();
Openfiledialog.filter = "Excel file |*.xls";

if ((bool) Openfiledialog.showdialog ())
{
FileInfo FileInfo = new FileInfo (openfiledialog.filename);
string filePath = Fileinfo.fullname;
String connexcel = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + FilePath + "; Extended Properties=excel 8.0 ";

using (OleDbConnection oledbconn = new OleDbConnection (Connexcel))
{
Oledbconn.open ();

Get Excel Table
DataTable dt = oledbconn.getoledbschematable (OleDbSchemaGuid.Tables, NULL);
Get table name for Excel table
String tablename = dt. ROWS[0][2]. ToString (). Trim ();
Remove Space
TableName = "[" + Tablename.replace ("'", "") + "]";

Use SQL statements to get data from Excel files
string query = @ "Select number, name, charitable labor, electronic process internship, operating system, computer composition, numerical analysis, network equipment and integration, dynamic website Development Experiment Week, dynamic website development, evenly divided, ranked from"; + tablename;
DataSet DataSet = new DataSet ();

using (OleDbCommand Oledbcomm = Oledbconn.createcommand ())
{
Oledbcomm.commandtext = query;
OleDbDataAdapter oleadapter = new OleDbDataAdapter (OLEDBCOMM);
Oleadapter.fill (DataSet);
}
String connstr = "Data source=heshuhua-pc;initial catalog=rsmsystem;integrated security=true";
Use SqlBulkCopy to BULK INSERT data
using (sqlbulkcopy SQLBC = new SqlBulkCopy (CONNSTR))
{
SQLBC. DestinationTableName = "T_stuscore";
SQLBC. Columnmappings.add ("School Number", "Stunum"), the first parameter corresponds to the column name in the database,
The second parameter corresponds to the column name of the corresponding table in the database
SQLBC. Columnmappings.add ("School Number", "stunum");
SQLBC. Columnmappings.add ("name", "Stuname");
SQLBC. Columnmappings.add ("Charitable labor", "activity");
SQLBC. Columnmappings.add ("Electronic Craft Practice", "elecact");
SQLBC. Columnmappings.add ("Operating System", "Oprationsystem");
SQLBC. Columnmappings.add ("Computer composition", "computermaded");
SQLBC. Columnmappings.add ("Numerical Analysis", "dataanalyze");
SQLBC. Columnmappings.add ("Network Equipment and Integration", "network");
SQLBC. Columnmappings.add ("Dynamic website Development Experiment Week", "Webweek");
SQLBC. Columnmappings.add ("Dynamic website development", "Webmake");
SQLBC. Columnmappings.add ("Evenly Divided", "Avscore");
SQLBC. Columnmappings.add ("Rank", "stupaiming");
SQLBC. WriteToServer (Dataset.tables[0]);
MessageBox.Show ("Data import successful!") ");

}
}

}

}

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.