클래스에서 속성을 설정하고 결과 집합을 화면에 출력하고 파일에 쓰는 데이터베이스에서 읽는 간단한 응용 프로그램이 있습니다. 내 응용 프로그램이 웹 응용 프로그램이 될 수 있도록 미리 계획을 세우려고 노력하고 있으며 가능한 한 모범 사례에 근접하도록 설계하려고합니다. 내 DAL 디자인에 큰 결함이 있는지, 사용자로부터 입력을 받아 매개 변수로 설정하는 방법이 정상적인 지 또는 더 좋은 방법이 있는지 알고 싶습니다. 프로그램의 모든 내용이 예상대로 작동합니다.데이터 액세스 레이어의 저장 프로 시저 사용
DAL
public static List<Customers> GetCustomersByName()
{
//make the list of the type that the method will be returning
List<Customers> c = new List<Customers>();
//make a connection string variable
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
using(SqlCommand cmd = new SqlCommand("spFindCustomersByName",con))
{
con.Open();
//this stored procedure has one input parameter, how do I send that to the data access layer?
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@lastName", SqlDbType.VarChar, 50);
//only way I could think of to get the value entered from a screen into
//a parameter
cmd.Parameters["@lastName"].Value = Customers.AddSpParams();
//instantiate SqlDataReader
SqlDataReader rdr = cmd.ExecuteReader();
while(rdr.Read())
{
Customers custList = new Customers();
custList.CustomerId = Convert.ToInt32(rdr["customerId"]);
custList.LastName = rdr["lastName"].ToString();
custList.FirstName = rdr["firstName"].ToString();
custList.DateHired = (DateTime)rdr["dateHired"];
c.Add(custList);
}
}
return c;
}
방법은 기본적으로
static void Main(string[] args)
{
Console.WriteLine("This is only a test");
List<Customers> c = DataAccessCustomers.GetCustomersByName();
using (StreamWriter sw = new StreamWriter(@"C:\Users\customersList.txt"))
{
foreach(Customers custList in c)
{
//write to console
Console.WriteLine(custList.CustomerId + "\t" + custList.FirstName + "\t" +
custList.LastName + "\t" + custList.DateHired);
//write to file
sw.WriteLine(custList.CustomerId + "\t" + custList.FirstName + "\t" +
custList.LastName + "\t" + custList.DateHired);
}
}
Console.ReadLine();
}
대신 코드 검토로 이동해야합니다. –
그 존재를 몰랐습니다. 내가 어떻게 그럴 수 있니? – wootscootinboogie
여기에서 찾을 수 있습니다 : http://codereview.stackexchange.com/ –