0
因此,我已根據教程創建了此圖像上傳程序。問題是我在框架中做了所有的事情。現在我想讓它成爲MVC。我爲Controller和Dataaccesslayer創建了類。有人可以幫我開始嗎?將此上傳圖像應用程序轉換爲MVC體系結構
問候威廉
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;
namespace ImageSaveToSQLServer
{
public partial class Form1 : Form
{
SqlConnection conn = new SqlConnection("server=localhost; Trusted_Connection=yes; database=ImageDataBase");
SqlCommand command;
string imgLoc = "";
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void buttonBrowse_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif|All Files (*.*)|*.*";
dlg.Title = "Select Employee Picture";
if(dlg.ShowDialog() == DialogResult.OK)
{
imgLoc = dlg.FileName.ToString();
picEmp.ImageLocation = imgLoc;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
try {
byte[] img = null;
FileStream fs = new FileStream(imgLoc, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
img = br.ReadBytes((int)fs.Length);
string sql = "INSERT INTO Employee(EID,FIRST_NAME,LAST_NAME,IMAGE)VALUES("+textBoxEID.Text+",'"+textBoxFNAME.Text+"','"+textBoxLname.Text+"',@img)";
if (conn.State != ConnectionState.Open)
conn.Open();
command = new SqlCommand(sql, conn);
command.Parameters.Add(new SqlParameter("@img",img));
int x = command.ExecuteNonQuery();
conn.Close();
MessageBox.Show(x.ToString() + " record(s) saved.");
textBoxEID.Text = "";
textBoxFNAME.Text = "";
textBoxLname.Text = "";
picEmp.Image = null;
}
catch (Exception ex)
{
conn.Close();
MessageBox.Show(ex.Message);
}
}
private void buttonShow_Click(object sender, EventArgs e)
{
try
{
string sql = "SELECT FIRST_NAME,LAST_NAME,IMAGE FROM Employee WHERE EID="+textBoxEID.Text+"";
if(conn.State!=ConnectionState.Open)
conn.Open();
command = new SqlCommand(sql, conn);
SqlDataReader reader = command.ExecuteReader();
reader.Read();
if (reader.HasRows)
{
textBoxFNAME.Text=reader[0].ToString();
textBoxLname.Text=reader[1].ToString();
byte[] img = (byte[])(reader[2]);
if(img==null)
picEmp.Image= null;
else
{
MemoryStream ms = new MemoryStream(img);
picEmp.Image = Image.FromStream(ms);
}
}
else
{
MessageBox.Show("FINNS INTE");
}
conn.Close();
}
catch(Exception ex)
{
conn.Close();
MessageBox.Show(ex.Message);
}
}
}
}