没有合适的资源?快使用搜索试试~ 我知道了~
用VS往SQL数据库中上传及读取图片和资料
5星 · 超过95%的资源 需积分: 50 33 下载量 197 浏览量
2013-07-20
11:06:47
上传
评论 2
收藏 6KB TXT 举报
温馨提示
试读
6页
用VS往SQL数据库中上传及读取图片和资料
资源推荐
资源详情
资源评论
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 个人资料界面;
using System.IO;
using System.Drawing.Imaging;
namespace 个人资料界面
{
public partial class Form1 : Form
{
public string name = "你好";
public Form1()
{
InitializeComponent();
}
private void button5_Click_1(object sender, EventArgs e)//上传图片及限制图片大小
{
//上传图片到数据库
OpenFileDialog openDlg = new OpenFileDialog();
openDlg.Filter = "图片文件(*.jpg)|*.jpg*|图片文件(*.gif)|*.gif|图片文件(*.jpeg|*.jpeg"; //文件类型(在框中选择)
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 个人资料界面;
using System.IO;
using System.Drawing.Imaging;
namespace 个人资料界面
{
public partial class Form1 : Form
{
public string name = "你好";
public Form1()
{
InitializeComponent();
}
private void button5_Click_1(object sender, EventArgs e)//上传图片及限制图片大小
{
//上传图片到数据库
OpenFileDialog openDlg = new OpenFileDialog();
openDlg.Filter = "图片文件(*.jpg)|*.jpg*|图片文件(*.gif)|*.gif|图片文件(*.jpeg|*.jpeg"; //文件类型(在框中选择)
string filePath = "";
if (openDlg.ShowDialog() == DialogResult.OK)//如若获取了图片执行以下语句
{
filePath = openDlg.FileName;//将获取的文件地址赋给filePath
//this.txtFilePath.Text = filePath;
///////////////////////限制图片大小
FileInfo fi = new FileInfo(openDlg.FileName);//用fileinfo的方法获取图片
long _size = fi.Length;//得到图片的字节大小
if (_size <= 1024 * 1024 * 2)
{
this.picShow.ImageLocation = filePath;//在picturebox控件中显示图片
FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read); //打开文件流,用来读取图片文件中的数据
byte[] buffer = new byte[stream.Length]; //将文件流中的数据存入内存字节组中,buffer
stream.Read(buffer, 0, (int)stream.Length);
stream.Close();
try
{
//调用存储图片数据的存取过程
string strName = Path.GetFileName(filePath);
SqlConnection conn = DBConnection.MyConnection();
conn.Open();
SqlCommand cmd = new SqlCommand("proc_UploadPicture", conn);
cmd.CommandType = CommandType.StoredProcedure;
//cmd.Parameters.Add("@ID", SqlDbType.Int).Value = 1;
cmd.Parameters.Add("@Picture", SqlDbType.Image).Value = buffer;//将buffer(内存字节组)赋给sql中的@Picture
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = name;
cmd.ExecuteNonQuery();
剩余5页未读,继续阅读
资源评论
- 小东门儿2014-04-12资源不错 可利用性强
___永
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功