【解決方法】データテーブルを別のファイルにインポートするにはどうすればよいですか?


そのため、C# に関する知識を向上させようとしており、データ構造とアルゴリズムに関する多くの例がある https://www.dotnetperls.com/ という Web サイトを見つけました。 また、データテーブルについて: https://www.dotnetperls.com/datatable. 2 番目の例では、最初の例と同じ DataTable を使用し、コード内のコメントは、 //(please paste it in)

ここで、DataTable を使用してファイル全体をインポートすることで、それを回避しようとしました。 しかし、どこを探しても説明がありません。 これらは2つの例です。

C#
using System;
using System.Data;

class Program
{
    static void Main()
    {
        // Step 1: get the DataTable.
        DataTable table = GetTable();
        
        // Step 4: print the first cell.
        Console.WriteLine("FIRST ROW, DOSAGE: {0}", table.Rows[0]["Dosage"]);
    }
    
    static DataTable GetTable()
    {
        // Step 2: here we create a DataTable.
        // ... We add 4 columns, each with a Type.
        DataTable table = new DataTable();
        table.Columns.Add("Dosage", typeof(int));
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Diagnosis", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));
        
        // Step 3: here we add rows.
        table.Rows.Add(25, "Drug A", "Disease A", DateTime.Now);
        table.Rows.Add(50, "Drug Z", "Problem Z", DateTime.Now);
        table.Rows.Add(10, "Drug Q", "Disorder Q", DateTime.Now);
        table.Rows.Add(21, "Medicine A", "Diagnosis A", DateTime.Now);
        return table;
    }
}
C#
using System;
using System.Data;

class Program
{
    static void Main()
    {
        // This uses the GetTable method (please paste it in).
        DataTable data = GetTable();
        
        // ... Loop over all rows.
        foreach (DataRow row in data.Rows)
        {
            // ... Write value of first field as integer.
            Console.WriteLine(row.Field<int>(0));
        }
    }
}

私が試したこと:

dotnet.microsoft または stackoverflow を調べてみましたが、エラーのない解決策が得られません。 それらを同じフォルダー内の同じ名前空間に配置し、メイン関数の名前を変更しました。 first_example.Program.Main(args);
first_example.Program.GetTable();

私は他の言語からのアプローチを試みました:
using first_example.Program;

しかし、DataTable を持つプログラムを別のプログラムに C# でインポートする方法について、適切な説明が見つかりません。 より良い知識を持っている人は、私を正しい方向に向けてもらえますか?

解決策 3

アプリケーション間でコードを共有する場合は、「クラス ライブラリ」プロジェクトを作成し、両方のアプリケーションから参照する必要があります。

Visual Studio を使用して .NET クラス ライブラリを作成する – .NET | マイクロソフト ラーン[^]

クラス ライブラリで:

C#
using System;
using System.Data;

namespace MyClassLibrary
{
    public static class MySampleData
    {
        public static DataTable GetTable()
        {
            // Step 2: here we create a DataTable.
            // ... We add 4 columns, each with a Type.
            DataTable table = new DataTable();
            table.Columns.Add("Dosage", typeof(int));
            table.Columns.Add("Drug", typeof(string));
            table.Columns.Add("Diagnosis", typeof(string));
            table.Columns.Add("Date", typeof(DateTime));
            
            // Step 3: here we add rows.
            table.Rows.Add(25, "Drug A", "Disease A", DateTime.Now);
            table.Rows.Add(50, "Drug Z", "Problem Z", DateTime.Now);
            table.Rows.Add(10, "Drug Q", "Disorder Q", DateTime.Now);
            table.Rows.Add(21, "Medicine A", "Diagnosis A", DateTime.Now);
            return table;
        }
    }
}

アプリケーション 1 では:

C#
using System;
using System.Data;
using MyClassLibrary;

static class Program
{
    static void Main()
    {
        // Step 1: get the DataTable.
        DataTable table = MySampleData.GetTable();
        
        // Step 4: print the first cell.
        Console.WriteLine("FIRST ROW, DOSAGE: {0}", table.Rows[0]["Dosage"]);
    }
}

アプリケーション 2 では:

C#
using System;
using System.Data;
using MyClassLibrary;

static class Program
{
    static void Main()
    {
        // Step 1: get the DataTable.
        DataTable data = MySampleData.GetTable();
        
        // ... Loop over all rows.
        foreach (DataRow row in data.Rows)
        {
            // ... Write value of first field as integer.
            Console.WriteLine(row.Field<int>(0));
        }
    }
}

解決策 1

多分、詳細がわかりにくかったと思います。 あなたがする必要があるのは、の実際のテキストをコピーすることです GetTable メソッドを最初のソース ファイルから取得し、2 番目のソース ファイルに貼り付けます。 したがって、2 番目のソース ファイルは次のようになります。

C#
using System;
using System.Data;

class Program
{
    static void Main()
    {
        // This uses the GetTable method (please paste it in).
        DataTable data = GetTable();
        
        // ... Loop over all rows.
        foreach (DataRow row in data.Rows)
        {
            // ... Write value of first field as integer.
            Console.WriteLine(row.Field<int>(0));
        }
    }

// copied from program 1 and pasted here
    static DataTable GetTable()
    {
        // Step 2: here we create a DataTable.
        // ... We add 4 columns, each with a Type.
        DataTable table = new DataTable();
        table.Columns.Add("Dosage", typeof(int));
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Diagnosis", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));
        
        // Step 3: here we add rows.
        table.Rows.Add(25, "Drug A", "Disease A", DateTime.Now);
        table.Rows.Add(50, "Drug Z", "Problem Z", DateTime.Now);
        table.Rows.Add(10, "Drug Q", "Disorder Q", DateTime.Now);
        table.Rows.Add(21, "Medicine A", "Diagnosis A", DateTime.Now);
        return table;
    }
// end of pasted code
}

コメント

タイトルとURLをコピーしました