C#一次将多个图像从Datagridview插入到数据库中 [英] C# Inserting multiple images from Datagridview into Database at once

查看:99
本文介绍了C#一次将多个图像从Datagridview插入到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


如何将Datagridview中的图像一次保存到数据库中。我试图使用迭代,但仍然有这样的错误

How do I save the images in my Datagridview into Database at once. I tried to used iteration but still I got an error like this


" ArgumentException未处理。从对象类型System.Windows.Forms.DataGridViewCellCollection到已知的托管提供者本机类型不存在映射。"

"ArgumentException was unhandled. No mapping exists from object type System.Windows.Forms.DataGridViewCellCollection to a known managed provider native type."


这是我的代码到目前为止。 。 。

This is my code so far . . .


SqlConnection cn = new SqlConnection("Data Source = localhost; Integrated Security = True; Database = ogs_database;");
SqlDataAdapter adp = new SqlDataAdapter();

private void bnSave_Click(object sender, EventArgs e)
        {
            try
            {                
                for (int i = 0; i < dgScannedImage.Rows.Count; i++)
                {
                    string qry = "INSERT INTO tblCommunication (CommImage, CommType, LetterDate, LetterReceived, LetterType, LetterNumber, LetterAmount, LetterFrom, LetterTo, ReceivedBy, Requisition, LetterSubject, LetterContent, LetterRemarks) VALUES (@CommImage, @CommType, @LetterDate, @LetterReceived, @LetterType, @LetterNumber, @LetterAmount, @LetterFrom, @LetterTo, @ReceivedBy, @Requisition, @LetterSubject, @LetterContent, @LetterRemarks)";
                    adp.InsertCommand = new SqlCommand(qry, cn);
                    adp.InsertCommand.Parameters.AddWithValue("@CommImage", dgScannedImage.Rows[i].Cells);
                    adp.InsertCommand.Parameters.AddWithValue("@CommType", cmbCommType.Text);
                    adp.InsertCommand.Parameters.AddWithValue("@LetterDate", dtpDateLetter.Value.ToString());
                    adp.InsertCommand.Parameters.AddWithValue("@LetterReceived", dtpDateReceived.Value.ToString());
                    adp.InsertCommand.Parameters.AddWithValue("@LetterType", txLetterType.Text);
                    adp.InsertCommand.Parameters.AddWithValue("@LetterNumber", txLetterNumber.Text);
                    adp.InsertCommand.Parameters.AddWithValue("@LetterAmount", txLetterAmount.Text);
                    adp.InsertCommand.Parameters.AddWithValue("@LetterFrom", txLetterFrom.Text);
                    adp.InsertCommand.Parameters.AddWithValue("@LetterTo", txLetterTo.Text);
                    adp.InsertCommand.Parameters.AddWithValue("@ReceivedBy", txReceivedBy.Text);
                    adp.InsertCommand.Parameters.AddWithValue("@Requisition", txRequisition.Text);
                    adp.InsertCommand.Parameters.AddWithValue("@LetterSubject", txLetterSubject.Text);
                    adp.InsertCommand.Parameters.AddWithValue("@LetterContent", txLetterContent.Text);
                    adp.InsertCommand.Parameters.AddWithValue("@LetterRemarks", txLetterRemarks.Text);
                }

                cn.Open();

                if (string.IsNullOrEmpty(cmbCommType.Text))
                {
                    MessageBox.Show("Type of communication is required.");
                }
                else if (string.IsNullOrEmpty(txLetterContent.Text))
                {
                    MessageBox.Show("Content is required.");
                }
                else
                {
                    int x;
                    x = adp.InsertCommand.ExecuteNonQuery();
                    if (x > 1)
                    {
                        MessageBox.Show("New Communication has been successfully saved.");

                        cmbCommType.Text = "";
                        dtpDateLetter.Value = DateTime.Now;
                        dtpDateReceived.Value = DateTime.Now;
                        txLetterType.Text = "";
                        txLetterNumber.Text = "";
                        txLetterAmount.Text = "";
                        txLetterFrom.Text = "";
                        txLetterTo.Text = "";
                        txReceivedBy.Text = "";
                        txRequisition.Text = "";
                        txLetterSubject.Text = "";
                        txLetterContent.Text = "";
                        txLetterRemarks.Text = "";
                    }
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                cn.Close();
            }
        }








推荐答案

" ArgumentException未处理。从对象类型System.Windows.Forms.DataGridViewCellCollection到已知托管提供程序本机类型不存在映射。"

.NET中的本机/基元类型列表

The list of native/primitive types in .NET

https:/ /docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/built-in-types-table

你不能使用datagrid对象作为某种原始类型用作SQL参数。

You you can't use the datagrid object as some kind of primitive type to be used as a SQL parameter.

adp InsertCommand <跨度>参数 <跨度> <跨度> AddWithValue <跨度>( <跨度>" @ CommImage" <跨度>, dgScannedImage [ i ]。 单元 );

adp.InsertCommand.Parameters.AddWithValue("@CommImage", dgScannedImage.Rows[i].Cells);

https://www.codeproject.com/文章/ 10861 /存储和检索图像 - 来自SQL-Server-usin

      ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;


这篇关于C#一次将多个图像从Datagridview插入到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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