请教各位大侠 有什么好方法没

解决方案 »

  1.   


    将DataTable的TableName属性设置为数据库中表的名称,然后调用该方法public static string ExecuteInsert(DataTable dt)
      {
       
       string sql="";
       string fieldStr="";
       string valueStr="";
       int i=0;
       try
       { 
         
        for(i=0;i<dt.Columns.Count;i++)
        {
         fieldStr += dt.Columns[i].ColumnName + ",";     
        }    fieldStr = fieldStr.Substring(0,fieldStr.Length-1);   
       
        for(i=0;i<dt.Rows.Count;i++)
        {
         sql = "insert into {0}({1}) values({2})";
         valueStr ="";
         for(int j=0;j<dt.Columns.Count;j++)
         {
          
          switch(System.Type.GetTypeCode(dt.Rows[i][j].GetType()))
          {
           case System.TypeCode.Byte:
           case System.TypeCode.Char:
           case System.TypeCode.Decimal:
           case System.TypeCode.Double:
           case System.TypeCode.Int16:
           case System.TypeCode.Int32:
           case System.TypeCode.Int64:
           case System.TypeCode.SByte:
           case System.TypeCode.Single:
           case System.TypeCode.UInt16:
           case System.TypeCode.UInt32:
           case System.TypeCode.UInt64:
            valueStr += dt.Rows[i][j].ToString() +",";
            break;
           case System.TypeCode.DateTime:
           case System.TypeCode.String:       
            valueStr += "'" + dt.Rows[i][j].ToString() +"',";
            break;
           case System.TypeCode.Boolean:
           {
            if ((bool)dt.Rows[i][j]==true)
             valueStr += "1,";
            else
             valueStr += "0,";        break;
           }
           default:
            valueStr +=" null,";
            break; 
          }      
         }
         
         valueStr = valueStr.Substring(0,valueStr.Length-1); 
         sql = string.Format(sql,dt.TableName,fieldStr,valueStr);
         ExecuteNonQuery(sql); 
        
        }
        return "ok";
       }   catch(Exception ex)
       {
        return ex.Message;   }
      }
      

  2.   

    都到DataTable了,不就想怎么存就怎么存了嗎?
      

  3.   

      只能做循环 拼接SQL 插入数据库!
      

  4.   


     SqlConnection  myConnection   =  New   SqlConnection(strConn)   ;
       SqlDataAdapter      adapterupdate2   =   New   SqlDataAdapter   ;        
      For   int   =   1   To   i   
                                      String  strInsertMes   ;  
                                     String   N_Time   ;  
                                      strInsertMes   =   "insert   into   BOMD   values("   
                                      strInsertMes   +=   "'"   &   CODP_ITEM.Text   &   "'"   
                                      strInsertMes   +=   ","   &   "'"   &   L_datatable.Rows(int   -   1).Item(0)   &   "'"   
                                      strInsertMes   +=   ","   &   "'"   &   L_datatable.Rows(int   -   1).Item(1)   &   "'"   
                                      strInsertMes   +=   ","   &   "'"   &   L_datatable.Rows(int   -   1).Item(2)   &   "'"   
                                      strInsertMes   +=   ","   &   "'"   &   L_datatable.Rows(int   -   1).Item(3)   &   "'"   
                                      strInsertMes   +=   ","   &   "'"   &   L_datatable.Rows(int   -   1).Item(4)   &   "'"   
                                      strInsertMes   +=   ","   &   "'"   &   L_datatable.Rows(int   -   1).Item(5)   &   "'"   
                                      strInsertMes   +=   ","   &   "'"   &   L_datatable.Rows(int   -   1).Item(6)   &   "'"   
                                      strInsertMes   +=   ","   &   "'"   &   L_datatable.Rows(int   -   1).Item(7)   &   "'"   
                                      strInsertMes   +=   ","   &   "'"   &   type   &   "'"   
                                      strInsertMes   +=   ")"   
                                      adapterupdate2.SelectCommand   =   New   SqlCommand(strInsertMes,   myConnection)   
                                      adapterupdate2.SelectCommand.ExecuteNonQuery()   
                              Next   int
      

  5.   


    在.Net1.1中无论是对于批量插入整个DataTable中的所有数据到数据库中,还是进行不同数据源之间的迁移,都不是很方便。而在.Net2.0中,SQLClient命名空间下增加了几个新类帮助我们通过DataTable或DataReader批量迁移数据。数据源可以来自关系数据库或者XML文件,甚至WebService返回结果。其中最重要的一个类就是SqlBulkCopy类,使用它可以很方便的帮助我们把数据源的数据迁移到目标数据库中。
    下面我们先通过一个简单的例子说明这个类的使用:DateTime startTime;
         protected void Button1_Click(object sender, EventArgs e)
         {
             startTime = DateTime.Now;
             string SrcConString;
             string DesConString;
             SqlConnection SrcCon = new SqlConnection();
             SqlConnection DesCon = new SqlConnection();
             SqlCommand SrcCom = new SqlCommand();
             SqlDataAdapter SrcAdapter = new SqlDataAdapter();
             DataTable dt = new DataTable();
             SrcConString =
             ConfigurationManager.ConnectionStrings["SrcDBConnectionString"].ConnectionString;
             DesConString =
             ConfigurationManager.ConnectionStrings["DesDBConnectionString"].ConnectionString;
             SrcCon.ConnectionString = SrcConString;
             SrcCom.Connection = SrcCon;
             SrcCom.CommandText = " SELECT * From [SrcTable]";
             SrcCom.CommandType = CommandType.Text;
             SrcCom.Connection.Open();
             SrcAdapter.SelectCommand = SrcCom;
             SrcAdapter.Fill(dt);
             SqlBulkCopy DesBulkOp;
             DesBulkOp = new SqlBulkCopy(DesConString,
             SqlBulkCopyOptions.UseInternalTransaction);
             DesBulkOp.BulkCopyTimeout = 500000000;
             DesBulkOp.SqlRowsCopied +=
             new SqlRowsCopiedEventHandler(OnRowsCopied);
             DesBulkOp.NotifyAfter = dt.Rows.Count;
             try
             {
                 DesBulkOp.DestinationTableName = "SrcTable";
                 DesBulkOp.WriteToServer(dt);
             }
             catch (Exception ex)
             {
                 lblResult.Text = ex.Message;
             }
             finally
             {
                 SrcCon.Close();
                 DesCon.Close();
             }
         }
         private void OnRowsCopied(object sender, SqlRowsCopiedEventArgs args)
         {
             lblCounter.Text += args.RowsCopied.ToString() + " rows are copied<Br>";
             TimeSpan copyTime = DateTime.Now - startTime;
             lblCounter.Text += "Copy Time:" + copyTime.Seconds.ToString() + "." + copyTime.Milliseconds.ToString() + " seconds";
         }
    接着具体分析这几行代码:
    SqlBulkCopy DesBulkOp;
    DesBulkOp = new SqlBulkCopy(DesConString, SqlBulkCopyOptions.UseInternalTransaction);先生成SqlBulkCopy 实例,构造函数指定了目标数据库,使用SqlBulkCopyOptions.UseInternalTransaction是指迁移动作指定在一个Transaction当中,如果数据迁移中产生错误或异常将发生回滚。其他选项请参考MSDN。
    DesBulkOp.BulkCopyTimeout = 500000000;
    指定操作完成的Timeout时间
             DesBulkOp.SqlRowsCopied += new SqlRowsCopiedEventHandler(OnRowsCopied);
             DesBulkOp.NotifyAfter = dt.Rows.Count;
             try
             {
                 DesBulkOp.DestinationTableName = "SrcTable";
                 DesBulkOp.WriteToServer(dt);
             }
    NotifyAfter属性指定通知通知事件前处理的数据行数,在这里指定为表的行数,并添加SqlRowsCopied事件输出整个迁移过程的时间。WriteToServer方法就是将数据源拷备到目标数据库。在使用WriteToServer方法之前必须先指定DestinationTableName属性,也就是目标数据库的表名,
    我们还可以自己定义一个Transaction,例如:
    SqlTransaction Transaction;
    Transaction =
    SrcCom.Connection.BeginTransaction();
    SqlBulkCopy DesBulkOp;
    DesBulkOp = new SqlBulkCopy(new SqlConnection(DesConString),
    SqlBulkCopyOptions.Default,
    Transaction);
    try
    {   
    //..
    }
    catch{}   
    finally
    {
    Transaction.Commit();
    }
    另外还有一个SqlBulkCopyColumnMapping类,可以让数据源字段映射到目标数据中命名不同的字段上。也就是说如果目标数据和源数据的列名不同时,可以用这个类进行映射:
    SqlBulkCopyColumnMapping ColMap = new SqlBulkCopyColumnMapping("SrcCol", "DesCol");
    DesBulkOp.ColumnMappings.Add(ColMap);
    或者可以直接添加映射:
    DesBulkOp.ColumnMappings.Add("SrcCol", "DesCol");
    性能问题:
    我使用上面的例子测试,迁移了2万条左右的记录,花的时间不到一秒,应改说性能还是不错的。另外,使用SQL Profile监视迁移事件,可以看见请求记录非常少,只有几条而已。据说使用SqlBulkCopy可以大大减少数据迁移的时间
      

  6.   

    如果你要写入到数据库的Data不仅仅是读取excel得到的datatable里的Data,比如要加上SeqNo等信息时,可能只能循环写入了