Beispiel #1
0
        public static void CreateClient(string name, string phoneNumber, bool orderDone, List <Cars> cars)
        {
            Client data = new Client
            {
                Name        = name,
                PhoneNumber = phoneNumber,
                OrderDone   = orderDone,
                Cars        = cars
            };

            string sql = @"insert into dbo.Client (Name, PhoneNumber, OrderDone) 
                           values (@Name, @PhoneNumber, @OrderDone);";

            SqlDataAccess.SaveData(sql, data);

            var    carName   = ConvertToEnglish(name) + "Cars";
            string sqlcreate = @"create table [dbo].[" + carName + @"] 
                                ([Id] INT NOT NULL PRIMARY KEY IDENTITY,
                                [CarName] NVARCHAR(50) NOT NULL,
                                [Model] NVARCHAR(50) NOT NULL,
                                [VIN] NVARCHAR(50) NOT NULL,
                                [EngineVolume] FLOAT NOT NULL, 
                                [ManufactureYear] DATETIME NOT NULL, 
                                [Defects] NVARCHAR(50) NOT NULL,
                                [ClientId] NVARCHAR(50) NOT NULL);";

            using (IDbConnection cnn = new SqlConnection(SqlDataAccess.GetConnectionString()))
            {
                cnn.Execute(sqlcreate);
            }

            foreach (var c in cars)
            {
                Car car = new Car
                {
                    CarName         = ConvertFromEnum(c.Name),
                    Model           = c.Model,
                    VIN             = c.VIN,
                    EngineVolume    = (float)c.EngineVolume,
                    ManufactureYear = c.ManufactureYear,
                    Defects         = GetFromList(c.DefectsDescription),
                    ClientId        = data.Name
                };

                string sqldata = @"insert into dbo." + carName + @" (CarName, Model, VIN, EngineVolume, ManufactureYear, Defects, ClientId) 
                                values (@CarName, @Model, @VIN, @EngineVolume, @ManufactureYear, @Defects, @ClientId);";

                SqlDataAccess.SaveData(sqldata, car);
            }
        }