将Excel数据上传到SQL Server错误

用户2345661

这是我用来尝试将数据上传到SQL Server数据库的代码。

它已经运行了几次,但是现在在“浏览”文本框中显示未选择文件。

我尝试调试并检查

 strFilepPath = DirectoryPath + FileUpload1.FileName;

实际上具有所选文件的路径。

在调试代码不会运行过去 sqlBulk.WriteToServer(ds.Tables[0]);

protected void btnImport_Click(object sender, EventArgs e)
{
    string strFilepPath;

    // Create a Connection String
    string CS = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    // SqlConnection is in System.Data.SqlClient namespace
    using (SqlConnection con = new SqlConnection(CS))
    {
        // SqlCommand to Drop and Re-Create Table
        string DropString = "USE Sit302GroupProject IF OBJECT_ID('students', 'U') IS NOT NULL DROP TABLE students;";

        string CreateString = "USE Sit302GroupProject " +
             "CREATE TABLE   students " +
            "(student_id   int Not Null, " +
             "first_name   nvarchar (255) Not Null, " +
             "surname   nvarchar (255) Not Null, " +
             "email   nvarchar (255) Not Null, " +
             "campus   nvarchar (255) Not Null, " +
             "enrollment_status   nvarchar (255) Not Null, " +
             "project_type   nvarchar (255) Not Null, " +
             "group_id   nvarchar (50) Null) ";

        try
        {
            SqlCommand DropCommand = new SqlCommand(DropString, con);
            SqlCommand CreateCommand = new SqlCommand(CreateString, con);

            con.Open();

            CreateCommand.ExecuteNonQuery();
            DropCommand.ExecuteNonQuery();

            con.Close();
        }
        catch (Exception ex1)
        {
            ScriptManager.RegisterStartupScript(Page, GetType(), "script1",
            "alert('error occured: " + ex1.Message.ToString() + "');", true);
        }
    }

    DataSet ds = new DataSet();
    string strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    if (FileUpload1.HasFile)
    {
        try
        {
            FileInfo fi = new FileInfo(FileUpload1.PostedFile.FileName);
            string ext = fi.Extension;

            if (ext == ".xls" || ext == ".xlsx")
            {
                string filename = Path.GetFullPath(FileUpload1.PostedFile.FileName);
                string DirectoryPath = Server.MapPath("~/UploadExcelFile//");
                strFilepPath = DirectoryPath + FileUpload1.FileName;

                Directory.CreateDirectory(DirectoryPath);

                FileUpload1.SaveAs(strFilepPath);

                string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strFilepPath + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\"";

                OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open();

                OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", conn);

                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                da.Fill(ds);

                SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection, SqlBulkCopyOptions.KeepIdentity);
                sqlBulk.DestinationTableName = "students";
                sqlBulk.WriteToServer(ds.Tables[0]);

                conn.Close();
                sqlBulk.Close();

                Array.ForEach(Directory.GetFiles(@DirectoryPath), File.Delete);

                ScriptManager.RegisterStartupScript(Page, GetType(), "script1",
                "alert('Excel file successfully imported into DB');", true);

                return;
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, GetType(), "script1",
                "alert('Please upload excel file only');", true);
                return;
            }
        }
        catch (Exception ex)
        {
            DeleteExcelFile(FileUpload1.FileName);
            ScriptManager.RegisterStartupScript(Page, GetType(), "script1",
              "alert('error occured: " + ex.Message.ToString() + "');", true);
            return;
        }
    }
    else
    {
        ScriptManager.RegisterStartupScript(Page, GetType(), "script1",
        "alert('Please upload excel file');", true);
        return;
    }
}

protected void DeleteExcelFile(string Name)
{
    if (Directory.Exists(Request.PhysicalApplicationPath +
    "UploadExcelFile\\"))
    {
        string[] logList = Directory.GetFiles(Request.PhysicalApplicationPath
        + "UploadExcelFile\\", "*.xls");
        foreach (string log in logList)
        {
            FileInfo logInfo = new FileInfo(log);
            string logInfoName = logInfo.Name.Substring(0,
            logInfo.Name.LastIndexOf('.'));
            if (logInfoName.Length >= Name.Length)
            {
                if (Name.Equals(logInfoName.Substring(0, Name.Length)))
                {
                    logInfo.Delete();
                }
            }
        }
    }
}
marc_s

首先创建表似乎很奇怪,在下一行中,您再次将其删除...

SqlCommand DropCommand = new SqlCommand(DropString, con);
SqlCommand CreateCommand = new SqlCommand(CreateString, con);

con.Open();

CreateCommand.ExecuteNonQuery();  // creates "dbo.students"
DropCommand.ExecuteNonQuery();    // drops that same "dbo.students" right away ....

con.Close();

因此,最后-您没有一个可批量插入数据的表...

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将CSV文件上传到SQL Server

来自分类Dev

将文件上传到SQL Server

来自分类Dev

无需安装Access数据库引擎,将最新版本的Excel数据(如2013)上传到SQL Server表中

来自分类Dev

使用php将图像上传到sql数据库时显示错误

来自分类Dev

c#如何将多个文件上传到SQL Server数据库?

来自分类Dev

将数据框上传到 sql server 中的现有表

来自分类Dev

将文件上传到SQL Server,无法正确检索它

来自分类Dev

MVC4代码优先实体框架将大文件上传到SQL Server数据库

来自分类Dev

Dropzone JS上传到WCF错误数据

来自分类Dev

无法将文件上传到transfer.sh。错误:无法保存元数据

来自分类Dev

将数据上传到服务器后出现404错误

来自分类Dev

由于Base64错误,无法将图像上传到数据库

来自分类Dev

尝试使用按钮将图像上传到我在 CodeIgniter 中的数据库,但出现错误

来自分类Dev

尝试将消息上传到 Firebase 数据库时出现“发现冲突的 getter”错误

来自分类Dev

无法通过Android Studio项目将数据上传到我的Parse Server

来自分类Dev

从Laravel将映像上传到AWS s3存储时显示Heroku Server 500错误

来自分类Dev

使用 WCF 服务将 10000 多条记录的文件上传到 SQL Server

来自分类Dev

SQL Server:数据导入错误

来自分类Dev

通过网站将Excel工作表数据上传到SQL数据库

来自分类Dev

从Excel将数据导入SQL Server表

来自分类Dev

将SQL Server数据导出到Excel

来自分类Dev

将数据从Excel导入SQL Server

来自分类Dev

尝试将图像和用户数据上传到mongoDB时出现错误“发送后无法设置标题”

来自分类Dev

SQL Server / .Net错误?

来自分类Dev

SQL Server ISDATE()错误

来自分类Dev

SQL Server:查询错误

来自分类Dev

sql server的bcp错误

来自分类Dev

连接错误SQL Server

来自分类Dev

创建一个循环将 Excel 上传到 SQL 数据库

Related 相关文章

  1. 1

    将CSV文件上传到SQL Server

  2. 2

    将文件上传到SQL Server

  3. 3

    无需安装Access数据库引擎,将最新版本的Excel数据(如2013)上传到SQL Server表中

  4. 4

    使用php将图像上传到sql数据库时显示错误

  5. 5

    c#如何将多个文件上传到SQL Server数据库?

  6. 6

    将数据框上传到 sql server 中的现有表

  7. 7

    将文件上传到SQL Server,无法正确检索它

  8. 8

    MVC4代码优先实体框架将大文件上传到SQL Server数据库

  9. 9

    Dropzone JS上传到WCF错误数据

  10. 10

    无法将文件上传到transfer.sh。错误:无法保存元数据

  11. 11

    将数据上传到服务器后出现404错误

  12. 12

    由于Base64错误,无法将图像上传到数据库

  13. 13

    尝试使用按钮将图像上传到我在 CodeIgniter 中的数据库,但出现错误

  14. 14

    尝试将消息上传到 Firebase 数据库时出现“发现冲突的 getter”错误

  15. 15

    无法通过Android Studio项目将数据上传到我的Parse Server

  16. 16

    从Laravel将映像上传到AWS s3存储时显示Heroku Server 500错误

  17. 17

    使用 WCF 服务将 10000 多条记录的文件上传到 SQL Server

  18. 18

    SQL Server:数据导入错误

  19. 19

    通过网站将Excel工作表数据上传到SQL数据库

  20. 20

    从Excel将数据导入SQL Server表

  21. 21

    将SQL Server数据导出到Excel

  22. 22

    将数据从Excel导入SQL Server

  23. 23

    尝试将图像和用户数据上传到mongoDB时出现错误“发送后无法设置标题”

  24. 24

    SQL Server / .Net错误?

  25. 25

    SQL Server ISDATE()错误

  26. 26

    SQL Server:查询错误

  27. 27

    sql server的bcp错误

  28. 28

    连接错误SQL Server

  29. 29

    创建一个循环将 Excel 上传到 SQL 数据库

热门标签

归档