示例#1
0
        /// <summary>
        /// Write addressbooks to JSON file
        /// </summary>
        /// <param name="addressBooks"></param>
        public static void WriteToJson(AddressBooks addressBooks)
        {
            DictToListMapping dictToList = new DictToListMapping(DictToListMapping.DictionaryToList(addressBooks));

            string jsonData = JsonConvert.SerializeObject(dictToList);

            File.WriteAllText(path, jsonData);
        }
示例#2
0
        public static void WriteToSqlServer(AddressBooks addressBooks)
        {
            DeleteAllRows();

            DictToListMapping dictToList = new DictToListMapping(DictToListMapping.DictionaryToList(addressBooks));

            DataSet   dataSet   = new DataSet();
            DataTable dataTable = new DataTable("AddressBook");

            dataTable.Columns.Add("AddressBookName", typeof(string));
            dataTable.Columns.Add("ContactName", typeof(string));
            dataTable.Columns.Add("FirstName", typeof(string));
            dataTable.Columns.Add("LastName", typeof(string));
            dataTable.Columns.Add("City", typeof(string));
            dataTable.Columns.Add("State", typeof(string));
            dataTable.Columns.Add("Zip", typeof(string));
            dataTable.Columns.Add("Phone", typeof(string));
            dataTable.Columns.Add("Email", typeof(string));
            dataTable.Columns.Add("DateAdded", typeof(DateTime));

            dataSet.Tables.Add(dataTable);
            dataSet.Tables[0].TableName = "AddressBook";
            for (int i = 0; i < dictToList.AddressBookName.Count; i++)
            {
                DataRow row = dataSet.Tables[0].NewRow();

                row[0] = dictToList.AddressBookName[i];
                row[1] = dictToList.ContactName[i];
                row[2] = dictToList.FirstName[i];
                row[3] = dictToList.LastName[i];
                row[4] = dictToList.City[i];
                row[5] = dictToList.State[i];
                row[6] = dictToList.ZipCode[i];
                row[7] = dictToList.PhoneNumber[i];
                row[8] = dictToList.Email[i];
                row[9] = dictToList.DateAdded[i];

                dataSet.Tables["AddressBook"].Rows.Add(row);
            }

            using (SqlConnection connection = new SqlConnection(path))
            {
                connection.Open();
                SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from AddressBook", connection);

                SqlCommandBuilder builder = new SqlCommandBuilder(dataAdapter);
                int rowsUpdated           = dataAdapter.Update(dataSet, "AddressBook");
                Console.WriteLine("Rows Affected " + rowsUpdated);
            }
        }
示例#3
0
        /// <summary>
        /// Write addressbooks to CSV file
        /// </summary>
        /// <param name="addressBooks"></param>
        public static void WriteToCsv(AddressBooks addressBooks)
        {
            DictToListMapping dictToList = new DictToListMapping(DictToListMapping.DictionaryToList(addressBooks));
            List <string[]>   records    = new List <string[]>();

            string[] person;

            for (int i = 0; i < dictToList.AddressBookName.Count; i++)
            {
                person = new string[9];

                person[0] = dictToList.AddressBookName[i];
                person[1] = dictToList.ContactName[i];
                person[2] = dictToList.FirstName[i];
                person[3] = dictToList.LastName[i];
                person[4] = dictToList.City[i];
                person[5] = dictToList.State[i];
                person[6] = dictToList.ZipCode[i];
                person[7] = dictToList.PhoneNumber[i];
                person[8] = dictToList.Email[i];

                records.Add(person);
            }

            Type type = dictToList.GetType();

            PropertyInfo[] propertyInfo = type.GetProperties();

            using (var writer = new StreamWriter(path))
            {
                writer.WriteLine(string.Join(",", propertyInfo.Select(p => p.Name)));
                for (int i = 0; i < records.Count; i++)
                {
                    writer.WriteLine(string.Join(",", records[i]));
                }
            }
        }