/// <summary> ///uploading local information to the server/// </summary> Public Static voidUploaddatatoserver () {Try { #region1. Read Local dataStringBuilder Sbselect=NewStringBuilder (); foreach(stringTblnameinchcmmconstants.uploadtablenames) {//querying for data that has not been uploadedSbselect.append (string. Format ("SELECT * from {0} WHERE isupload= ' 0 ' OR isupload is NULL;", tblname)); } Sqlitedbmanager _sqlitedbmanager=NewSqlitedbmanager (); DataSet DS= _sqlitedbmanager.getdataset (Sbselect.tostring (),NULL); for(inti =0; i < CMMConstants.UPLOADTABLENAMES.Length; i++) { //Set dataTableNameDs. Tables[i]. TableName =Cmmconstants.uploadtablenames[i]; } #endregion #region2. Uploading to the server//Server data AccessDBManager _dbmanager =NewDBManager (); //get a list of fields for a server tableDataSet Dscolumns =_dbmanager.getcolumns (CMMConstants.UPLOADTABLENAMES.ToList ()); List<SqlBulkCopyColumnMapping> mappingcollection =NULL; DataTable dttemp=NULL; DataTable Dtdata=NULL; DataTable dtcopy=NULL; stringScolumnname =NULL; //converting GUID column types and copying data table dataFunc<datatable> SetType = () = { //A collection of column names that need to be set to a GUID type string[] ColumnNames =New string[] {"Shiyongid","shiyanshiid","zuoweiid","shiyongmingxiid","Shukajiluid" }; DataTable Dtsettype=Dtdata.clone (); foreach(stringColumnNameinchcolumnnames) { if(DtSetType.Columns.Contains (columnName)) {//Set Column typeDtsettype.columns[columnname]. DataType =typeof(GUID); } } //Populating DataDtsettype.load (Dtdata.createdatareader ()); returnDtsettype; }; //Looping multiple sheets foreach(stringTblnameinchcmmconstants.uploadtablenames) {dtdata=ds. Tables[tblname]; Dttemp=Dscolumns.tables[tblname]; Mappingcollection=NewList<sqlbulkcopycolumnmapping>(); if(DtData.Rows.Count >0) { //a collection of columns in a looping table foreach(DataRow rowinchdttemp.rows) {scolumnname= row["name"]. ToString (); if(DtData.Columns.Contains (scolumnname)) {//Add MappingsMappingcollection.add (Newsqlbulkcopycolumnmapping (Scolumnname, scolumnname)); } } //Gets the data table after converting the column typeDtcopy =SetType (); //updating data to the server_dbmanager.bulkcopy (Tblname, dtcopy, mappingcollection); //Update Local data upload status stringsKeyName =Cmmconstants.uploadtablenames_primarykey[tblname]; stringSinstr =InStr (dtcopy, sKeyName); stringSupdatesql =string. Format ("UPDATE {0} SET isupload= ' {1} ' where {2} in ({3})", Tblname,"1", sKeyName, SINSTR); intIresult =_sqlitedbmanager.exesql (Supdatesql); } } #endregion } Catch(Exception ex) {ThrowEx } }
Ps:
The column type cannot be changed until the DataTable is processed without Func, a string-to-GUID exception is reported, and the DataTable column type is directly modified to have data.
The Func process may not be the best, and it might be better to change the bulkcopy to use a custom table type and pass the data to the server for writing.
Write SQLite data to SQL Server