您的位置:首页 > 数据库

页面控件级 excel表格导入sql

2010-04-02 15:21 281 查看
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
DataSet ds = new DataSet();
public void Ge()
{
string mapath = FileUpload1.PostedFile.FileName.ToString().Trim();
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Extended Properties=Excel 8.0;" + "data source=" + mapath;

DataSet ds = new DataSet();
OleDbConnection myConn = new OleDbConnection(strConn);
string sqll = "select * from [Sheet1$]";
if (myConn.State == ConnectionState.Closed)
{
myConn.Open();
}
OleDbDataAdapter MyComm = new OleDbDataAdapter(sqll, myConn);
MyComm.SelectCommand.Parameters.Add("@path", OleDbType.VarChar, 100);
MyComm.SelectCommand.Parameters["@path"].Value = txtPath.Text.ToString().Trim();
MyComm.Fill(ds);
myConn.Close();
if (ds == null)
{
Label1.Text = "您输入的路径无效.有问题请联系有关开发人员";
}
else return;

}

protected void Button1_Click(object sender, EventArgs e)
{
Ge();
}

public void Inser()
{
int num = 0;
string conn = "server=.;database=ComData;uid=sa;pwd=01120817lhh";
SqlConnection sqlConn = new SqlConnection(conn);
try
{
for (int i = 0; i < ds.Tables.Count; i++)
{

num += ds.Tables[i].Rows.Count;
for (int j = 0; j < ds.Tables[i].Rows.Count; j++)
{

string str = "INSERT INTO Company_Information (公司序号,公司简称,公司全程,公司英文,公司省份,公司城市,公司详细地址,公司电话,公司邮编,公司网址,公司E_mail,公司简介,公司案例,公司系统资质等级,公司证书编号,批准备案日期,首次获奖日期,发证机关,资质) VALUES (@number,@Abbreviation,@Fname,@Ename,@Province,@City,@Address,@Phone,@ZipCode,@Website,@Email,@Introduction,@Case,@Grade,@CNumber,@ADate,@Fdata,@AOrgan,@Qualification)";
SqlDataAdapter comm = new SqlDataAdapter(str, sqlConn);
//SqlCommand comm = new SqlCommand(str,sqlConn);
comm.SelectCommand.Parameters.Add("@number", SqlDbType.NVarChar, 4000);
comm.SelectCommand.Parameters["@number"].Value = ds.Tables[i].Rows[j]["序号"].ToString().Trim();
comm.SelectCommand.Parameters.Add("@Abbreviation", SqlDbType.NVarChar, 4000);
comm.SelectCommand.Parameters["@Abbreviation"].Value = ds.Tables[i].Rows[j]["简称"].ToString().Trim();
comm.SelectCommand.Parameters.Add("@Fname", SqlDbType.NVarChar, 4000);
comm.SelectCommand.Parameters["@Fname"].Value = ds.Tables[i].Rows[j]["全称"].ToString().Trim();
comm.SelectCommand.Parameters.Add("@Ename", SqlDbType.NVarChar, 4000);
comm.SelectCommand.Parameters["@Ename"].Value = ds.Tables[i].Rows[j]["英文"].ToString().Trim();
comm.SelectCommand.Parameters.Add("@Province", SqlDbType.NVarChar, 4000);
comm.SelectCommand.Parameters["@Province"].Value = ds.Tables[i].Rows[j]["简称"].ToString().Trim();
comm.SelectCommand.Parameters.Add("@City", SqlDbType.NVarChar, 4000);
comm.SelectCommand.Parameters["@City"].Value = ds.Tables[i].Rows[j]["市"].ToString().Trim();
comm.SelectCommand.Parameters.Add("@Address", SqlDbType.NVarChar, 4000);
comm.SelectCommand.Parameters["@Address"].Value = ds.Tables[i].Rows[j]["详细地址"].ToString().Trim();
comm.SelectCommand.Parameters.Add("@Phone", SqlDbType.NVarChar, 4000);
comm.SelectCommand.Parameters["@Phone"].Value = ds.Tables[i].Rows[j]["电话"].ToString().Trim();
comm.SelectCommand.Parameters.Add("@ZipCode", SqlDbType.NVarChar, 4000);
comm.SelectCommand.Parameters["@ZipCode"].Value = ds.Tables[i].Rows[j]["邮编"].ToString().Trim();
comm.SelectCommand.Parameters.Add("@Website", SqlDbType.NVarChar, 4000);
comm.SelectCommand.Parameters["@Website"].Value = ds.Tables[i].Rows[j]["网址"].ToString().Trim();
comm.SelectCommand.Parameters.Add("@Email", SqlDbType.NVarChar, 4000);
comm.SelectCommand.Parameters["@Email"].Value = ds.Tables[i].Rows[j]["e-mail"].ToString().Trim();
comm.SelectCommand.Parameters.Add("@Introduction", SqlDbType.NVarChar, 4000);
comm.SelectCommand.Parameters["@Introduction"].Value = ds.Tables[i].Rows[j]["公司简介"].ToString().Trim();
comm.SelectCommand.Parameters.Add("@Case", SqlDbType.NVarChar, 4000);
comm.SelectCommand.Parameters["@Case"].Value = ds.Tables[i].Rows[j]["案例"].ToString().Trim();
comm.SelectCommand.Parameters.Add("@Grade", SqlDbType.NVarChar, 4000);
comm.SelectCommand.Parameters["@Grade"].Value = ds.Tables[i].Rows[j]["系统集成资质等级"].ToString().Trim();
comm.SelectCommand.Parameters.Add("@CNumber", SqlDbType.NVarChar, 4000);
comm.SelectCommand.Parameters["@CNumber"].Value = ds.Tables[i].Rows[j]["证书编号"].ToString().Trim();
comm.SelectCommand.Parameters.Add("@ADate", SqlDbType.NVarChar, 4000);
comm.SelectCommand.Parameters["@ADate"].Value = ds.Tables[i].Rows[j]["批准(备案)日期"].ToString().Trim();
comm.SelectCommand.Parameters.Add("@Fdata", SqlDbType.NVarChar, 4000);
comm.SelectCommand.Parameters["@Fdata"].Value = ds.Tables[i].Rows[j]["首次获证日期"].ToString().Trim();
comm.SelectCommand.Parameters.Add("@AOrgan", SqlDbType.NVarChar, 4000);
comm.SelectCommand.Parameters["@AOrgan"].Value = ds.Tables[i].Rows[j]["发证机关"].ToString().Trim();
comm.SelectCommand.Parameters.Add("@Qualification", SqlDbType.NVarChar, 4000);
comm.SelectCommand.Parameters["@Qualification"].Value = ds.Tables[i].Rows[j]["资质"].ToString().Trim();

//SqlCommand sqComm = new SqlCommand(str, sqlConn);

//if (sqlConn.State == ConnectionState.Closed)
//{
// sqlConn.Open();
//}
//comm.ExecuteNonQuery();

//sqComm.ExecuteNonQuery();

}

}
}
catch (Exception ep)
{
Response.Write(ep.Message);
}
//finally
//{
// sqlConn.Close();
//}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: