// writes the starmap to DB, and generates for each star a systemmap which uis also written to DB // See more at: http://www.sqlteam.com/article/use-sqlbulkcopy-to-quickly-load-data-from-your-client-to-sql-server#sthash.MRQfKJ44.dpuf static void CopyData(SqlConnection destConnection, List <Star> systemElements, MapGenerator.SystemGenerator.Workers.Worker systemGenerator, System.Windows.Forms.TextBox output) { using (SqlCommand ins = new SqlCommand("[engine].StarMapInsert", destConnection)) { ins.CommandType = CommandType.StoredProcedure; SqlParameter tvpParam = ins.Parameters.Add("@StarOrNebula", SqlDbType.Structured); tvpParam.TypeName = "[engine].StarMapInsertType"; var dataTable = new System.Data.DataTable(Guid.NewGuid().ToString()); DataColumn column; column = new DataColumn(); column.DataType = System.Type.GetType("System.Int32"); column.ColumnName = "X"; dataTable.Columns.Add(column); column = new DataColumn(); column.DataType = System.Type.GetType("System.Int32"); column.ColumnName = "Y"; dataTable.Columns.Add(column); column = new DataColumn(); column.DataType = System.Type.GetType("System.Int16"); column.ColumnName = "objectId"; dataTable.Columns.Add(column); column = new DataColumn(); column.DataType = System.Type.GetType("System.Int16"); column.ColumnName = "size"; dataTable.Columns.Add(column); column = new DataColumn(); column.DataType = System.Type.GetType("System.Int16"); column.ColumnName = "startSystem"; dataTable.Columns.Add(column); column = new DataColumn(); column.DataType = System.Type.GetType("System.Int32"); column.ColumnName = "id"; dataTable.Columns.Add(column); int counter = 0; //only up to 1000 are to be inserted at once // and now, do the work: for (int i = 0; i < systemElements.Count; i++) { if (systemElements[i].StarNebulaType == 3) { continue; } (new { X = systemElements[i].X, Y = systemElements[i].Y, objectId = systemElements[i].StarNebulaType == 1 ? systemElements[i].ObjectId : StarGenerator.getNebulaId(systemElements[i].StarNebulaType), // 5000, size = systemElements[i].StarNebulaType == 1 ? 24 : 1, startSystem = systemElements[i].StarNebulaType == 1 && systemElements[i].StartingSystem ? 1 : 0, id = i + 1 }).AddObjectToDataTable(dataTable); counter++; // create the system belonging to the star: if (systemElements[i].StarNebulaType == 1) { tvpParam.Value = dataTable; ins.ExecuteNonQuery(); counter = 0; output.Text = i.ToString() + " / " + systemElements.Count.ToString(); output.Refresh(); //this forces the label to redraw itself dataTable.Clear(); MapGenerator.SystemGenerator.SolarSystem x = systemGenerator.createSystem(false, false, systemElements[i].Type, systemElements[i].StartingSystem); SystemBulkInsert(i + 1, connectionString, x, output); //starIds begin at 1 ( it is not checked yet if 0 is supported by the javascript when starid is transfered there) } if (counter > 1000) { tvpParam.Value = dataTable; ins.ExecuteNonQuery(); //ins.ExecuteNonQuery(); counter = 0; output.Text = i.ToString() + " / " + systemElements.Count.ToString(); output.Refresh(); //this forces the label to redraw itself dataTable.Clear(); } } if (counter > 0) { tvpParam.Value = dataTable; //ins.ExecuteNonQuery(); ins.ExecuteNonQuery(); counter = 0; output.Text = systemElements.Count.ToString() + " / " + systemElements.Count.ToString() + Environment.NewLine; output.Refresh(); //this forces the label to redraw itself } } }
public DBWriter(List <Star> stars, MapGenerator.SystemGenerator.Workers.Worker systemGenerator) { Stars = stars; SystemGenerator = systemGenerator; }