2017-02-18 11 views
2

私の研究に基づいて、.NETコアはDataTable/DataSetをサポートしていないようです。私は最近、新しいアプリケーションを開発するために.netコアに移行しましたが、最初に.netコアにはライブラリがないことを認識できませんでした。私はストアドプロシージャを介してデータをフェッチし、クラスのコレクションを埋めるためにdt/dsを使用しました。しかし、今、私はこの新しい問題でかなり失われています.dt/dsの代替品を見つけなければなりません。具体的には、私はこれをやっていました:.NETコアのDataTable/DataSetの置換

私は4つの入力パラメータを取り、テーブルを返すストアドプロシージャを持っています。

ALTER PROCEDURE stp_Student 

@Name nvarchar(450), 
@StudentIds tvp_ArrayInt READONLY, 
@StartDate date, 
@EndDate date, 

AS 

blah blah 

//returns student summary 
SELECT stu.StudentId, 
     stu.StudentName, 
     CASE WHEN (COUNT(DISTINCT course.Id) IS NOT NULL) THEN COUNT(DISTINCT course.Id) ELSE 0 END AS CourseCount, 
     CASE WHEN (SUM(course.TotalCourses) IS NOT NULL) THEN SUM(course.TotalCourses) ELSE 0 END AS TotalCourses, 
     CASE WHEN (SUM(course.Hours) IS NOT NULL) THEN SUM(course.Hours) ELSE 0 END AS Hours 
FROM #TempStudent AS #Temp 

と私は、ストアドプロシージャを持っているのと同じフィールドを持つクラスを作成します。

public class StudentModel 
{ 
    public string StudentId { get; set; } 
    public string StudentName { get; set; } 
    public int CourseCount { get; set; } 
    [Column(TypeName = "Money")] 
    public decimal TotalCourses { get; set; } 
    public double Hours { get; set; } 
} 

とデータをフェッチし、クラスのコレクション

using (SqlConnection conn = new SqlConnection(connectionString)) 
{ 
    SqlCommand cmd = new SqlCommand("sp_Student", conn); 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.Parameters.AddWithValue("@Name", name); 
    cmd.Parameters.AddWithValue("@StudentIds", studentIds); 
    cmd.Parameters.AddWithValue("@StartDate", startDate); 
    cmd.Parameters.AddWithValue("@EndDate", endDate); 
    conn.Open(); 

    SqlDataAdapter da = new SqlDataAdapter(); 
    da.SelectCommand = cmd; 
    da.Fill(ds); 
    conn.Close(); 
} 

foreach (Datarow row in ds.Tables[0].Rows) 
{ 
    var model = new StudentModel(); 
    model.StudentId = Convert.ToString(row["StudentId"]); 
    //etc.. 
    studentList.Add(model); 
} 

マイ悪いを埋め、私はプロジェクトを開始する前に、私はより多くを研究している必要がありますが、私は実際に上記のコードの代替品を見つけるために誰の助けを必要としています。 .netコアのFromSqlメソッドを代わりに使用することは可能でしょうか?どんな提案やサンプルコードも感謝します。

編集

はピーターの記事によると、.NETコア版ではDS/dtのを実装する方法を見つけましたが、私のストアドプロシージャにint配列(StudentIds)を通過ビットの問題を持ちます。

public List<StudentModel> GetStudents(string name, IEnumerable<int> studentIds, DateTime startDate, DateTime endDate) 
    { 
     List<StudentModel> students = new List<StudentModel>(); 
     StudentModel = null; 

     List<DbParameter> parameterList = new List<DbParameter>(); 

     parameterList.Add(base.GetParameter("Name", name)); 
     parameterList.Add(base.GetParameter("StudentIds", studentIds));   
     parameterList.Add(base.GetParameter("StartDate", startDate)); 
     parameterList.Add(base.GetParameter("EndDate", endDate));  

     using (DbDataReader dataReader = base.ExecuteReader("stp_Student", parameterList, CommandType.StoredProcedure)) 
     { 
      if (dataReader != null) 
      { 
       while (dataReader.Read()) 
       { 
        model = new StudentModel(); 
        model.StudentId= (int)dataReader["StudentId"]; 
        .... 

        students .Add(model); 
       } 
      } 
     } 
     return students; 
    } 
} 

IEnumerableを渡すと問題が発生すると思いますか? int配列をストアドプロシージャに正しく渡すにはどうすればよいですか?

+2

それは、次の版ãに来る。またhttps://github.com/dotnet/corefx/pull/12426 –

+1

を参照してください、https://social.technet.microsoft.com/wikiを見てみましょう/contents/articles/35974.exploring-net-core-net-core-1-0-connecting-sql-server-database.aspx#ExecuteReader –

+0

私のストアドプロシージャがいくつかのテーブルを返す以外は、この問題もあります。私のコードでは、結果をデータセットに割り当て、そこから各テーブルにアクセスします。 Microsoftはまた、ハーフベークド製品の使用を強要しています。 –

答えて

1

.Net Coreについてはあまりよく分かりませんが、代わりにIEnumerable<SqlDataRecord>を使用できると思います。

2

DataTableが.NET Core 2.0に追加されました。私の答えはhttps://blogs.msdn.microsoft.com/devfish/2017/05/15/exploring-datatable-and-sqldbadapter-in-asp-net-core-2-0/です。

public static DataTable ExecuteDataTableSqlDA(SqlConnection conn, CommandType cmdType, string cmdText, SqlParameter[] cmdParms) 
{ 
System.Data.DataTable dt = new DataTable(); 
System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter(cmdText, conn); 
da.Fill(dt); 
return dt; 
} 
+0

ネット標準1.5との互換性はありますか?データセットは使用できません。 2.0はプレビュー版で、生産準備はできていません。 – soydachi

関連する問題