zoukankan      html  css  js  c++  java
  • sql笨办法同步数据

    Helpers.SqlHelper sqlHelper = new Helpers.SqlHelper("server=***;database=Cms;user id=sa;password=123");
    
                string[] tableNames = { "A", "B", "C", "D", "E" };
                var sb = new StringBuilder();
                foreach (var tableName in tableNames)
                {
                    var dt = sqlHelper.ExecuteDataTable("select * from "+tableName+" where 0=0");
                    
                    sb.AppendLine("set identity_insert "+tableName+" on");
                    sb.AppendLine("truncate table  " + tableName + " ");
                    if (dt != null)
                    {
                        var val = string.Empty;
    
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (i%1000 == 0)
                            {
                                sb.AppendLine("insert into [" + tableName + "](");
                                for (int r = 0; r< dt.Columns.Count; r++)
                                {
                                    if (r > 0)
                                        sb.Append(",");
                                    sb.Append(dt.Columns[r].ColumnName);
                                }
                                sb.Append(")");
                                sb.AppendLine("values");
                            }
                            else
                                sb.AppendLine(",");
    
                            sb.Append("(");
                            for (int j = 0; j < dt.Columns.Count; j++)
                            {
                                if (j > 0)
                                    sb.Append(",");
    
                                val = dt.Rows[i][j].ToString();
                                if (dt.Columns[j].DataType == typeof (DateTime) || dt.Columns[j].DataType == typeof (int) ||
                                    dt.Columns[j].DataType == typeof (long))
                                {
                                    if (string.IsNullOrEmpty(val))
                                    {
                                        sb.Append("null");
                                    }
                                    else
                                    {
                                        sb.Append("'" + val + "'");
                                    }
                                }
                                else
                                {
                                    sb.Append("'" + val.Replace("'", "''") + "'");
                                }
                            }
                            sb.Append(")");
                        }
                    }
                    sb.AppendLine();
                    sb.AppendLine("set identity_insert " + tableName + " off");
                    sb.AppendLine();
                }
                var sql = sb.ToString();
    
                Console.Write(sql);
                Console.Read();

    identity表导入失败,所以就用了这种笨办法来同步数据

  • 相关阅读:
    PR工具栏(选择、轨道选择、剃刀工具)
    PPT动画与多媒体制作
    小微企业增值税申报操作流程
    标题样式
    样式排版
    文本框排版
    使用SmartArt
    插图片与调整
    表格的运用
    Word标尺与段落
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/6027220.html
Copyright © 2011-2022 走看看