使用具有不同数量的列的Excel文件更新数据库

时间:2014-08-22 13:24:00

标签: c# sql excel code-behind xls

我想更新sql中按钮事件的表(Quotation),其中用户导入包含任意数量列的excel文件,其中一个字段'Quotation No'不能为null或为空,而其他字段可能为或可能不是null或空。还有一些备用(额外)列在数据库中,用户可以更新。我创建了另一个表(QuotationExtraColumn),以便重命名的额外字段名称存储在新表中,但值是存储在前一个报价表中。字段供应商号和设备号没有固定,应该将它们作为备用字段。

现在我可以做的是固定列我可以更新数据,无论是否有值,但对于备用字段我还不知道该怎么做。  问题是有一个xls上传,其中预定义的列名称具有不同数量的列。但是现在要求客户端想要数据库中的备用字段(列),以便如果他需要一个新字段,他可以添加列并从此通用更新添加值到新字段,甚至不需要您知道要求开发人员为其创建新字段及其单独的更新功能。

这是代码:

    protected void btnGenUpdate_Click(object sender, EventArgs e)
{
    OleDbConnection con = new OleDbConnection();
    string strExtension = Path.GetExtension(fuGenUpdate.FileName);
    if (strExtension.ToLower() != ".xls" && strExtension.ToLower() != ".xlsx")
    {
        lblMessage.Text = "Please import valid file.";
        lblMessage.ForeColor = System.Drawing.Color.Red;
        lblMessage.CssClass = "error";
        return;
    }

    string strConnectionString = string.Empty;
    if (strExtension.ToLower() == ".xls")
    {
        strConnectionString = @"Datasource";
    }

    con.ConnectionString = strConnectionString;
    con.Open();

    DataTable dtSchema = con.GetSchema("Tables");
    DataTable dtSheet = new DataTable();
    if (dtSchema != null && dtSchema.Rows.Count > 0)
    {
        OleDbDataAdapter da = new OleDbDataAdapter("Select * from [" + Convert.ToString(dtSchema.Rows[0]["TABLE_NAME"]) + "]", con);
        da.Fill(dtSheet);

        bool isExistsQuotation = false;

        for (int j = 0; j < dtSheet.Columns.Count; j++)
        {
            if (dtSheet.Columns[j].ColumnName == "")
            {
                lblMessage.Text = "Please Enter Column Name.";
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.CssClass = "error";
            }

            if (dtSheet.Columns[j].ColumnName.Trim().ToLower() == "quotation no" || dtSheet.Columns[j].ColumnName.Trim().ToLower() == "quotationno")
            {
                isExistsQuotation = true;
            }
        }

        if (!isExistsQuotation)
        {
            lblMessage.Text = "";
            lblMessage.Visible = false;
            con.Close();
            con.Dispose();
            ScriptManager.RegisterStartupScript(this, GetType(), "alertmessage", "EmptyQuotationNoColumn();", true);
            return;
        }


        string QuotationNo = "";
        string OrderNo = "";
        string JCF = "";
        string WBSNo = "";
        string Cost = "";
        string InvoiceNo = "";
        string SAPExpenses = "";
        string spare1 = "";
        string spare2 = "";
        string spare3 = "";
        string spare4 = "";
        string spare5 = "";
        string spare6 = "";
        string spare7 = "";
        string spare8 = "";
        int index1;
        int index2;
        int temp = 0;
        for (int i = 0; i < dtSheet.Rows.Count; i++)
        {
            try
            {
                DataRow row = dtSheet.Rows[i];
                //int columnCount = dtSheet.Columns.Count;
                //string[] columns = new string[columnCount];
                //dtSheet.Columns.Contains();
                for (int j = 0; j < dtSheet.Columns.Count; j++)
                {
                    if (dtSheet.Columns[j].ColumnName.ToLower() == "quotationno" || dtSheet.Columns[j].ColumnName.ToLower() == "quotation no")
                    {
                        QuotationNo = row[j].ToString();
                        if (QuotationNo == "")
                        {
                            lblMessage.Text = "";
                            lblMessage.Visible = false;
                            ScriptManager.RegisterStartupScript(this, GetType(), "alertmessage", "EmptyQuotationNo();", true);
                            return;
                        }
                    }

                    if (dtSheet.Columns[j].ColumnName.ToLower() == "orderno" || dtSheet.Columns[j].ColumnName.ToLower() == "order no")
                    {
                        OrderNo = row[j].ToString();
                    }
                    if (dtSheet.Columns[j].ColumnName.ToLower() == "jcf")
                    {
                        JCF = row[j].ToString();
                    }
                    if (dtSheet.Columns[j].ColumnName.ToLower() == "wbsno" || dtSheet.Columns[j].ColumnName.ToLower() == "wbs no")
                    {
                        WBSNo = row[j].ToString();
                    }
                    if (dtSheet.Columns[j].ColumnName.ToLower() == "cost")
                    {
                        Cost = row[j].ToString();
                    }
                    if (dtSheet.Columns[j].ColumnName.ToLower() == "invoiceno" || dtSheet.Columns[j].ColumnName.ToLower() == "invoice no")
                    {
                        InvoiceNo = row[j].ToString();
                    }
                    if (dtSheet.Columns[j].ColumnName.ToLower() == "sapexpenses" || dtSheet.Columns[j].ColumnName.ToLower() == "sap expenses")
                    {
                        SAPExpenses = row[j].ToString();
                    }
                    if (dtSheet.Columns[j].ColumnName.ToLower() == "vendorno" || dtSheet.Columns[j].ColumnName.ToLower() == "vendor no")
                    {
                        //index1 = dtSheet.Columns[j].ColumnName.ToLower().IndexOf("vendorno");
                        spare1 = row[j].ToString();
                    }
                 //vendorno is spare1 and equipmentno is spare2
                    if (dtSheet.Columns[j].ColumnName.ToLower() == "equipmentno" || dtSheet.Columns[j].ColumnName.ToLower() == "equipment no")
                    {

                        spare2 = row[j].ToString();
                    }
                }

                int count = Quotation.InsertGenericUpdate(QuotationNo, OrderNo, JCF, WBSNo, InvoiceNo, Cost, SAPExpenses, spare1, spare2);
                temp += count;
                lblMessage.Visible = true;
                if (temp != 0)
                {
                    lblMessage.Text = "Generic Update Successful : " + temp + " Row(s) Updated";
                }
                else { lblMessage.Text = "Enter proper data"; }
            }
            catch
            {
                lblMessage.Text = "Generic Update Unsuccessful: No Rows Updated";
            }
            finally { con.Close(); con.Dispose(); }
        }
    }
    else
    {
        lblMessage.Text = "Please import valid file.";
        lblMessage.CssClass = "error";
        return;
    }
}

没有备用字段的SQL代码是:

    SET @orderno = CASE WHEN (@orderno = '') THEN null WHEN (@orderno != '') THEN @orderno END
SET @jcf = CASE WHEN (@jcf = '') THEN NULL WHEN (@jcf != '') THEN @jcf END
SET @wbs = CASE WHEN (@wbs = '') THEN null WHEN (@wbs != '') THEN @wbs END
SET @invoiceno = CASE WHEN (@invoiceno = '') THEN null WHEN (@invoiceno != '') THEN @invoiceno END
SET @cost = CASE WHEN (@cost = '') THEN null WHEN (@cost != '') THEN @cost END
SET @sapexpenses = CASE WHEN (@sapexpenses = '') THEN null WHEN (@sapexpenses != '') THEN @sapexpenses END

UPDATE [AFS].[dbo].[Quotation]      
    SET [OrderNo] = ISNULL(@orderno,[OrderNo]),
        [JCF] = ISNULL(@jcf,JCF),
        [WBSNo] = ISNULL(@wbs,WBSNo),
        [InvoiceNo] = ISNULL(@invoiceno,InvoiceNo),
        [Cost] = ISNULL(@cost,Cost),
        [SAPExpenses] = ISNULL(@sapexpenses,SAPExpenses)
    WHERE [QuotationNo] = @quotationno;

0 个答案:

没有答案