datagridview 中的图片存入数据库,还有菜单的用法

这篇博客介绍了一个Windows Forms应用中如何处理DataGridView控件,特别是当它涉及到存储和更新数据库中的图片时。通过点击事件,实现了在datagridview单元格中插入图片,并将图片数据保存到MySQL数据库。此外,还展示了如何从数据库中读取图片并显示在datagridview中。涉及到的技术包括ADO.NET的SqlCommand和SqlParameter用于执行SQL操作,以及使用Image对象和MemoryStream来处理图像数据。
摘要由CSDN通过智能技术生成

调出菜单跟谁鼠标 

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 2 || e.ColumnIndex == 3)
            {
                return;
            }

            this.cms.Visible = true;
            this.cms.Show(this, new Point(Cursor.Position.X - this.Left - 5, Cursor.Position.Y - this.Top - 10));
        }

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Drawing.Imaging;

namespace labnsgl
{
    public partial class ndshjh : Form
    {
        private string ndshjh_nsid;
        public ndshjh(String nsid)
        {
            InitializeComponent();
            ndshjh_nsid = nsid;
        }

        private void DataBind()
        {
            string sql = "select jhid,nsid,ys1,ys2,tb1,tb2,tb3,tb4,tb5,tb6,tb7,tb8,tb9,tb10,tb11,tb12 from lab_nsjh where nsid='" + ndshjh_nsid + "'";
            SqlDataAdapter sda = new SqlDataAdapter(sql, alluse.Class1.conn1);
            DataSet ds = new DataSet();
            sda.Fill(ds, "lab_nsjh");
            this.dataGridView1.DataSource = ds.Tables["lab_nsjh"];
        }

        private void ndshjh_Load(object sender, EventArgs e)
        {
            DataBind();          
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            string jhid = this.dataGridView1.CurrentRow.Cells["jhid"].Value.ToString();
            string ys1 = this.dataGridView1.CurrentRow.Cells["ys1"].Value.ToString();
            string ys2 = this.dataGridView1.CurrentRow.Cells["ys2"].Value.ToString();

            string mysql = "update lab_nsjh set nsid='" + ndshjh_nsid + "',ys1='" + ys1 + "',ys2='" + ys2 + "' where jhid='" + jhid + "'";
            SqlCommand mycmd = new SqlCommand(mysql, alluse.Class1.conn1);
            mycmd.ExecuteNonQuery();

            if (this.dataGridView1.CurrentRow.Cells["tb1"].Tag != null)
            {
                string mysql1 = "update lab_nsjh set tb1=@img1 where jhid='" + jhid + "'";
                SqlCommand mycmd1 = new SqlCommand(mysql1, alluse.Class1.conn1);
                SqlParameter prm1 = new SqlParameter("@img1", SqlDbType.Image);
                MemoryStream ms1 = new MemoryStream();
                System.Drawing.Image img1 = System.Drawing.Image.FromFile(Application.StartupPath + "//picture//" + this.dataGridView1.CurrentRow.Cells["tb1"].Tag.ToString());
                img1.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
                prm1.Value = ms1.GetBuffer();
                mycmd1.Parameters.Add(prm1);
                mycmd1.ExecuteNonQuery();
            }

            if (this.dataGridView1.CurrentRow.Cells["tb2"].Tag != null)
            {
                string mysql2 = "update lab_nsjh set tb2=@img2 where jhid='" + jhid + "'";
                SqlCommand mycmd2 = new SqlCommand(mysql2, alluse.Class1.conn1);
                SqlParameter prm2 = new SqlParameter("@img2", SqlDbType.Image);
                MemoryStream ms2 = new MemoryStream();
                System.Drawing.Image img2 = System.Drawing.Image.FromFile(Application.StartupPath + "//picture//" + this.dataGridView1.CurrentRow.Cells["tb2"].Tag.ToString());
                img2.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg);
                prm2.Value = ms2.GetBuffer();
                mycmd2.Parameters.Add(prm2);
                mycmd2.ExecuteNonQuery();
            }

            if (this.dataGridView1.CurrentRow.Cells["tb3"].Tag != null)
            {
                string mysql3 = "update lab_nsjh set tb3=@img3 where jhid='" + jhid + "'";
                SqlCommand mycmd3 = new SqlCommand(mysql3, alluse.Class1.conn1);
                SqlParameter prm3 = new SqlParameter("@img3", SqlDbType.Image);
                MemoryStream ms3 = new MemoryStream();
                System.Drawing.Image img3 = System.Drawing.Image.FromFile(Application.StartupPath + "//picture//" + this.dataGridView1.CurrentRow.Cells["tb3"].Tag.ToString());
                img3.Save(ms3, System.Drawing.Imaging.ImageFormat.Jpeg);
                prm3.Value = ms3.GetBuffer();
                mycmd3.Parameters.Add(prm3);
                mycmd3.ExecuteNonQuery();
            }

            if (this.dataGridView1.CurrentRow.Cells["tb4"].Tag !

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值