2个回答
展开全部
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text;
using System.Windows.Forms; using System.IO;
using System.Data.SqlClient; using System.Drawing.Imaging;
namespace Card {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
private void btnImage_Click(object sender, EventArgs e) {
OpenFileDialog of = new OpenFileDialog();
of.Filter = "*.bmp;*.jpg;*.gif|*.bmp;*.jpg;*.gif;*.jpeg";
of.ShowDialog();
txt_image.Text = of.FileName; string filePath = of.FileName;
if (!File.Exists(of.FileName)) {
MessageBox.Show("不能为空"); return; }
}
private void button1_Click(object sender, EventArgs e) {
string xlh = textBox1.Text.Trim(); string dwmc = textBox2.Text.Trim(); string jbry = textBox3.Text.Trim(); string sfz = textBox4.Text.Trim();
string strPath = txt_image.Text.Trim();
//截取图片的名字
string loadName = strPath.Substring(strPath.LastIndexOf("\\") + 1);
FileStream fs = new FileStream(strPath, FileMode.Open, FileAccess.Read);
byte[] byteFile = new byte[fs.Length]; fs.Read(byteFile, 0, (int)fs.Length); fs.Close(); try {
this.picImage.SizeMode = PictureBoxSizeMode.Normal;
this.picImage.Image = System.Drawing.Image.FromFile(strPath); }
catch (Exception ex) {
MessageBox.Show(ex.Message); }
SqlConnection conn = new SqlConnection("Data
Source=USER-7836C6117D\\SQLEXPRESS;Initial Catalog=Card;uid=sa;pwd=123"); try {
SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; conn.Open();
string strSql = "insert into card(xlh,dwmc,jbry,sfz,qzyj,img) values(@xlh,@dwmc,@jbry,@sfz,@qzyj,@img) ";
cmd.CommandText = strSql;
SqlParameter[] parameters = new SqlParameter[6];
parameters[0] = new SqlParameter("@xlh", SqlDbType.Int, 4);
parameters[0].Value = xlh;
parameters[1] = new SqlParameter("@dwmc", SqlDbType.VarChar, 100);
parameters[1].Value = dwmc;
parameters[2] = new SqlParameter("@jbry", SqlDbType.VarChar, 100);
parameters[2].Value = dwmc;
parameters[3] = new SqlParameter("@sfz", SqlDbType.VarChar, 50);
parameters[3].Value = sfz;
parameters[4] = new SqlParameter("@qzyj", SqlDbType.VarChar, 50);
parameters[4].Value = loadName;
parameters[5] = new SqlParameter("@img", SqlDbType.Image, 16);
parameters[5].Value = byteFile;
cmd.Parameters.AddRange(parameters);
int i = cmd.ExecuteNonQuery(); if (i > 0) {
MessageBox.Show("上传成功"); } }
catch(Exception) {
conn.Close();
MessageBox.Show("上传失败"); }
}
/* private void btnDownLoad_Click(object sender, EventArgs e) {
byte[] bytFile;
SqlConnection conn = new SqlConnection("Data
Source=USER-7836C6117D\\SQLEXPRESS;Initial Catalog=Card;uid=sa;pwd=123");
//try //{
SqlCommand cmd = new SqlCommand();
string strSql = "select img from card "
cmd.Connection = conn;
cmd.CommandText = strSql;
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read()) {
bytFile = (Byte[])sdr["img"];
// bytFile = System.Text.Encoding.ASCII.GetString (sdr["img"]); // bytFile = System.Text.Encoding.Unicode.GetBytes("img"); }
else {
bytFile = new byte[0];
}
sdr.Close();
conn.Close();//通过内存流MemoryStream,
//把byte[]数组fileContent加载到Image中并赋值给图片框的Image属性,
//让数据库中的图片直接显示在窗体上。
MemoryStream ms = new MemoryStream(bytFile, 0, bytFile.Length); Image img = Image.FromStream(ms,true); this.picImage.Image = img;
//关闭内存流
ms.Close();
// }
// // catch // {
// conn.Close();
// MessageBox.Show("失败");
} */ } }
推荐于2016-07-11
展开全部
可以使用控件SaveFileDialog实现;
参考例子如下:
#region 保存对话框
private void ShowSaveFileDialog()
{
//string localFilePath, fileNameExt, newFileName, FilePath;
SaveFileDialog sfd = new SaveFileDialog();
//设置文件类型
sfd.Filter = "数据库备份文件(*.bak)|*.bak|数据文件(*.mdf)|*.mdf|日志文件(*.ldf)|*.ldf";
//设置默认文件类型显示顺序
sfd.FilterIndex = 1;
//保存对话框是否记忆上次打开的目录
sfd.RestoreDirectory = true;
//点了保存按钮进入
if (sfd.ShowDialog() == DialogResult.OK)
{
string localFilePath = sfd.FileName.ToString(); //获得文件路径
string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("//") + 1); //获取文件名,不带路径
//获取文件路径,不带文件名
//FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("//"));
//给文件名前加上时间
//newFileName = DateTime.Now.ToString("yyyyMMdd") + fileNameExt;
//在文件名里加字符
//saveFileDialog1.FileName.Insert(1,"dameng");
//System.IO.FileStream fs = (System.IO.FileStream)sfd.OpenFile();//输出文件
////fs输出带文字或图片的文件,就看需求了
}
}
#endregion
参考例子如下:
#region 保存对话框
private void ShowSaveFileDialog()
{
//string localFilePath, fileNameExt, newFileName, FilePath;
SaveFileDialog sfd = new SaveFileDialog();
//设置文件类型
sfd.Filter = "数据库备份文件(*.bak)|*.bak|数据文件(*.mdf)|*.mdf|日志文件(*.ldf)|*.ldf";
//设置默认文件类型显示顺序
sfd.FilterIndex = 1;
//保存对话框是否记忆上次打开的目录
sfd.RestoreDirectory = true;
//点了保存按钮进入
if (sfd.ShowDialog() == DialogResult.OK)
{
string localFilePath = sfd.FileName.ToString(); //获得文件路径
string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("//") + 1); //获取文件名,不带路径
//获取文件路径,不带文件名
//FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("//"));
//给文件名前加上时间
//newFileName = DateTime.Now.ToString("yyyyMMdd") + fileNameExt;
//在文件名里加字符
//saveFileDialog1.FileName.Insert(1,"dameng");
//System.IO.FileStream fs = (System.IO.FileStream)sfd.OpenFile();//输出文件
////fs输出带文字或图片的文件,就看需求了
}
}
#endregion
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询