خرید بک لینک

Vote count: 0

I am hoping someone could check my context of how I am using Transaction with MySql. I believe this should work with the outline below. Can someone look at my code and tell me if I am doing it correctly? Thank you.

I believe this should:

  • Instantiate the db coection.
  • Iterate through the DataTable rows of the given DataTable.
  • Check to see if the table exists and if it does not it will Execute the Create Table.
  • Execute Insert Command with Parameters of information into the newly created or existing table.
  • Commit the Transaction and then close the coection.

        //Open the SQL Coection
        var dbCoection = new MySqlCoection(GetCoectionString(WowDatabase));
        dbCoection.Open();
        //Instantiate the Command
        using (var cmd = new MySqlCommand())
        {
            //Create a new Transaction
            using (var transaction = dbCoection.BeginTransaction())
            {
                uint lastId = 999999;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    //var identifier = dt.Rows[i].Field<int>("Identifier");
                    var id = dt.Rows[i].Field<uint>("Entry");
                    var name = dt.Rows[i].Field<string>("Name");
                    var zone = dt.Rows[i].Field<uint>("ZoneID");
                    var map = dt.Rows[i].Field<uint>("MapID");
                    var state = dt.Rows[i].Field<Enums.ItemState>("State");
                    var type = dt.Rows[i].Field<Enums.ObjectType>("Type");
                    var faction = dt.Rows[i].Field<Enums.FactionType>("Faction");
                    var x = dt.Rows[i].Field<float>("X");
                    var y = dt.Rows[i].Field<float>("Y");
                    var z = dt.Rows[i].Field<float>("Z");
                    string dataTableName = "entry_" + id;
                    //Create Table if it does not exist.
                    if (id != lastId)
                    {
                        cmd.CommandText = $"CREATE TABLE IF NOT EXISTS `{dataTableName}` (" +
                                          "`identifier`   int NOT NULL AUTO_INCREMENT COMMENT 'Auto Incriment Identifier' ," +
                                          "`zone_id`      int NULL COMMENT 'Zone Entry' ," +
                                          "`x_axis`       float NULL COMMENT 'X Axis on Map' ," +
                                          "`y_axis`       float NULL COMMENT 'Y Axis on Map' ," +
                                          "`z_axis`       float NULL COMMENT 'Z Axis on Map' ," +
                                          "`situation`    enum('') NULL COMMENT 'Location of the item. Underground, Indoors, Outdoors)' ," +
                                          "`faction`      enum('') NULL COMMENT 'Specifies the Faction which can safely access the item.' ," +
                                          "PRIMARY KEY(`identifier`)" +
                                          ")";
                        cmd.ExecuteNonQuery();
                        lastId = id;
                    }
                    //Create command to execute the insertion of Data into desired Table
                    cmd.CommandText = $"INSERT INTO [{dataTableName}] " +
                                           "([identifier], [zone_id], [x_axis], [y_axis], [z_axis], [situation], [faction], [Create_Date], [Update_Date]) " +
                                           "VALUES (@Identifier, @Zone_Id, @X_Axis, @Y_Axis, @Z_Axis, @Situation, @Faction, @Create_Date, @Update_Date)";
                    //Add data value with Parameters.
                    cmd.CommandType = CommandType.Text;
                    //cmd.Parameters.AddWithValue("@Identifier", identifier);
                    cmd.Parameters.AddWithValue("@Identifier", id);
                    cmd.Parameters.AddWithValue("@Zone_Id", zone);
                    cmd.Parameters.AddWithValue("@X_Axis", x);
                    cmd.Parameters.AddWithValue("@Y_Axis", y);
                    cmd.Parameters.AddWithValue("@Z_Axis", z);
                    cmd.Parameters.AddWithValue("@Situation", state);
                    cmd.Parameters.AddWithValue("@Faction", faction);
                    cmd.Parameters.AddWithValue("@Create_Date", DateTime.Now.Date);
                    cmd.Parameters.AddWithValue("@Update_Date", DateTime.Now.Date);
    
                    cmd.ExecuteNonQuery();
                } //for (int i = 0; i < dt.Rows.Count; i++)
    
                //Commit the Transaction
                transaction.Commit();
            } //using (var transaction = dbCoection.BeginTransaction())
        } //using (var cmd = new MySqlCommand())
    
        //Close the Coection
        dbCoection.Close();
    
    
asked 2 mins ago

برچسب: نویسنده: استخدام کار تاريخ: پنجشنبه 19 فروردين 1395 ساعت: 2:56

صفحه بندی