Write SQLite data to SQL Server

Source: Internet
Author: User
Tags sqlite

       /// <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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.