private void unidadesBtn_Click(object sender, EventArgs e)
{
var owner_response = proxy.GetOwners();
if (owner_response != null)
{
DataTable vehicleData = new DataTable();
foreach (var owner in owner_response.Owners)
{
var vehicle_response = proxy.GetVehicles(owner.OwnerId);
if (vehicle_response != null)
{
vehicleData.Columns.Add("TypeDisplayName", typeof(SqlString));
vehicleData.Columns.Add("DefaultDriverId", typeof(Guid));
vehicleData.Columns.Add("VehicleId", typeof(Guid));
vehicleData.Columns.Add("OwnerId", typeof(Guid));
vehicleData.Columns.Add("DisplayName", typeof(SqlString));
vehicleData.Columns.Add("Description", typeof(SqlString));
vehicleData.Columns.Add("Registration", typeof(SqlString));
vehicleData.Columns.Add("Notes", typeof(SqlString));
vehicleData.Columns.Add("VehicleTypeId", typeof(Guid));
vehicleData.Columns.Add("VehicleProfileId", typeof(Guid));
vehicleData.Columns.Add("NotifyDriverOfGeofenceAlert", typeof(Boolean));
vehicleData.Columns.Add("IsProfile", typeof(Boolean));
vehicleData.Columns.Add("VehicleGroupId", typeof(Guid));
vehicleData.Columns.Add("CreatedDateTime", typeof(DateTime));
vehicleData.Columns.Add("ModifiedDateTime", typeof(DateTime));
vehicleData.Columns.Add("UserId", typeof(Guid));
vehicleData.Columns.Add("UniqueVehicleId", typeof(SqlString));
vehicleData.Columns.Add("DefaultVechileStateId", typeof(Boolean));
vehicleData.Columns.Add("GISCulture", typeof(SqlString));
foreach (var vehicle in vehicle_response.Vehicles)
{
vehicleData.Rows.Add(vehicle.TypeDisplayName.ToCharArray(), vehicle.DefaultDriverId, vehicle.VehicleId, vehicle.OwnerId,
vehicle.DisplayName.ToCharArray(), vehicle.Description.ToCharArray(), vehicle.Registration.ToCharArray(), vehicle.Notes.ToCharArray(),
vehicle.VehicleTypeId, vehicle.VehicleProfileId, vehicle.NotifyDriverOfGeofenceAlert, vehicle.IsProfile, vehicle.VehicleGroupId,
vehicle.CreatedDateTime, vehicle.ModifiedDateTime, vehicle.UserId, vehicle.UniqueVehicleID.ToCharArray(),
vehicle.DefaultVehicleStateID, vehicle.GISCulture.ToCharArray());
}
string nombre = owner.OwnerName;
}
}
if (vehicleData.Rows.Count > 0)
{
SqlConnection con = new SqlConnection(@"Data Source=10.113.192.122\SQLEXPRESS;Initial Catalog=FOO;Persist Security Info=True;User ID=Navman;Password=Pato745477;Initial Catalog=Navman");
SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
bc.DestinationTableName = "Vehicles";
bc.BatchSize = vehicleData.Rows.Count;
con.Open();
SqlCommand cmd = new SqlCommand("DELETE FROM dbo.Vehicles");
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
cmd.ExecuteNonQuery();
bc.WriteToServer(vehicleData);
bc.Close();
con.Close();
}
}
}

VulpesRank #55 · 29.2K reputationPosted Feb 28, 2014, 2:46 PM EST
http://msdn.microsoft.com/en-us/library/ms172138(v=vs.110).aspx
Patricio Llaguno7 reputationPosted Feb 28, 2014, 2:49 PM EST
VulpesRank #55 · 29.2K reputationPosted Feb 28, 2014, 3:07 PM EST
Patricio Llaguno7 reputationPosted Feb 28, 2014, 3:19 PM EST
VulpesRank #55 · 29.2K reputationPosted Feb 28, 2014, 3:24 PM EST
If you are, then I'd forget about setting MaxLength and just go with PadRight.
Patricio Llaguno7 reputationPosted Mar 1, 2014, 6:36 AM EST
VulpesRank #55 · 29.2K reputationPosted Mar 1, 2014, 7:56 AM EST
vehicleData.Columns.Add("TypeDisplayName", typeof(SqlString)).MaxLength = 50;
As it's no longer a fixed length column, you won't need to use PadRight and, as long as there's no chance of the TypeDisplayName property exceeding 50 characters, I'd expect the following to work now:
(vehicle.TypeDisplayName != null) ? new SqlString(vehicle.TypeDisplayName)) : SqlString.Null
If it could exceed 50 characters, then you'll need to use the Substring method to truncate it to the required size.
I'd only use a fixed length string type in the database if the values will always be exactly 'n' characters long. Otherwise I'd use varchar or nvarchar with 'n' set to the maximum conceivable length of the string, rounded up a bit to be on the safe side.
Sanjay Dholakiya82 reputationPosted Mar 2, 2014, 1:56 PM EST
You have at lease one of following property with null value in it.
- TypeDisplayName
- DisplayName
- Description
- Registration
- Notes
- vehicle
- UniqueVehicleID
- GISCulture
- Sanjay