试图将信息从datagrid传递到sql表中 [英] trying to pass information from datagrid into sql table

查看:72
本文介绍了试图将信息从datagrid传递到sql表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在尝试将数据网格中的信息解析到sql表中

我就是这样想的:

 私有 无效 storeDataBTN_Click(对象发​​件人,EventArgs e)
{
    //  DataTable BookDetails = new DataTable("BookDetails"); 
    使用(conn)
    {
        SqlCommand命令=  SqlCommand(" ,conn);
         foreach (DataGridViewRow dr  in  DGTables.Rows中)
        {
            
                command.Parameters.AddWithValue(" ,titleDataGridViewTextBoxColumn);
                command.Parameters.AddWithValue(" ,authorFirstNameDataGridViewTextBoxColumn);
                command.Parameters.AddWithValue(" ,authorSurnameDataGridViewTextBoxColumn);
                command.Parameters.AddWithValue(" ,aquisitionDateDataGridViewTextBoxColumn.ToString());
                command.Parameters.AddWithValue(" ,publisherNameDataGridViewTextBoxColumn);
                command.Parameters.AddWithValue(" ,publisherCityDataGridViewTextBoxColumn);
                command.Parameters.AddWithValue(" ,publishingDateDataGridViewTextBoxColumn.ToString());
                command.Parameters.AddWithValue(" ,iSBNDataGridViewTextBoxColumn);
                command.Parameters.AddWithValue(" ,dDClassificationDataGridViewTextBoxColumn);

                尝试
                {
                    conn.Open();
                     int  rows = command.ExecuteNonQuery();


                    如果(行>   0 )
                    {
                        MessageBox.Show(" );
                    }

                }
                捕获(例外)
                {
                    MessageBox.Show(ex.Message);
                }
        }
    } 




我收到错误消息:

字符串或二进制数据将被截断

任何帮助表示赞赏

通常,当您从集合中选择"SelectedIndex"时,会报告错误.例如,

 cmd.Parameters.AddWithValue(" ,ddlcompany.SelectedItem ); 



应该是ddlcompany.SelectedIndex或ddlcompany.SelectedValue,因为SelectedItem引用的是ListItem本身,而不是值.


hey, im trying to parse information from a datagrid into an sql table

this is what im thinking:

private void storeDataBTN_Click(object sender, EventArgs e)
{
    //DataTable BookDetails = new DataTable("BookDetails");
    using (conn)
    {
        SqlCommand command = new SqlCommand("INSERT INTO BookDetails VALUES(@Title, @AuthorFirstName, @AuthorSurname, @AquisitionDate, @PublisherName ,@PublisherCity, @PublishingDate, @ISBN, @DDClassification)", conn);
        foreach (DataGridViewRow dr in DGTables.Rows)
        {
            
                command.Parameters.AddWithValue("@Title", titleDataGridViewTextBoxColumn);
                command.Parameters.AddWithValue("@AuthorFirstName", authorFirstNameDataGridViewTextBoxColumn);
                command.Parameters.AddWithValue("@AuthorSurname", authorSurnameDataGridViewTextBoxColumn);
                command.Parameters.AddWithValue("@AquisitionDate", aquisitionDateDataGridViewTextBoxColumn.ToString());
                command.Parameters.AddWithValue("@PublisherName", publisherNameDataGridViewTextBoxColumn);
                command.Parameters.AddWithValue("@PublisherCity", publisherCityDataGridViewTextBoxColumn);
                command.Parameters.AddWithValue("@PublishingDate", publishingDateDataGridViewTextBoxColumn.ToString());
                command.Parameters.AddWithValue("@ISBN", iSBNDataGridViewTextBoxColumn);
                command.Parameters.AddWithValue("@DDClassification", dDClassificationDataGridViewTextBoxColumn);

                try
                {
                    conn.Open();
                    int rows = command.ExecuteNonQuery();


                    if (rows > 0)
                    {
                        MessageBox.Show("Insert succesful");
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }            
        }
    }




i get the error :

string or binary data would be truncated

any help appreciated

thanks

解决方案

Usually, the reported error raises when you are selecting ''SelectedIndex'' from the collection. As an example,

cmd.Parameters.AddWithValue("@company",ddlcompany.SelectedItem);



Should be ddlcompany.SelectedIndex or ddlcompany.SelectedValue because SelectedItem is referencing the ListItem itself, not a value.


这篇关于试图将信息从datagrid传递到sql表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆