| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1906 人关注过本帖
标题:excel导入sq程序l提示“from字句语法错误”
取消只看楼主 加入收藏
daylightme
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-11-14
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
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 编辑 ]
搜索更多相关主题的帖子: 不胜感激 excel 数据库 EXCEL 
2010-11-14 10:07
daylightme
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-11-14
收藏
得分:0 
谢谢版主,我去试一下这个方法!
2010-11-14 15:27
快速回复:excel导入sq程序l提示“from字句语法错误”
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027067 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved