本帖最后由 joeyxiaobin 于 2011-06-18 23:53:02 编辑

解决方案 »

  1.   

    既然你是执行多条语句,那参数赋值应该放在你的循环中。
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data.OracleClient;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    using System.Diagnostics;namespace ORACLEDB.DAL
    {
      public class Insert
      {
      private readonly string sqlConnString = ConfigurationManager.ConnectionStrings["SQLConnection"].ToString();
      private readonly string sqlOwner = ConfigurationManager.ConnectionStrings["SQLOwner"].ToString();
      public void Insrtdata()
      {
      int recc = 10;
      using (SqlConnection conn = new SqlConnection(sqlConnString))
      {
      SqlCommand objCommand = new SqlCommand();
      objCommand.Connection = conn;
      objCommand.CommandText = "insert into TEST4 values(:IDParam,:NAMEParam,:CLASSParam)";
      conn.Open();
      string[] ID = new string[recc];
      string[] NAME = new string[recc];
      string[] CLASS = new string[recc]; 
      Stopwatch sw = new Stopwatch();
      sw.Start();
      for (int i = 0; i < recc; i++)
      {
         ID[i] = i.ToString();
      NAME[i] = i.ToString();
      CLASS[i] = i.ToString(); SqlParameter IDParam = new SqlParameter("ID", SqlDbType.NChar);
      IDParam.Direction = ParameterDirection.Input;
      IDParam.Value =  ID[i];
     objCommand.Parameters.Add(IDParam);  SqlParameter NAMEParam = new SqlParameter("NAME",SqlDbType.NChar);
      NAMEParam.Direction = ParameterDirection.Input;
      NAMEParam.Value = NAME[i]; 
     objCommand.Parameters.Add(NAMEParam);  SqlParameter CLASSParam = new SqlParameter("CLASS",SqlDbType.NChar);
      CLASSParam.Direction = ParameterDirection.Input;
      CLASSParam.Value = CLASS[i];
     objCommand.Parameters.Add(CLASSParam);
        objCommand.ExecuteNonQuery();
      }  sw.Stop();
      Debug.WriteLine("批量插入"+recc.ToString()+"条记录占用时间:"+sw.ElapsedMilliseconds.ToString());
      }
      }
      }
    }
      

  2.   

    TO:taomanman,你好,感谢你回复!
    按照你的提示,结果还是不行,还是报错,还是在objCommand.ExecuteNonQuery();报错,提示未处理InvalidCastException,将参数值从 String[] 转换到 String 失败。
    以下是异常详细信息:
    ——————————————————
    未处理 System.InvalidCastException
      Message="将参数值从 String[] 转换到 String 失败。"
      Source="System.Data"
      StackTrace:
           在 System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType)
           在 System.Data.SqlClient.SqlParameter.GetCoercedValue()
           在 System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc)
           在 System.Data.SqlClient.SqlCommand.BuildParamList(TdsParser parser, SqlParameterCollection parameters)
           在 System.Data.SqlClient.SqlCommand.BuildExecuteSql(CommandBehavior behavior, String commandText, SqlParameterCollection parameters, _SqlRPC& rpc)
           在 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
           在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
           在 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
           在 System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
           在 ORACLEDB.DAL.Insert.Insrtdata() 位置 D:\Study\C#\练习\ORACLEDB\ORACLEDB.DAL\Insert.cs:行号 59
           在 ORACLEDB.InsertData.btnInsert_Click(Object sender, EventArgs e) 位置 D:\Study\C#\练习\ORACLEDB\ORACLEDB\InsertData.cs:行号 32
           在 System.Windows.Forms.Control.OnClick(EventArgs e)
           在 System.Windows.Forms.Button.OnClick(EventArgs e)
           在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           在 System.Windows.Forms.Control.WndProc(Message& m)
           在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
           在 System.Windows.Forms.Button.WndProc(Message& m)
           在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.Run(Form mainForm)
           在 ORACLEDB.Program.Main() 位置 D:\Study\C#\练习\ORACLEDB\ORACLEDB\Program.cs:行号 18
           在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           在 System.Threading.ThreadHelper.ThreadStart()
      InnerException: System.InvalidCastException
           Message="对象必须实现 IConvertible。"
           Source="mscorlib"
           StackTrace:
                在 System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
                在 System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType)
           InnerException: 
      

  3.   


    IDParam.Value = ID;这句的问题前面在数组库中是SqlDbType.NChar类型的。。而你后面是个数组
      

  4.   

    TO:porschev
    该如何修改的,我是新手,望不吝赐教,谢谢你
      

  5.   


    从你的结构。。我都看不出你要做什么?你是要循环插入recc条数据吗?
      

  6.   

    对,看看插入recc条数据到表里的时间,目前是10条,我想测试下这种方法插入百万条数据的速度怎么样?
      

  7.   


    那你为什么不写一个插入一条语句的方法Insrtdata把业务写在数据访问做什么执行多少次。。for(int i = 0 ; i <recc ; i ++ )
    {
      Insrtdata();
    }
      

  8.   

    //插入方法
       private readonly string sqlConnString = ConfigurationManager.ConnectionStrings["SQLConnection"].ToString();
            private readonly string sqlOwner = ConfigurationManager.ConnectionStrings["SQLOwner"].ToString();
            public void Insrtdata(string id,string name,string classStr)
            {            
                using (SqlConnection conn = new SqlConnection(sqlConnString))
                {
                    SqlCommand objCommand = new SqlCommand();
                    objCommand.Connection = conn;
                    objCommand.CommandText = "insert into TEST4 values(:IDParam,:NAMEParam,:CLASSParam)";
                    conn.Open();         
                    SqlParameter IDParam = new SqlParameter("ID", SqlDbType.NChar);
                    IDParam.Direction = ParameterDirection.Input;
                    IDParam.Value = id;
                    objCommand.Parameters.Add(IDParam);
                    SqlParameter NAMEParam = new SqlParameter("NAME", SqlDbType.NChar);
                    NAMEParam.Direction = ParameterDirection.Input;
                    NAMEParam.Value = name;
                    objCommand.Parameters.Add(NAMEParam);
                    SqlParameter CLASSParam = new SqlParameter("CLASS", SqlDbType.NChar);
                    CLASSParam.Direction = ParameterDirection.Input;
                    CLASSParam.Value = classStr;
                    objCommand.Parameters.Add(CLASSParam);
                    objCommand.ExecuteNonQuery();                                
                }
            }//测式时间
    public void Test()
            {
                int count = 10;
                System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
                sw.Start();
                for (int i = 0; i < count; i++)
                {
                    string flag = i.ToString();
                    Insrtdata(flag, flag, flag);
                }
                sw.Stop();
                Debug.WriteLine("批量插入" + count.ToString() + "条记录占用时间:" + sw.ElapsedMilliseconds.ToString());
            }
      

  9.   

    感谢porschev;
    我这个例子主要是想测试大数据量的数据采集所耗的时间的一个算法,也是看到网络上有个人写的算法,因此自己拿过来测试下,所以代码写的不够规范,按照porschev的提示,已经可以运行成功,但是运行的效果并非和那算法预计的一样,可能代码中还需要优化,我运行的效果是:插入1万条记录大概6S,但是网络上写这个算法的人说百万条数据仅需1s,可能我的代码还需要优化,感谢各位的答复,谢谢!