将Excel .xslx文件导入SQL服务器:更新表困难

本文关键字:更新 服务器 SQL Excel xslx 文件 导入 | 更新日期: 2023-09-27 17:56:16

我正在将excel文件导入sql服务器数据库。代码工作正常,但我目前的做法是删除(清除表)表数据。

         string ssqltable = "tStudent";
          string myexceldataquery = "select id,student,rollno,course from [sheet1$]";
        try
        {
            string sexcelconnectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" + excelfilepath + "; Extended Properties='"Excel 12.0; HDR=Yes; IMEX=2'"";
            string ssqlconnectionstring = "Data Source=DELL''SQLSERVER1;Trusted_Connection=True;DATABASE=Test;CONNECTION RESET=FALSE";
            SqlConnection sqlconn = new SqlConnection(ssqlconnectionstring);
            SqlCommand sqlcmd = new SqlCommand(@"MERGE tStudent AS target
                              USING (select ID, STUDENT , ROLLNO from @source)  as source
                                ON (source.ID = target.ID)
                              WHEN MATCHED THEN
                              UPDATE SET Student = source.Student,
                                         ROLLNO = source.ROLLNO
                              WHEN NOT MATCHED THEN
                              INSERT (ID, STUDENT , ROLLNO)
                              VALUES (source.id, source.Student, source.RollNo);", sqlconn);
          ******************************************
            SqlParameter param = new SqlParameter();
            sqlcmd.Parameters.AddWithValue("@source", dr);
            param.SqlDbType = SqlDbType.Structured;
            param.TypeName = "dbo.tStudent";
          ******************************************
            sqlconn.Open();
            sqlcmd.ExecuteNonQuery();
            sqlconn.Close();
            //series of commands to bulk copy data from the excel file into our sql table
            OleDbConnection oledbconn = new OleDbConnection(sexcelconnectionstring);
            OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn);
            oledbconn.Open();
            OleDbDataReader dr = oledbcmd.ExecuteReader();
            SqlBulkCopy bulkcopy = new SqlBulkCopy(ssqlconnectionstring);
            bulkcopy.DestinationTableName = ssqltable;
            bulkcopy.WriteToServer(dr);
            while (dr.Read())
            {
                //bulkcopy.WriteToServer(dr);
            }
            oledbconn.Close();
            Console.WriteLine(".xlsx file imported succssessfully into database.", bulkcopy.NotifyAfter);
        }

请参阅 * 部分。我已经在 Sqlparameters 中分配了我的 OleDb DataRreader dr,但我稍后会在代码中声明它。请指导我如何构建我的代码。

示例将不胜感激。

将Excel .xslx文件导入SQL服务器:更新表困难

鉴于您的 excel 文件与表格的结构相同,并且您想要更新而不仅仅是插入,最简单的方法是使用合并和表值参数

SqlCommand cmd = new SqlCommand(@"MERGE tStudent AS target
                                  USING (select ID, STUDENT , ROLLNO from @source)  as source
                                    ON (source.ID = target.ID)
                                  WHEN MATCHED THEN
                                  UPDATE SET Student = source.Student,
                                             ROLLNO = source.ROLLNO
                                  WHEN NOT MATCHED THEN
                                  INSERT (ID, STUDENT , ROLLNO)
                                  VALUES (source.id, source.Student, source.RollNo);"
                                   , sqlconn);
SqlParameter param cmd.Parameters.AddWithValue("@source", dr);
param.SqlDbType = SqlDbType.Structured;
param.TypeName = "dbo.tStudent";  

其他选项包括循环、使用临时表、将数据作为 xml 数据或字符串数据传递,或使用 SSIS 等 ETL 工具。