Turn: Asp.Net import Excel data into Sql Server database

Asp.Net find an example import Excel data into Sql Server database on the Internet, is to call a stored procedure, share.

I just used, I feel okay. The basic idea is to create a new page, put a HTML control File FIled control, and then this control into a server control, add a button (server control) is used to get the contents of the File FIled, the next step is programmed in the background , first introduced two namespaces, using System.data.oledb driving and using system.data.sqlclint oledb wherein the space for providing Excel, sqlclint sql server 2000 used to connect with the next button can be put in the following events boost code on the line, but also on the process of writing your own store Oh!

Introduction: cmd.CommandText = "Proc_Address"; this sentence is stored procedure call "Proc_Address"
The code for the stored procedure:
the CREATE Proc Proc_Address @CardNo char (20 is), @ ToAddress char (50), @ CCAddress char (50), char @YYYYMM (12 is)
AS
DECLARE @strSql char (400)

SET @strSql = 'the Insert INTO the EmailAddress' + + @YYYYMM' values ( '' + @ + CardNo '', '' ToAddress @ + + '', '' + @ CCAddress + '', '' + '1') '- to insert a record
exec (@strSql) - can also be used statement insert into
the GO



Private void the Button1_Click (Object SENDER, System.EventArgs E)
{
the CreateTable ();

// EXCEL first imported into the database, a: first introducing EXCEL dateView, II:Then import the data into a database in dateView inside

// EXCEL connection string
string sConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; " +
"Data Source=" + File1.PostedFile.FileName.ToString() + ";" +
"Extended Properties=Excel 8.0;";

//建立EXCEL的连接
OleDbConnection objConn = new OleDbConnection(sConnectionString);

objConn.Open();

OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;

DataSet objDataset1 = new DataSet();

objAdapter1.Fill(objDataset1, "XLData";

// DataGrid1.DataSource = objDataset1.Tables[0].DefaultView; //测试代码,用来测试是否能读出EXCEL上面的数据
// DataGrid1.DataBind();

DataTable dt = objDataset1.Tables[0];
DataView myView = new DataView(dt);

// SQL SERVER database connection
Conn the SqlConnection;
String System.Configuration.ConfigurationSettings.AppSettings DNS = [ "Constr"]; // connection string
Conn the SqlConnection new new = (DNS);

the SqlCommand cmd = conn.CreateCommand ();
cmd.CommandType = CommandType.StoredProcedure;
cmd. = the CommandText "Proc_Address";

int COUNT = 0; // record the number of errors for

the try
{
the foreach (mydrv as in the DataRowView myView)
{
COUNT ++;
// close to the last connection to SQL Server
if (conn.State.ToString () = "Closed"!
conn.Close ();

// every time we emptied all the parameters of the CMD
cmd.Parameters.Clear ();

// execute a stored procedure
// obtain the parameters of a total of 3
// @ CardNo , ToAddress @, @ CCAddress
the SqlParameter paraCardNo = cmd.Parameters.Add ( "@ CardNo", SqlDbType.Char);
SqlParameter paraToAddress =cmd.Parameters.Add("@ToAddress",SqlDbType.Char);
SqlParameter paraCCAddress =cmd.Parameters.Add("@CCAddress",SqlDbType.Char);
SqlParameter paraYYYYMM =cmd.Parameters.Add("@YYYYMM",SqlDbType.Char);

//表示是输出参数
paraCardNo.Direction = ParameterDirection.Input;
paraToAddress.Direction = ParameterDirection.Input;
paraCCAddress.Direction = ParameterDirection.Input;
paraYYYYMM.Direction = ParameterDirection.Input;

//参数赋值
paraCardNo.Value = myDrv[0].ToString().Trim();
paraToAddress.Value = myDrv[1].ToString().Trim();
paraCCAddress.Value = myDrv[2].ToString().Trim();
= ddlYear.Items paraYYYYMM.Value [ddlYear.SelectedIndex] + ddlMonth.Items .Value [ddlMonth.SelectedIndex] .Value;

conn.Open ();
the cmd.ExecuteNonQuery (); // write SQL database
}
}
the catch
{
Page. Response.Write ( "alert ( 'first" + count.ToString () + "th data error!');";
objConn.Close (); // close the connection of EXCEL
}

objConn.Close (); // Close EXCEL Connection

Transfer: http://www.wybcom.cn

Reproduced in: https: //www.cnblogs.com/zsli-2008-20/archive/2008/03/14/1105525.html

Guess you like

Origin blog.csdn.net/weixin_33816946/article/details/93314739