The field is too small to accept the amount of data you attempted to add. Has anyone gotten this error from ADO.NET?

匿名 (未验证) 提交于 2019-12-03 02:31:01

问题:

Updating an Excel file from a dataset seems to work fine. But if I have text that's longer than 255 characters then I get the error above. Has anyone else gotten such an error? How do you fix it? I've been working on this for a couple of hours and I haven't gotten anywhere. I've tried messing with the connection string and changing a registry setting, but no luck.

回答1:

There is no easy fix for this. Matter of fact I had to use a hack of sorts.

I tried to insert some text in an Excel field that was 262 characters long and got this error: The field is too small to accept the amount of data you attempted to add. Try inserting or pasting less data.

This trick/hack works easily because I already have the Excel document created and it's empty (ie. no rows except the header). So in Excel, I pasted text that was 262 characters (it can be anything over 255) into the cells of the first row that could receive text that big. Afterwards, I ran ADO.NET (thru VB.NET) and pushed the data thru a dataset (ds.Update) to Excel and all the data went over nicely with no errors.

Unless someone knows of a way in Excel or code-behind to force the Excel cells to be what's called Memo fields, this is the only way that worked. I tried the trick with the registry but it didn't work for me.



回答2:

I used OleDb and with dotnetN00b's tip I managed to do it.

        string con = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\temp\\test.xls;Extended Properties=""Excel 8.0;HDR=YES;""");                     var cnn = new OleDbConnection(con);         cnn.Open();          string createCom = "CREATE TABLE [Sheet1] ( [A] string, [B] Memo);";         string insertCom = "INSERT INTO [Sheet1] VALUES('This can be max 255', 'This one can be realy looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong!');";          OleDbCommand cmd = new OleDbCommand(createCom, cnn);         cmd.ExecuteNonQuery();          cmd = new OleDbCommand(insertCom, cnn);         cmd.ExecuteNonQuery();          cnn.Close(); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!