0

これは、パス内のファイルのパスを定義する必要があるコードです。「文字列パス」内には、次のようなものがあります。

(C:\フォルダ\text.txt)

string path = @"C:\path.txt";
StreamReader str = new StreamReader(path);
string datasample;

while ((datasample = str.ReadLine()) != null)
      {
      }   

私がする必要があるのは、txtファイルをSQLテーブルにインポートすることです.hereは正常に動作するインポート用のコードです。

StreamReader sr = new StreamReader(Global_Variables.filename.ToString());
             DataTable dt = new DataTable();
                DataRow row;
                dt.Columns.Add(new DataColumn("Reports"));

                while (!sr.EndOfStream)
                {
                    string value = sr.ReadLine();
                    if ((value.Length != 0) && (value != ""))
                    {
                        row = dt.NewRow();
                        row[0] = value;
                        dt.Rows.Add(row);
                    }
                }
                SqlBulkCopy bc = new SqlBulkCopy(Global_Variables.con.ConnectionString, SqlBulkCopyOptions.TableLock);
                bc.DestinationTableName = "tbl_WinApps_ApprovedExpReports";
                bc.BatchSize = dt.Rows.Count;
                Global_Variables.con.Open();
                bc.WriteToServer(dt);
                bc.BulkCopyTimeout = 120;
                bc.Close();
                Global_Variables.con.Close();

2つのコードを一緒に結合することはできません.pls help :(

4

1 に答える 1

0

StreamReader を使用してファイルを既にループしているように見えるので、次のようなことができます。

string path = File.ReadAllText(@"C:\path.txt");
string line = "";
StreamReader sr = new StreamReader(path);
DataTable dt = new DataTable();
DataRow row;
dt.Columns.Add(new DataColumn("Reports"));

while ((line = sr.readLine()) != null)
{
    if ((line.Length != 0) && (line != ""))
    {
        row = dt.NewRow();
        row[0] = line;
        dt.Rows.Add(row);
    }
}

SqlBulkCopy bc = new SqlBulkCopy(Global_Variables.con.ConnectionString, SqlBulkCopyOptions.TableLock);
bc.DestinationTableName = "tbl_WinApps_ApprovedExpReports";
bc.BatchSize = dt.Rows.Count;
Global_Variables.con.Open();
bc.WriteToServer(dt);
bc.BulkCopyTimeout = 120;
bc.Close();
Global_Variables.con.Close();
于 2013-01-07T08:36:36.543 に答える