我向assess里循环插入数据,如我我加上断点F10单步运行,不报错误,插入无误。可如果我不加断点直接运行,也不会报错,可就是总会少了第1条记录。我实在无奈了。大家有没有遇到过相同问题,帮帮忙

解决方案 »

  1.   

     private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog openfile = new OpenFileDialog();
                openfile.RestoreDirectory = true;
                openfile.Multiselect = false;
                openfile.InitialDirectory = "";
                openfile.Filter = "TXT文件(*.txt)|*.txt";
                openfile.FilterIndex = 1;
                if (openfile.ShowDialog() == DialogResult.OK)
                {
                    //获取文件路径
                    string localFilePath = openfile.FileName.ToString();
                    //获取文件名
                    string fileName = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1, (openfile.FileName.Length - openfile.FileName.LastIndexOf("\\") - 5));
                    //获取文件路径,不带文件名   
                    string FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\") + 1);
                    //创建MDB文件
                    string dbName = FilePath + fileName + ".mdb";
                    ADOX.CatalogClass cat = new ADOX.CatalogClass();
                    try
                    {
                        cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName + ";");                }
                    catch (Exception)
                    {                    MessageBox.Show("该记录已存在!!");
                        this.Dispose();
                    }
                    ADOX.TableClass tbl = new ADOX.TableClass();
                    tbl.ParentCatalog = cat;
                    tbl.Name = fileName;
                    ADOX.ColumnClass col = new ADOX.ColumnClass();
                    col.ParentCatalog = cat;
                    col.Type = ADOX.DataTypeEnum.adInteger;   //   必须先设置字段类型  
                    col.Name = "id";
                    col.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
                    col.Properties["AutoIncrement"].Value = true;
                    tbl.Columns.Append(col, ADOX.DataTypeEnum.adInteger, 0);
                    //读取txt文件
                    StreamReader objReader = new StreamReader(openfile.FileName.ToString(), System.Text.Encoding.Default);
                    string sLine = "";
                    List<string> listLine = new List<string>();
                    while (sLine != null)
                    {
                        sLine = objReader.ReadLine();
                        if (sLine != null)
                        {
                            listLine.Add(sLine);
                        }
                    }
                    objReader.Close();
                    //添加字段
                    string strZiduan = "";
                    if (listLine[0] != null || listLine[0] != "")
                    {
                        string strLing = listLine[0];
                        string[] data = strLing.Split(',');
                       
                        for (int j = 0; j < data.Length; j++)
                        {
                            ADOX.ColumnClass col2 = new ADOX.ColumnClass();
                            col2.ParentCatalog = cat;
                            col2.Name = "字段" + (j + 1).ToString();
                            strZiduan = strZiduan + "字段" + (j + 1).ToString() + ",";
                            col2.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
                            tbl.Columns.Append(col2, ADOX.DataTypeEnum.adVarChar, 100);
                        }
                    }
                    //设置主键  
                    tbl.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "id", "", "");
                    cat.Tables.Append(tbl);
                    tbl = null;
                    cat = null;              
                    string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName;                for (int x = 0; x < listLine.Count; x++)
                    {
                        string[] data1 = listLine[x].Split(',');
                        string strValues = string.Empty;
                        for (int z = 0; z < data1.Length; z++)
                        {
                            strValues += "'" + data1[z] + "'" + ",";
                        }
                        string subStr = strValues.Substring(0, strValues.Length - 1);
                        OleDbConnection connection = new OleDbConnection(ConnectionString);
                        connection.Open();
                        string sql = "insert into " + fileName + "(" + strZiduan.Substring(0, strZiduan.Length - 1) + ") values(" + strValues.Substring(0, strValues.Length - 1) + ")";
                        OleDbCommand conn = new OleDbCommand(sql, connection);
                        conn.ExecuteNonQuery();
                        connection.Close();
                    }
                    MessageBox.Show("转换成功!!");                          
                    this.Dispose();
                }
            }
    我做的这是一个txt文件转换成mdb文件的程序。到最后的两个for循环出的问题,大家麻烦都帮忙看看
      

  2.   

    lz调试到
       for (int x = 0; x < listLine.Count; x++)
         for (int z = 0; z < data1.Length; z++)确认数据到这里已得到你想要的实际长度了没?
    还有,建议把for循环置如下:
    using(OleDbConnection connection = new OleDbConnection(ConnectionString))
    {
      connection.Open();
      StringBuilder sb =new StringBuilder();
       for(int ....){
         sb.appendLine("");
       }
       OleDbCommand conn = new OleDbCommand(sb, connection);
       conn.ExecuteNonQuery();
      }
      

  3.   

    把connection.Open();放在for循环开始之前,
    把connection.Close();放在for循环结束之后。
    这两句不要放在for循环内部。
    我猜测反复开关数据库连接造成的不同步。只是猜测,还希望楼主尝试一下。
      

  4.   

    应为在外边不行我才把connection.Open();放里边试试的,再有就是我如果调试的话,得到的结果就没问题,如果不调试才有问题。。
      

  5.   

    本来就不应重复多次打开connection,正如#7所述的,就有可能出现lz所说的问题,本来这个操作打开一次就可以了。
      

  6.   

    按照我的经验,把
    OleDbConnection connection = new OleDbConnection(ConnectionString);
    connection.Open();
    放在for循环之上,把
    connection.Close();
    放在for循环之后,是绝对没问题的,而且不影响for循环的执行。调试运行和不调试运行的差别在于:(我猜测)不调试执行时,有可能出现并发的情况。open和close并发执行造成问题。
      

  7.   

    连续运行和断点运行的区别,说不定是因为数据库需要一点准备时间造成第一条数据没有正确插入。可以这样,在插入之前或之后加一个SLEEP()语句试一下