简体   繁体   English

我无法在数据库中添加行

[英]I can't add a row to my database

I want to add a row from my form to the database, this is the code I used for it : 我想从表单向数据库添加一行,这是我使用的代码:

private Connexion connexion = new Connexion();
private SqlDataAdapter daAuteur;
private byte[] imgData;

     class Connexion
        {
            public DataSet ds = new DataSet();
            public SqlConnection cnx = new SqlConnection("Data Source=MAC-BOOK-AIR;Initial Catalog=Gestion_bib;Integrated Security=True");

    }

    private void AjoutAuteur_Load(object sender, EventArgs e)
            {
                InitializeOpenFileDialog();
                daAuteur = new SqlDataAdapter("select * from Auteur", connexion.cnx);
                daAuteur.Fill(connexion.ds, "Auteur");
            }

     private void AjoutAuteur_Load(object sender, EventArgs e)
            {
                InitializeOpenFileDialog();
                daAuteur = new SqlDataAdapter("select * from Auteur", connexion.cnx);
                daAuteur.Fill(connexion.ds, "Auteur");
            }

      private void ajouterNewBook_Click(object sender, EventArgs e)
            {
                DataRow row = connexion.ds.Tables["Auteur"].NewRow();
                row[1] = String.IsNullOrEmpty(nomAuteurBox.Text) ?
                    DBNull.Value :
                    nomAuteurBox.Text as Object;
                row[2] = String.IsNullOrEmpty(prenomAuteurBox.Text) ?
                    DBNull.Value :
                    prenomAuteurBox.Text as Object;
                row[3] = String.IsNullOrEmpty(nomAuteurBox.Text) ?
                    DBNull.Value :
                    dateDeNaissanceAuteurBox.Text as Object;
                row[4] = String.IsNullOrEmpty(lieuDeNaissanceAuteurBox.Text) ?
                    DBNull.Value :
                    lieuDeNaissanceAuteurBox.Text as Object;
                row[5] = String.IsNullOrEmpty(nationaliteAuteurBox.Text) ?
                    DBNull.Value :
                    nationaliteAuteurBox.Text as Object;
                if (decesCheckBox.Checked)
                {
                    row[6] = true;
                    row[7] = String.IsNullOrEmpty(dateDecesAuteurBox.Text) ?
                    DBNull.Value :
                    dateDecesAuteurBox.Text as Object;
                    row[8] = String.IsNullOrEmpty(lieuDecesAuteurBox.Text) ?
                        DBNull.Value :
                        lieuDecesAuteurBox.Text as Object;
                }
                else
                {
                    row[6] = false;
                    row[7] = DBNull.Value;
                    row[8] = DBNull.Value;
                }
                row[9] = String.IsNullOrEmpty(periodeAuteurBox.Text) ?
                    DBNull.Value :
                    periodeAuteurBox.Text as Object;
                row[10] = String.IsNullOrEmpty(resumeAuteurBox.Text) ?
                   DBNull.Value :
                   resumeAuteurBox.Text as Object;
                row[11] = String.IsNullOrEmpty(lien1Box.Text) ?
                   DBNull.Value :
                   lien1Box.Text as Object;

                imgData = new ImageConverter().ConvertTo(auteurPhoto.Image, typeof(Byte[])) as Byte[];
                row[12] = imgData == null ?
                   DBNull.Value :
                   imgData as Object;
                connexion.ds.Tables["Auteur"].Rows.Add(row);

                SqlCommandBuilder cmb = new SqlCommandBuilder(daAuteur);
                daAuteur.Update(connexion.ds, "Auteur");
            }

but it gives me this error : 但是它给了我这个错误:

String or binary data would be truncated. 字符串或二进制数据将被截断。 The statement has been terminated 该语句已终止

Where is the problem ? 问题出在哪儿 ?

You must also ensure that the size on field in database is conform your string parameter length 您还必须确保数据库中size on fieldsize on field符合您的string parameter length

You can fix size of your row content by using SubString method 您可以使用SubString method固定行内容的大小

You have problem about transfering of image 您有关于图像传输的问题

imgData = new ImageConverter().ConvertTo(auteurPhoto.Image, typeof(Byte[])) as Byte[];
row[12] = imgData == null ? DBNull.Value : imgData; //Adjust here

This error means that one (or more) of the columns in the database are too small to contain the text that you are attempting to insert. 此错误意味着数据库中的一列(或多列)太小而无法包含您要插入的文本。 You need to either ensure that the strings are not too long, or increase the length of the columns in the database. 您需要确保字符串不要太长,或者增加数据库中列的长度。

Without more information, I can't pinpoint the problem any more. 没有更多信息,我无法再查明问题了。

As others have said, you are running into exactly what the error is stating: the data you are trying to enter is larger than that allowed by the column. 正如其他人所说,您正遇到错误所指出的一切:您要输入的数据大于该列所允许的数据。

Take a look at this sqlfiddle 看看这个sqlfiddle

Notice that it works, but if you change the insert to have 6 characters, it will fail to build the schema for the same reason that you have stated. 请注意,它可以工作,但是如果将插入内容更改为6个字符,则出于与上述相同的原因,它将无法构建模式。

That truncation error means that the data that you are trying to insert is too large to fit in the columns. 该截断错误意味着您尝试插入的数据太大而无法放入列中。 Have you set limits on how much data can be added to your table columns? 您是否设置了可以向表列中添加多少数据的限制?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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