【问题标题】:upload excel file and update the same records asp.net上传excel文件并更新相同的记录asp.net
【发布时间】:2018-09-16 09:17:42
【问题描述】:

我正在做一个项目,我必须在 SQL 服务器数据库中上传 excel 文件,如果记录已经存在,还更新我已经使用 SQL bulk cop 实现了代码,它将 excel 文件插入到数据库表,但是如果记录已经可用,我会卡住如何更新。 我为此使用asp.net c#,这是我目前的代码,帮助我并指导我将excel文件上传到数据库的最佳方法

      protected void btnUpload_Click(object sender, EventArgs e)
{
    try
    {
        int id;
        string contactPerson;
        string designation;
        string company;
        string contact;
        string emailaddress;
        string city;
        string region;
        string industry;
        string division;
        string mobile;
        string address;




        string path = Path.GetFileName(FileUpload1.FileName);
        path = path.Replace(" ", "");

        FileUpload1.SaveAs(Server.MapPath("~/uploadExcel/") + FileUpload1.FileName);
        String ExcelPath = Server.MapPath("~/uploadExcel/") + FileUpload1.FileName;
        OleDbConnection mycon = new OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + ExcelPath + "; Extended Properties=Excel 8.0; Persist Security Info = False");

        //       OleDbConnection myconn =
        //new OleDbConnection( @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelPath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1;';");


        mycon.Open();
        OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]", mycon);
        OleDbDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            if (dr[0].ToString() != "")
            {
                // Response.Write("<br/>"+dr[0].ToString());
                id = Convert.ToInt32(dr[0].ToString());
                contactPerson = dr[1].ToString();
                designation = dr[2].ToString();
                company = dr[3].ToString();
                emailaddress = dr[4].ToString();
                contact = dr[5].ToString();
                mobile = dr[6].ToString();
                address = dr[7].ToString();
                city = dr[8].ToString();
                region = dr[9].ToString();
                industry = dr[10].ToString();
                division = dr[11].ToString();


               UpdateDatabase(id, contactPerson, designation, company, emailaddress, contact, 
                mobile, address,city,region,industry,division);
            }
            else
            {
                break;
            }




        }
       lblmessage.Text = "Data Has Been Updated Successfully";
        mycon.Close();

        File.Delete(ExcelPath);
    }
    catch (Exception ex)
    {

        Console.WriteLine(ex.Message);
    }


}
//designation, company, emailaddress, contact, mobile, address,city,region,industry,division

private void UpdateDatabase(int id, String contactPerson, String designation, String company, String emailaddress, 
                             String contact, String mobile, String address,String city,String region,String industry,
                             String division)
{


    String query = "insert into Tbl_TempExcelData (id,contactperson,designation,company,email,contact,mobile,address,city,region,industry,division) values('" + id + "','" + contactPerson + "', '" + designation + "','" + company + "','" + emailaddress + "','" + contact + "','" + mobile + "','" + address + "','" + city + "','" + region + "','" + industry + "','" + division + "')";

    //String mycon = "Data Source=Ali-PC\\SQLEXPRESS; Initial Catalog=ExcelDatabase; Integrated Security=True";
    String mycon = "Data Source=Ali-PC;Initial Catalog=MushkhoApp;Integrated Security=True";
    SqlConnection con = new SqlConnection(mycon);
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = query;
    cmd.Connection = con;
    cmd.ExecuteNonQuery();

}

【问题讨论】:

    标签: c# asp.net sql-server-2008 sql-server-2012


    【解决方案1】:

    在 sql server 中创建用户定义类型,其中包含要插入的所有阀门

    在该用户 Sql server 合并语句之后,在一个查询中插入和更新记录。

    How to use Sql Merge

    在 Sql Merge 语句中,将用户定义的表变量用作源表,而要插入的表将其用作目标表。

    【讨论】:

    • 我先将我的记录插入到一​​个临时表中,然后我必须插入到一个原始表中,如何在 asp.net 中使用代码端的 sql 合并,你能指导我吗
    • 您可以使用临时物理表作为源而不是用户定义的表类型。但只能在sql端而不是在asp.net端。
    • 你能指导我如何在我的代码中使用这个 SQL 合并,因为我是 asp.net 的新手
    • 以上链接“如何使用 Sql 合并”中的示例是最好和简单的。试着理解并使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多