excel导入sq程序l提示“from字句语法错误”
版主好,各位高手好,初入论坛,还请大家多关照,这两天因公司需要,要把一些EXCEL表格导入SQL数据库,下面的代码在执行时一直提示“from子句语法错误”,不知道是何原因!另外,有谁可以提供下比较好的excel导入sql的代码么,初学c#,不胜感激!using System;
using System.Collections;
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
using System.Data.OleDb;
public partial class inputexcel : System.Web.UI.Page
{
protected static string getErrMsg = "";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string filePath = "";
if (!FileUpload1.HasFile)
{
Response.Write("<script language=javascript>alert('请选择要上传的文件!');</script>");
return;
}
else
{
//获取文件路径
filePath = FileUpload1.PostedFile.FileName;
string excel_sql = "select [文件编号],[目录],[文件名],[文件路径],[上传时间],[状态],[上传人] from OPENROWSET('MICROSOFT.JET.OLEDB.4.0','Excel 8.0;HDR=YES;DATABASE=d:\\exceltosql.xls',sheet1$)";
//把excel看成数据库
string mystring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\exceltosql.xls;Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(mystring);
OleDbDataAdapter myda = new OleDbDataAdapter(excel_sql,conn);
DataSet myds = new DataSet();
myda.Fill(myds);
DataTable dt = new DataTable();
dt = myds.Tables[0];
GridView1.DataSource = dt;
GridView1.DataBind();
if (myds.Tables[0].Rows.Count != 0)
{
SqlConnection myconn = new SqlConnection();
string strConn = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
myconn.ConnectionString = strConn;
SqlCommand cmd = new SqlCommand();
try
{
for (int i = 0; i < myds.Tables[0].Rows.Count; i++)
{
string sql = "insert into filedetail(filenumber,catalogueid,filename,filepath,updatetime,state,funame) values('" + myds.Tables[0].Rows[i]["文件编号"] + "','" + myds.Tables[0].Rows[i]["目录"] + "','" + myds.Tables[0].Rows[i]["文件名"] + "','" + myds.Tables[0].Rows[i]["文件路径"] + "','" + myds.Tables[0].Rows[i]["上传时间"] + "','" + myds.Tables[0].Rows[i]["状态"] + "','" + myds.Tables[0].Rows[i]["上传人"] + "')";
cmd = new SqlCommand(sql,myconn);
if (cmd.Connection.ToString()=="Closed")
{
cmd.Connection.Open();
}
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
getErrMsg = ex.Message.ToString();
Response.Write(getErrMsg);
}
finally
{
cmd.Connection.Close();
cmd = null;
}
if (getErrMsg =="")
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script language='Javascript'>alert('导入成功!')</script>");
return;
}
else
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script language='Javascript'>alert('导入失败!')</script>");
return;
}
}
}
}
}
[ 本帖最后由 daylightme 于 2010-11-14 11:14 编辑 ]