且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

插入excel记录一次,不重复

更新时间:2022-12-26 12:44:39

如果你事先无法验证数据,我想你需要在插入之前查找记录,看它是否存在?
If you can't validate your data beforehand, I'd imagine you need to look for a record before you insert it, to see if it exists?


所以我删除了循环,当我将数据插入数据库表时,数据不再重复,谢谢



参考:href =https://***.com/questions/1438083/getting-the-first-sheet-from-an-excel-document-regardless-of -sheet-name-with-ole



so i removed the loop and the data no longer gets duplicated when i insert it into the database table, thanks

reference: href="https://***.com/questions/1438083/getting-the-first-sheet-from-an-excel-document-regardless-of-sheet-name-with-ole"

using (OleDbConnection connection = new OleDbConnection(sConStr))
    {
        connection.Open();
        /// get sheet name
       dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
       //var sheets = dt.Rows[0].Field<string>("TABLE_NAME");
       // foreach(var sheet in sheets) //loop through the collection of sheets ;)
       // {
       var sheets = dt.Rows[0].Field<string>("TABLE_NAME");
            //your logic here...
                    string myexceldataquery = string.Format("Select * FROM [{0}]; ", sheets);
                    //get data