Exemple #1
0
        public StoreData LoadStoreData()
        {
            StoreData storeData = new StoreData();

            storeData.ReturnCode    = "S900";
            storeData.ReturnMessage = "";
            DataSet DS = new DataSet();

            try
            {
                // 把引號'變成''以防止隱碼攻擊
                //Account = Account.Replace("'", "''");
                //Password = Password.Replace("'", "''");

                using (SqlConnection sqlConn = new SqlConnection(connectionString))
                {
                    SqlCommand sqlCmd = new SqlCommand();
                    sqlCmd.Connection = sqlConn;
                    sqlConn.Open();

                    Log.Debug("連線資訊 :" + sqlConn.ToString());

                    // 讀取老鼠資料 寫入DS資料列
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    adapter.SelectCommand = new SqlCommand("SELECT * FROM Store_ItemData", sqlConn);
                    adapter.Fill(DS);
                }
                // 若有讀到則 取得所有資料
                if (DS.Tables[0].Rows.Count > 0)
                {
                    int i = 0, j = 0;

                    foreach (DataTable table in DS.Tables)
                    {
                        Dictionary <string, object> dictData = new Dictionary <string, object>();
                        string itemID = "";

                        foreach (DataRow row in table.Rows)
                        {
                            j = 0;
                            Dictionary <string, object> dictData2 = new Dictionary <string, object>();
                            foreach (DataColumn col in table.Columns)
                            {
                                if (j == 0)
                                {
                                    itemID = table.Rows[i][col].ToString();
                                }
                                dictData2.Add(col.ColumnName, table.Rows[i][col].ToString());
                                j++;

                                Log.Debug(dictData2[col.ColumnName]);
                            }

                            dictData.Add(itemID, dictData2);
                            Log.Debug(dictData[itemID]);
                            i++;
                        }
                        storeData.StoreItem = Json.Serialize(dictData);
                    }
                    storeData.ReturnCode    = "S901"; //true
                    storeData.ReturnMessage = "取得商店資料成功!";
                }
                else
                {
                    storeData.ReturnCode    = "904";
                    storeData.ReturnMessage = "取得商店資料失敗!";
                }
            }
            catch (Exception e)
            {
                storeData.ReturnCode    = "S999";
                storeData.ReturnMessage = "載入商店資料例外情況!";
                throw e;
            }

            return(storeData); //回傳資料
        }