Question

I am importing excel file into sql server datatbase. The code works fine but the way I am doing currently is deleting (clear the table) the table data.

         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);
        }

See * section. I have assigned my OleDb DataRreader dr in Sqlparameters, but I am declaring it later in code. Please guide me with how to structure my code.

Example would be appreciated.

Était-ce utile?

La solution

Given that your excel file is the same structure as your table and you want to update rather than just insert the easiest way is to use Merge and a Table-Valued Paramter

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";  

Your other options involve looping, using staging tables, passing the data as xml data or string data, or using an ETL tool like SSIS.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top