选择,更新查询c#错误

时间:2014-04-02 17:57:12

标签: c# sql .net parameterized-query

我正在将excel文件导入sql数据库。我正在使用的工作代码是:

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

我不知道如何更新它,这就是为什么我要删除数据然后插入。请帮助我根据主键表LocalSKU更新列QOH。

我尝试了以下内容,但它提示错误说" Merge语句必须以半列(;)"

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

1 个答案:

答案 0 :(得分:4)

  

基本上我只需要两列KitSKU和Quantity   表Kits中的列。

变化:

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

要:

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

您想从表中选择列[KitSKU][Quantity]Kits

  

谢谢Max ..也如果我想更新它而不是插入,如何   实现它。

使用以下代码更新KitSKU和数量:

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

但我不建议使用该代码,因为它没有参数化,我认为你应该尝试一些基本的sql课程并使用sql参数。