저는 C#에서 3 단계 아키텍처의 기본 예제를 수행했습니다. 데이터 및 비즈니스 레이어에 대해 두 dll을 만들었습니다. 또한 비즈니스 레이어 코드에 데이터 레이어 dll을 사용하고 있습니다. 비즈니스 dll 및 프리젠 테이션 레이어 코드가 실행되고 부탁해 (A의 WinForm 응용 프로그램입니다) 프리젠 테이션 계층에서 데이터 액세스 dll은 예외가 밝히는오고있다 :데이터 레이어에서 데이터를 읽을 수 없습니다
데이터베이스 'D : \ 11feb의 \ 연습 \ 3tier \ PresentationLayer \ PresentationLayer \ bin \ Debug \ Data.mdf ' 존재하지 않습니다.
데이터 레이어에 Data.mdf
데이터베이스를 만들었습니다. 예외로 언급 한 위치에 데이터베이스 파일을 복사했는데 응용 프로그램이 성공적으로 실행되었습니다. 그러나 데이터 영역에서 데이터베이스에 액세스하려고합니다.
데이터 계층 코드 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DataAccessLayer
{
public class DataAccess
{
public DataTable dataRead()
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Data.mdf;Database=Data;Integrated Security=True;User Instance=True");
DataTable dt = new DataTable();
con.Open();
SqlCommand cmd = new SqlCommand("select ID,Name from datatable", con);
try
{
SqlDataReader rd = cmd.ExecuteReader();
dt.Load(rd);
return dt;
}
catch
{
throw;
}
}
}
}
비즈니스 계층 코드 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;
using System.Data;
namespace BusinessLogicLayer
{
public class BusinessLogic
{
DataAccess dataAccess = new DataAccess();
public DataTable getPersons()
{
try
{
return dataAccess.dataRead();
}
catch { throw; }
}
}
}
프리젠 테이션 레이어 코드 :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogicLayer;
namespace PresentationLayer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
BusinessLogic BusinessLogic = new BusinessLogic();
this.dataGridView1.DataSource = BusinessLogic.getPersons();
}
catch
{
MessageBox.Show("Error Occurred");
}
}
}
}
데이터베이스를 마우스 오른쪽 버튼으로 클릭하고 연결 문자열을보고 코드에서 사용하십시오. – Sachin