当前位置:网站首页>3、 Upload fabric photos to SQL server and provide name to display fabric photos

3、 Upload fabric photos to SQL server and provide name to display fabric photos

2022-06-13 01:41:00 Beyond proverb

One 、 The establishment of database

Still fiber_yy Create... Under database images surface
 Insert picture description here
images The table is designed as follows
 Insert picture description here

Two 、 Page perfect design

main_page The function of the page is improved
 Insert picture description here
Warehousing management system
warehousing page
 Insert picture description here
Inventory query system
query page
 Insert picture description here

The previous blog posts on the login registration page have been implemented , I will repeat it here , Still use the previous login page and database

3、 ... and 、 Code implementation

main_page page

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace fiber_yy
{
    
    public partial class main_page : Form
    {
    
        public main_page()
        {
    
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
    
            MessageBox.Show(" Quit successfully ");
            this.Close();
            new Form1().Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
    
            this.Close();
            new warehousing().Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
    
            this.Close();
            new shipment().Show();
        }

        private void button4_Click(object sender, EventArgs e)
        {
    
            this.Close();
            new query().Show();
        }
    }
}

warehousing page

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
namespace fiber_yy
{
    
    public partial class warehousing : Form
    {
    
        public string constr = "server=CY-20190824RMES;Initial Catalog=fiber_yy;User ID=sa;pwd=beyond";
        
        public warehousing()
        {
    
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
    

            OpenFileDialog ofdlgTest = new OpenFileDialog();
            //ofdlgTest.Filter = "*.jpg|*.png"; // File filtering   Filter files that can be opened 
            ofdlgTest.Filter = "";
            ofdlgTest.Multiselect = false;    // Set that multiple files cannot be selected 

            // The file open dialog box is displayed 
            DialogResult result = ofdlgTest.ShowDialog();

            // When selecting the open button , Display the file name in the text box 
            if (result == DialogResult.OK)                   // Determine whether to open the file 
            {
    
                this.textBox9.Text = ofdlgTest.FileName;

                pictureBox1.Image = Image.FromFile(ofdlgTest.FileName);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
    
            
            string path = textBox9.Text;

            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); // Add the picture of the specified path to FileStream Class 
            BinaryReader br = new BinaryReader(fs);// adopt FileStream Object instantiation BinaryReader object 

            byte[] imgBytesIn = br.ReadBytes(Convert.ToInt32(fs.Length));// Convert pictures to binary data 
            //Save(imgBytesIn);// call ( A way to write by yourself )
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();

            string name = textBox1.Text;
            SqlCommand cmd = new SqlCommand("insert into images (name,image) values(@name,@Image);", conn); //SQL sentence 
                

            cmd.Parameters.Add("@name", SqlDbType.VarChar);
            cmd.Parameters["@name"].Value = name;

            cmd.Parameters.Add("@Image", SqlDbType.Image);
            cmd.Parameters["@Image"].Value = imgBytesIn;

            cmd.ExecuteNonQuery();

            conn.Close();
            MessageBox.Show(" Image upload succeeded ");

        }

        private void button3_Click(object sender, EventArgs e)
        {
    
            new main_page().Show();
            this.Hide();
        }
    }
}

query page

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;

namespace fiber_yy
{
    
    public partial class query : Form
    {
    
        public string constr = "server=CY-20190824RMES;Initial Catalog=fiber_yy;User ID=sa;pwd=beyond";
        public query()
        {
    
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
    
            try
            {
    
                byte[] MyData = new byte[0];
                SqlConnection conn = new SqlConnection(constr);
                string name = textBox1.Text;
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "select * from images where name='" + name + "'"; // Write the picture you want to check name Information 
                SqlDataReader sdr = cmd.ExecuteReader();
                sdr.Read();
                object o = sdr["Image"];
                MyData = (byte[])sdr["Image"];// Read the bit stream of the first picture 
                MemoryStream memoryStream = null;
                memoryStream = new MemoryStream(MyData);
                pictureBox1.Image = Image.FromStream(memoryStream);// Assign pictures to pictureBox1 Control 
                MessageBox.Show(" Read successful ");
            }
            catch 
            {
    
                MessageBox.Show(" Read failed , Please check if the fabric is present ");
            }

            
        }
    }
}

Four 、 Effect demonstration

The program runs
 Insert picture description here
I won't demonstrate if I register , Please refer to the previous blog posts
from yy_user Find an account and password in the table , Direct user login
 Insert picture description here
 Insert picture description here
Login successful
 Insert picture description here
Entry system
 Insert picture description here
At present, we have preliminarily realized Warehousing management function and Inventory query function

First demonstrate Warehousing management system

Original database information
 Insert picture description here
At present, the warehousing management function mainly stores the fabric name and fabric photo entered by the user

Upload photos
 Insert picture description here
 Insert picture description here
Put in storage
 Insert picture description here
View database information

Right click images surface Choose the former 1000 That's ok , Update database information
 Insert picture description here
The last one is the newly stored picture

Next, click return , Go back to the main page , Demonstrate the inventory query function
 Insert picture description here
 Insert picture description here
 Insert picture description here
Click on button1
 Insert picture description here

Optimization continues …

原网站

版权声明
本文为[Beyond proverb]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130135332400.html