代码不在循环外运行

时间:2014-01-24 18:45:58

标签: c# sql sharpdevelop

编辑:我正在使用SharpDevelop

我是C#的新手,所以答案可能很简单......我有一些代码(下面),WHILE循环运行得很好。问题是,一旦WHILE循环中的处理完成,就不再执行代码了。如果我在'cn.Open()上放置一个断点;排队并运行程序,我从来没有打过那个断点。如果我在'cn.Open();正上方的大括号'}'上放置一个断点一行,代码将在每次遇到该断点时停止。我不确定如何运行其他代码。

void MainFormLoad(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    string line = null;
    int i = 0;
    SqlConnection cn = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Sandbox;Data Source=test");

    StreamReader sr = File.OpenText(@"C:\Users\rl\Desktop\TEST_I~1.CSV");

          while ((line = sr.ReadLine()) != null)
          {
                string[] data = line.Split(',');
                if (data.Length > 0)
                {
                      if (i == 0)
                      {
                          foreach (var item in data)
                          {
                            dt.Columns.Add(item.ToString());
                          }
                          i++;
                     }
                 DataRow row = dt.NewRow();
                 row.ItemArray = data;
                 dt.Rows.Add(row);
                 }
          }
    cn.Open();
    SqlBulkCopy copy = new SqlBulkCopy(cn);
    {
    //            copy.ColumnMappings.Add(0, 0);
    //            copy.ColumnMappings.Add(1, 1);
    //            copy.ColumnMappings.Add(2, 2);
    //            copy.ColumnMappings.Add(3, 3);
    //            copy.ColumnMappings.Add(4, 4);
    copy.DestinationTableName = "Member2";
    copy.WriteToServer(dt);
    }

3 个答案:

答案 0 :(得分:1)

您可能想要解决一些问题。这些可能与您使用#develop调试的任何问题有关,也可能没有。

  1. 在使用它们之前很久就会声明事物(样式指南)
  2. 不处理实施IDisposable的内容(使用using语句!)
  3. 内部范围块; copy变量正在其自己的范围内使用,没有明显的理由(我可能错了,但它可能是为了循环而抛出#development的调试器)
  4. 相反,您的代码应该更接近于此:

    void MainFormLoad(object sender, EventArgs e)
    {
        var dt = new DataTable();
    
        // You may want to pass other parameters to OpenText for read mode, etc.
        using (var sr = File.OpenText(@"C:\Users\rl\Desktop\TEST_I~1.CSV"))
        {
            var first = true;
            string line = null;
    
            while ((line = sr.ReadLine()) != null)
            {
                string[] data = line.Split(',');
                if (data.Length > 0)
                {
                    if (first)
                    {
                        foreach (var item in data)
                        {
                            dt.Columns.Add(item.ToString());
                        }
                        first = false;
                        // Don't add the first row's data in the table (headers?)
                        continue; 
                    }
                    var row = dt.NewRow();
                    row.ItemArray = data;
                    dt.Rows.Add(row);
                }
            }
        }
    
        using (var cn = new SqlConnection("<connection string>"))
        {
            cn.Open();
            using (var copy = new SqlBulkCopy(cn))
            {
                // copy.ColumnMappings.Add(0, 0);
                // copy.ColumnMappings.Add(1, 1);
                // copy.ColumnMappings.Add(2, 2);
                // copy.ColumnMappings.Add(3, 3);
                // copy.ColumnMappings.Add(4, 4);
                copy.DestinationTableName = "Member2";
                copy.WriteToServer(dt);
            }
        }
    
    }
    

答案 1 :(得分:0)

代码有点奇怪,但它看起来应该有效。可能存在文件锁定,使您对旧版本运行或挂在.csv打开行。

Cory's suggestions用于整理代码非常好。

答案 2 :(得分:-3)

我认为你有一个无限循环,因为你的while检查不太正确。您询问line = sr.ReadLine()是否为空,而不是line为空。将line设置为读取函数结果的结果将永远不会返回null。