c# - 如何在c#中将datagridview批量数据保存到sql数据库,同时将datagridview与图像绑定(bind)的数据保存到sql数据库

标签 c# mysql datagridview

有很多与我的问题类似的问题,但没有一个能帮助解决这个问题..
我正在使用 win-forms,用 C# 编码,使用 SQL 服务器

我有一个 datagridview,它从数据库中检索数据,其中一些数据为文本,一些数据为图像。
我的代码如下所示,用于将 datagridview 数据保存到数据库
但这不起作用并抛出错误对象或列名称丢失或为空。对于 select into 语句验证.........
请解决我的问题..

try
        {
            ConnectionStringSettings consettings = ConfigurationManager.ConnectionStrings["attendancemanagement"];
            string connectionString = consettings.ConnectionString;
            string StrQuery;
            try
            {
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    using (SqlCommand comm = new SqlCommand())
                    {
                        comm.Connection = conn;
                        conn.Open();
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {
                            StrQuery = @"INSERT INTO dailydemo VALUES ("
                + dataGridView1.Rows[i].Cells["S_No"].Value + ", "
                + dataGridView1.Rows[i].Cells["Employee_Id"].Value + ", "
                + dataGridView1.Rows[i].Cells["Employee_Name"].Value + ", "
                + dataGridView1.Rows[i].Cells["In_time"].Value + ", "
                + dataGridView1.Rows[i].Cells["Out_Time"].Value + ", "
                + dataGridView1.Rows[i].Cells["Date"].Value + ", "
                + dataGridView1.Rows[i].Cells["Week_No_of_The_Month"].Value + ", "
                + dataGridView1.Rows[i].Cells["Attendance"].Value + ", "
                + dataGridView1.Rows[i].Cells["Work_status"].Value + ", "
                + dataGridView1.Rows[i].Cells["Remarks"].Value + ", "
                + dataGridView1.Rows[i].Cells["Image_of_Employee"].Value + ");";
                            comm.CommandText = StrQuery;
                            comm.ExecuteNonQuery();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

最佳答案

使用这个可能会帮助完整...

byte[] imgByteArrLogo = new byte[0];
            using (SqlConnection con = new SqlConnection("Your Connection string Here"))
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                using (SqlTransaction _pTrans = con.BeginTransaction(IsolationLevel.ReadCommitted))
                {
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        if (dataGridView1.Rows[i].Cells["Image_of_Employee"].Value != null)
                        {
                            Image _dgvImage = (Image)dataGridView1.Rows[i].Cells["Image_of_Employee"].Value;
                            MemoryStream ms = new MemoryStream();
                            _dgvImage.Save(ms, ImageFormat.Jpeg);
                            imgByteArrLogo = new byte[ms.Length];
                            ms.Position = 0;
                            ms.Read(imgByteArrLogo, 0, imgByteArrLogo.Length);
                        }
                        else
                        {
                            imgByteArrLogo = new byte[0];
                        }

                        try
                        {
                            string sql = "INSERT INTO dailydemo (S_No,Employee_Id ,Employee_Name ,In_time ,Out_Time ,Date,Week_No_of_The_Month,Attendance,Work_status,Remarks,Image_of_Employee) VALUES (@S_No,@Employee_Id ,@Employee_Name ,@In_time ,@Out_Time ,@Date,@Week_No_of_The_Month,@Attendance,@Work_status,@Remarks,@Image_of_Employee)";
                            using (SqlCommand cmd = new SqlCommand(sql, con, _pTrans))
                            {
                                cmd.Parameters.Add(new SqlParameter("@S_No", dataGridView1.Rows[i].Cells["S_No"].Value));
                                cmd.Parameters.Add(new SqlParameter("@Employee_Id", dataGridView1.Rows[i].Cells["Employee_Id"].Value));
                                cmd.Parameters.Add(new SqlParameter("@Employee_Name", dataGridView1.Rows[i].Cells["Employee_Name"].Value));
                                cmd.Parameters.Add(new SqlParameter("@In_time", dataGridView1.Rows[i].Cells["In_time"].Value));
                                cmd.Parameters.Add(new SqlParameter("@Out_Time", dataGridView1.Rows[i].Cells["Out_Time"].Value));
                                cmd.Parameters.Add(new SqlParameter("@Week_No_of_The_Month", dataGridView1.Rows[i].Cells["Week_No_of_The_Month"].Value));
                                cmd.Parameters.Add(new SqlParameter("@Attendance", dataGridView1.Rows[i].Cells["Attendance"].Value));
                                cmd.Parameters.Add(new SqlParameter("@Work_status", dataGridView1.Rows[i].Cells["Work_status"].Value));
                                cmd.Parameters.Add(new SqlParameter("@Remarks", dataGridView1.Rows[i].Cells["Remarks"].Value));
                                cmd.Parameters.Add(new SqlParameter("@Image_of_Employee", imgByteArrLogo));
                                cmd.ExecuteScalar();
                            }
                        }
                        catch (Exception exp)
                        {
                            _pTrans.Rollback();
                            con.Close();
                            _pTrans.Dispose();
                            MessageBox.Show(exp.Message, "Error!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    _pTrans.Commit();
                    _pTrans.Dispose();
                    con.Close();
                }
            }

更新:

请注意,在本示例中,我使用您的 datagridview 单元格名称作为 sql 表列名称,如果列名称不同,请修改它。

关于c# - 如何在c#中将datagridview批量数据保存到sql数据库,同时将datagridview与图像绑定(bind)的数据保存到sql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26375447/

相关文章:

c# - 将网页重定向到 asp.net 中的同一选项卡

php - symfony2序列化数据替换错误

c# - KeyDown 事件 - 如何轻松知道按下的键是否为数字?

php - 完全重置 Laravel 5 迁移内容?

C# - 使用行中的按钮从 DataGridView 中删除项目

c# - 如何动态设置哪些属性绑定(bind)到 DataGridView?

c# - 常规 Exp 而不是字符串拆分

c# - 是否可以将推送通知从 ASP.NET MVC4 表单发送到 Windows Azure 移动服务

c# - 将现有表映射到 C# 中的域类

php - 我的 table 出了什么问题?不会显示数据库