0

データベースから読み取ってクラスのプロパティを設定し、結果セットを画面に出力してファイルに書き込む単純なアプリケーションを作成しました。アプリケーションを Web アプリケーションにすることを前もって計画しようとしていますが、可能な限りベスト プラクティスに近い設計にしたいと考えています。私の 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;
            }

ストアドプロシージャの入力パラメータに値を代入する方法

public static string AddSpParams()
        {
            Console.Write("Search for a string in customer's name: ");
            string nameParam = Console.ReadLine();
            return nameParam;
        }

テキスト ファイルへの書き込み、コンソールへの書き込み

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();
        }
4

1 に答える 1

2

基本的に、ここでの設計例外の小さな点は問題ありません。あなたのストア プロシージャとCustomerクラスは null 値をサポートしていないと思います。したがって、ここでの主な大きな欠陥は、コードがまだ DBNull 値を処理していないことです。

custList.LastName = rdr["lastName"].ToString();
custList.FirstName = rdr["firstName"].ToString();
custList.DateHired = (DateTime)rdr["dateHired"];

.ToString()呼び出しまたはボックス化の前に、rdr["lastName"] および rdr["firstName"] の DBNull 値を確認する必要があります(DateTime)rdr["dateHired"];

于 2013-04-24T13:51:48.607 に答える