简体   繁体   中英

C# DataTable and SqlBulkCopy

I have a problem with my code :

DataTable tablePmeElement = new DataTable("PMEELEMENT");

DataColumn codeBiblioColumn = new DataColumn();
codeBiblioColumn.DataType = System.Type.GetType("System.String");
codeBiblioColumn.ColumnName = "CODEBIBLIO";
codeBiblioColumn.AllowDBNull = false;
codeBiblioColumn.DefaultValue = "";
codeBiblioColumn.MaxLength = 3;

tablePmeElement.Columns.Add(codeBiblioColumn);

element.CODEBIBLIO = (this.bibMde.BibCode.Length > 3) ? 
this.bibMde.BibCode.Substring(0, 3) : this.bibMde.BibCode;

tablePmeElement.Rows.Add(element);

s.WriteToServer(tablePmeElement);

When I execute this code, I get an error:

Column: "CODEBIBLIO" contains data with a length greater than: 3.

Have you an idea to solve my problem because I didn't find any solution.

When you set the MaxLength property on a DataColumn , you need to make sure the character length of the data returned for this column is under the property value. For instance, if you set codeBiblioColumn.MaxLength = 3 and the database returns a value larger than 3 characters, you will get a run-time error. To resolve this, either increase the MaxLength value, or decrease the length of the values stored in your database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM