簡體   English   中英

錯誤-該字段太小,無法接受您嘗試添加的數據量。 嘗試插入或粘貼較少的數據

[英]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;
       }

我正在使用c#winform將一個csv文件上傳到我的ms Access數據庫中,但是卻出現錯誤“該字段太小,無法接受您嘗試添加的數據量。嘗試插入或粘貼較少的數據。” 現在該怎么辦?

我建議在SQL中指定表字段,例如

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

使用參數化值,而不要直接在字符串中寫入值。 這也允許您指定數據類型,然后ADO.Net OLE適配器將希望適當地處理它,並毫無問題地插入長文本。 請轉至讀取/寫入BLOB ...以獲取插入BLOBS的示例。 概念和代碼示例與插入Long Text值非常相關。 它演示了如何為查詢設置參數。 對於您的情況,對於長文本字段,請使用OleDbType.LongVarWChar

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM