gpt4 book ai didi

c# - 将此上传图像应用程序转换为 MVC 架构

转载 作者:行者123 更新时间:2023-11-30 17:40:13 25 4
gpt4 key购买 nike

所以我按照教程创建了这个图像 uploader 。问题是我在框架中完成了所有这些。现在我想把它做成MVC。我已经为 Controller 和数据访问层创建了类。有人可以帮助我开始吗?

问候威廉

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);

}
}
}
}

最佳答案

在这里,我给你留下了一个很好的实现的链接,它有一个演示,所以你在测试它时不会遇到任何麻烦。 http://www.codeproject.com/Articles/379980/Fancy-ASP-NET-MVC-Image-Uploader

关于c# - 将此上传图像应用程序转换为 MVC 架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21577475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com