C# Excel数据未插入c web app中的sql数据库,但无异常

C# Excel数据未插入c web app中的sql数据库,但无异常,c#,sql-server,excel,C#,Sql Server,Excel,我正在编写一个web应用程序,将excel数据上载到sql数据库。应用程序运行时没有错误。但是excel数据不会进入数据库。但是,当我第二次运行应用程序时,它会给出错误“违反主键约束”PK_Category。无法在对象“dbo.Category”中插入重复键。但这怎么可能呢,因为第一次没有数据进入数据库 这是我的密码 [HttpPost] public string excelUploder() { string status; str

我正在编写一个web应用程序,将excel数据上载到sql数据库。应用程序运行时没有错误。但是excel数据不会进入数据库。但是,当我第二次运行应用程序时,它会给出错误“违反主键约束”PK_Category。无法在对象“dbo.Category”中插入重复键。但这怎么可能呢,因为第一次没有数据进入数据库

这是我的密码

 [HttpPost]
    public string excelUploder()        {


        string status;
        string fileparth;
        string json;
        using (var reader = new StreamReader(Request.InputStream))
        {
            json = reader.ReadToEnd();
        }
        JObject jo = (JObject)JsonConvert.DeserializeObject(json);
      //  get the file path of excel file as ‘fileparth’
        fileparth = jo.Value<string>("uplaodFile");
        string conString = string.Empty;
        string extension = Path.GetExtension(fileparth);

        switch (extension)
        {
            case ".xls": //Excel 97-03
                conString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'", fileparth);


                break;
            case ".xlsx": //Excel 07 or higher
                conString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=YES'", fileparth);

                break;

        }

        using (OleDbConnection excel_con = new OleDbConnection(conString))
        {
            excel_con.Open();
            string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();
            DataTable dtExcelData = new DataTable();

            dtExcelData.Columns.AddRange(new DataColumn[3] { new DataColumn("CatCode", typeof(string)),
            new DataColumn("CatName", typeof(string)),
            new DataColumn("HieCode",typeof(string)) });
     //CatCode,CatName,HieCode are the excel table column values and also as same as the name of db table 
            using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + sheet1 + "]", excel_con))
            {
                oda.Fill(dtExcelData);
            }
            excel_con.Close();

            string consString = ConfigurationManager.ConnectionStrings["GatePassDemoConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(consString))
            {
                using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
                {
                    //Set the database table name
                    sqlBulkCopy.DestinationTableName = "dbo.Category";

                    //[OPTIONAL]: Map the Excel columns with that of the database table
                    sqlBulkCopy.ColumnMappings.Add("CatCode", "CatCode");
                    sqlBulkCopy.ColumnMappings.Add("CatName", "CatName");
                    sqlBulkCopy.ColumnMappings.Add("HieCode", "HieCode");
                    con.Open();
                    try
                    {
                        sqlBulkCopy.WriteToServer(dtExcelData);
                        status = "Succesfully copyed";
                    }
                    catch (Exception e){
                        status = e.ToString();
                    }
                    con.Close();
                }
            }
        }
        return status;
    }
我的html代码

            <input type="file" name="datafile" size="40" id="fileUploder">

请帮我解决这个问题。谢谢。

伙计,看起来数据实际上插入了数据库中。尝试在表上执行SELECT命令。是否可以尝试调试并确认在执行sqlBulkCopy.WriteToServerdtExcelData时,dtExcelData实际上包含数据?@king.code,出现第二个错误时,表中的数据似乎第一次丢失,请尝试将数据上传到db。但是他们不在房间里table@sr28在这一点上,当我调试时,sqlBulkCopy.WriteToServerdtExcelData dtExcelData显示为null。这是怎么发生的请帮我解决这个问题。感谢您调试您的方法并检查您的连接字符串是否指向您要查找的正确数据库
            <input type="file" name="datafile" size="40" id="fileUploder">