C# Windows フォーム アプリケーションで DataAccess レイヤーを呼び出そうとすると、このエラーが発生し続けます。
私の設計では、インターフェイスとクラスを使用しました。
iDataAccess と呼ばれるインターフェイスと、その中のコードは次のようになります。
IDataAccess.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
public interface IDataAccess
{
object GetScalar(string sql);
DataTable GetDataTable(string sql);
int InsOrUpdOrDel(string sql);
}
DataAccess というクラスとその中のコードは次のようになります。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public class DataAccess : IDataAccess
{
public static string CONNSTR = ConfigurationManager.ConnectionStrings["SqlDbConnect"].ConnectionString;
public DataAccess()
{
}
public System.Data.DataTable GetDataTable(string sql)
{
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(CONNSTR);
try
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.Fill(dt);
}
catch (Exception)
{
throw;
}
finally
{
conn.Close();
}
return dt;
}
}
ここで、form.cs コードで、前述のクラスのオブジェクトを作成したいと考えています。このため、以下に示すように、2 つの異なる方法を試しましたが、どれもうまくいきませんでした。
IDataAccess _idataAccess = new DataAccess();
// インスタンス障害エラーが発生します
IDataAccess _idataAccess = null;
// エラー: オブジェクト参照がオブジェクトのインスタンスに設定されていません
誰でもこの問題について教えてもらえますか?