简体   繁体   中英

Error - The field is too small to accept the amount of data you attempted to add. Try inserting or pasting less data

try
        {
            string CSVFilePathName = textBox4.Text;
            for (int i = 0; i < CSVFilePathName.Length; i++)
            {
                if (CSVFilePathName[i] == '\\')
                {
                    CSVFilePathName.Insert(i + 1, "\\");
                }
            }
                if (string.IsNullOrWhiteSpace(textBox4.Text))
                {
                    MessageBox.Show("Please Select a File");
                }
                else
                {
                    int count = 0;
                    // your code here 
                    // string CSVFilePathName = @"'" + textBox4.Text + "'";
                    string[] Lines = File.ReadAllLines(CSVFilePathName);
                    string[] Fields;
                    Fields = Lines[0].Split(new char[] { ',' });
                    int Cols = Fields.GetLength(0);
                    DataTable dt = new DataTable();
                    for (int i = 1; i < Lines.GetLength(0); i++)
                    {
                        Fields = Lines[i].Split(new char[] { ',' });
                        for (int f = 0; f < Cols; f++)
                        {
                            q = "SELECT * from questions where main_section='" + Fields[0] + "' AND secondary_section='" + Fields[1] + "' AND tert_section='" + Fields[2] + "' AND question='" + Fields[3] + "' AND difficulty='" + Fields[4] + "'";
                            OleDbCommand cmdn = new OleDbCommand(q, conn);
                            //MessageBox.Show(q);
                            object obj = cmdn.ExecuteScalar();
                            if (obj == null)
                            {
                                q = "insert into questions values('" + Fields[0] + "','" + Fields[1] + "','" + Fields[2] + "','" + Fields[3] + "' ,'" + Fields[4] + "')";
                                OleDbCommand cmdn1 = new OleDbCommand(q, conn);
                                cmdn1.ExecuteNonQuery();
                            }
                            else 
                            {
                                count++;
                            }
                            //MessageBox.Show(Fields[f]);
                        }

                    }
                    //  dataGridClients.DataSource = dt;
                    string msg = "Upload successful\n";
                    if (count > 0)
                    {
                        msg=count.ToString()+" Questions missed due to their duplicates in the database.";
                    }
                    MessageBox.Show(msg);
                }

        }
       catch (Exception ex)
       {
           MessageBox.Show("Error is " + ex.ToString());
           throw;
       }

I am using c# winform to upload a csv file to my ms access db, but it is givig the error "The field is too small to accept the amount of data you attempted to add. Try inserting or pasting less data." What should do now?

I suggest specifying the table fields in the SQL like

INSERT INTO questions (fieldname1, fieldname2, ...) VALUES (...)

Use parameterized-values rather than writing the values directly in the string. This also allows you to specify the datatype and then the ADO.Net OLE adapter will hopefully handle it appropriately and get the long text inserted with no trouble. Go to Read / Write BLOBs ... for an example inserting BLOBS. The concept and code example are very relevant to inserting Long Text values. It demonstrates how to set up parameters for the query. In your case, use OleDbType.LongVarWChar for the Long Text fields.

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