0

I am importing excel file into sql database.Working code I am Using is:

 public static void ImportToSql(string excelfilepath)
              {
        string ssqltable = "Inventory";
        string myexceldataquery = "select LocalSKU,QOH 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=CAMO;CONNECTION RESET=FALSE";

            //execute a query to erase any previous data from our destination table
            string sclearsql = "delete " + ssqltable;
enter code here
           "HERE I DON'T WANT THIS STEP TO DELETE TABLE DATA AND THEN INSERTING IT.INSTEAD I WANT TO UPDATE TABLE DATA"
            SqlConnection sqlconn = new SqlConnection(ssqlconnectionstring);
            SqlCommand sqlcmd = new SqlCommand(sclearsql, sqlconn);

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

            Console.WriteLine(".xlsx file imported succssessfully into database.", bulkcopy.NotifyAfter);
            oledbconn.Close();
        }

I don't know how to update it that's why what I do is I delete data and then insert. Please help me with Updating columns QOH based on primarykey table LocalSKU.

I tried the following thing but it gives error saying "Merge statement must be terminated by a semi-column(;)"

             SqlCommand sqlcmd = new SqlCommand(@"MERGE Inventory AS target
                              USING (select LocalSKU, QOH from @source)  as source
                                ON (source.ID = target.ID)
                              WHEN MATCHED THEN
                              UPDATE SET QOH = source.QOH 
                              WHEN NOT MATCHED THEN
                              INSERT (LocalSKU, QOH )
                              VALUES (source.LocalSKU, source.QOH )", sqlconn);

            SqlParameter param=new SqlParameter();
            sqlcmd.Parameters.AddWithValue("@source", ssqltable);
            param.SqlDbType = SqlDbType.Structured;
            param.TypeName = "dbo.tStudent";
2
  • 3
    Your query should be: "select [KitSKU] , [Quantity] from Kits" Commented Apr 2, 2014 at 17:58
  • @user2525244 Perhaps it is better to ask the update question, in another question. More answers will help you and faster, since this question has been answered already. Commented Apr 3, 2014 at 6:38

1 Answer 1

4

Basically I want only two columns KitSKU and Quantity out of many columns in table Kits.

Change:

SqlDataAdapter da = new SqlDataAdapter("select * from [KitSKU] , [Quantity] from Kits", sqlCon);

To:

SqlDataAdapter da = new SqlDataAdapter("select [KitSKU] ,[Quantity] from Kits", sqlCon);

you want to select column: [KitSKU] and [Quantity] from table: Kits

Thanks Max.. also If I want to Update it rather than inserting, how to implement that.

Use following code, for updating KitSKU and Quantity:

SqlDataAdapter da = new SqlDataAdapter("update Kits set [KitSKU] = 'entersku' ,[Quantity] = 5 where [KutSKU] = 'what record it should update'", sqlCon);

But I wouldn't recommend that code, since it isn't parameterized and I think you should try with some basic sql lessons and using sql parameters.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks Max.. also If I want to Update it rather than inserting, how to implement that.
KitSKU and Quantity in table Kits. Actuallly I am importing excel into sql data. Currently I am implementing it by deleting data from that table and then inserting. But I want to implement it as Update.
What data types are KitSKU and Quantity?
KitSKU varchar(200) with pk [is Identity is set to No] and Quantity int
So KitSKU is your primary key? So you only want to update the Quantity?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.