excel 是列的多少不固定。
sql server 表列是固定的
就是想实现将用户现有的数据导入到新开发的系统的数据库
,让用户自己来实现。现在excel 列名已经取出来了
导入的sql server 语句还不知道怎么写,因为列不是固定的,根据用户自己选择的
动态生成的sql 语句。
// cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
}
private void bind()
{
// 在此处放置用户代码以初始化页面
// Create connection string variable. Modify the "Data Source"
// parameter as appropriate for your environment.
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("/Files/Book1.xls") + ";" +
"Extended Properties=Excel 8.0;";
// Create connection object by using the preceding connection string.
OleDbConnection objConn = new OleDbConnection(sConnectionString);
// Open connection with the database.
objConn.Open();
// The code to follow uses a SQL SELECT command to display the data from the worksheet.
// Create new OleDbCommand to return data from worksheet.
OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
// Create new OleDbDataAdapter that is used to build a DataSet
// based on the preceding SQL SELECT statement.
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
// Pass the Select command to the adapter.
objAdapter1.SelectCommand = objCmdSelect;
// Create new DataSet to hold information from the worksheet.
DataSet objDataset1 = new DataSet();
// Fill the DataSet with the information from the worksheet.
objAdapter1.Fill(objDataset1, "XLData");
// Bind data to DataGrid control.
//DataGrid1.DataSource = objDataset1.Tables[0].DefaultView;
//DataGrid1.DataBind();
if (System.Convert.ToInt32(Session["i"])==objDataset1.Tables[0].Columns.Count)
{
Session["i"]=0;
}
this.ListBox1.DataTextField=objDataset1.Tables[0].Columns[System.Convert.ToInt32(Session["i"])].ToString();
this.ListBox1.DataBind();
ArrayList al = new ArrayList();
al.Add(objDataset1.Tables[0].Columns[System.Convert.ToInt32(Session["i"])].ToString());
for (int i=0;i {
Response.Write(al.ToString());
this.Label1.Text=al.ToString();
}