哪位前辈有空,麻烦指点一下C#连接SQL的一个问题,拜托了。

louisianagc 2009-04-17 03:05:28
大四了,正在做毕业设计。主要要求就是用C#做个C/S端,读取图像并添加一些信息(所有者,图像名等),让后将图像保存到一个SQL server数据库中,并能够通过C/S端访问和管理数据库中的图片。说的好像不太准确,大概就是这个意思。现在我做了个客户端,能够读取图片并添加信息然后保存到本地的目录中,读取和管理这些图片也能够实现。但是将图片保存到数据库中就彻底不会了,C#也是粗通皮毛,请前辈指点一下吧。
下面是一部分代码,主要是实现了两个功能,第一个btnLoadPicture_Click就是“打开图片”按钮,然后弹出资源管理器的窗口,找到要保存的图片点确定
第二个public void Saveimage是“保存”按钮,点一下图片就被保存到本地的文件夹中。
问题就是如何把这个本地的保存目录换成数据库。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace imageCollection
{
public partial class imageControl : UserControl
{
public imageControl()
{
InitializeComponent();
}

private void btnLoadimage_Click(object sender, EventArgs e)
{
// 获取应用程序当前目录
string curDir = Application.StartupPath;
// 构造数据的存放文件
string curFile = curDir + ".dat";
// 使用文件打开对话框
OpenFileDialog openDialog = new OpenFileDialog();
// 设置对话框过滤器,过滤图片的格式
openDialog.Filter = "图片文件(*.JPEG)|*.JPEG|图片文件(*.bmp)|*.bmp|所有文件(*.*)|*.*";
DialogResult r = openDialog.ShowDialog();
if (r == DialogResult.OK)
{
// 把选中的预览交给图像控件
if (!Directory.Exists(curDir + "\\image"))
Directory.CreateDirectory(curDir + "\\image");

File.Copy(openDialog.FileName, curDir + @"\image\" + Path.GetFileName(openDialog.FileName), true);
this.btnLoadimage.Tag = curDir + @"\image\" + Path.GetFileName(openDialog.FileName);
}
}




public void Saveimage()
{
if (!this.CheckimageInfo())
{
if (DialogResult.OK != MessageBox.Show("信息输入不全,是否继续?", "提示", MessageBoxButtons.YesNo,MessageBoxIcon.Question))
{
return;
}
}

// 获取应用程序当前目录
string curDir = Application.ExecutablePath;
// 构造数据的存放文件
string curFile = curDir + ".dat";
// 构造特定存储格式的数据
string data = this.txtimageName.Text + ","
+ this.comboBoxGrade.SelectedItem.ToString() + ","
+ this.txtAuthor.Text + ","
+ this.txtPages.Text + ","
+ this.txtAdvice.Text + ","
+ this.txtNote.Text + ","
+ this.pictureTitle.Image.Tag.ToString() + ","
+ this.btnLoadimage.Tag.ToString();

// 按照特定格式保存数据到文件
using (StreamWriter sw = File.AppendText(curFile))
{
sw.WriteLine(data);
ClearimageInfo();
MessageBox.Show("保存成功!");
}
}




...全文
268 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
louisianagc 2009-04-20
  • 打赏
  • 举报
回复
还得麻烦大家一下,上面是我没说明白,开始做程序的时候直接就把图片放在本地了,老师要求的不是放在本地而是放在数据库中。这个程序得改一下,但是我不会改,所以就来找前辈们帮忙了。能不能请大家说得具体一点,最好是在什么地方替换什么代码也说一下,对C#实在是没什么研究。麻烦大家了

zhangjingcheng 2009-04-20
  • 打赏
  • 举报
回复
图片存目录的文件夹 路径存数据库????
VS2008VS2208 2009-04-20
  • 打赏
  • 举报
回复
顶一下 呵呵
Code従業員 2009-04-20
  • 打赏
  • 举报
回复
保存到数据库要转化为流的形式,因为现在的图片都是经过高压的,如果转化为二进制流保存到数据库会大的很多,有些甚至大上百倍,并不是推荐的方式,个人建议直接存到文件夹下比较合适。
LoveLife_Go 2009-04-20
  • 打赏
  • 举报
回复
答案已经有了
深海之蓝 2009-04-20
  • 打赏
  • 举报
回复
image类型
FlyBee 2009-04-20
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 ximi82878 的回复:]
楼上几位都已经解答完了,我就不献丑了
[/Quote]
我也不献丑了
wartim 2009-04-20
  • 打赏
  • 举报
回复

// 存
MemoryStream MS = new MemoryStream();
pictureBox1.Image.Save(MS, ImageFormat.Jpeg);
Byte[] PhotoData = new Byte[MS.Length];
MS.Position = 0;
MS.Read(PhotoData, 0, Convert.ToInt32(MS.Length));
Data.Rows[0][DC.ColumnName] = PhotoData;

// 取
MemoryStream MS = new MemoryStream((Byte[])MasterData.Rows[0][DC.ColumnName]);
Image Photo = Image.FromStream(MS, true);
pictureBox1.Image = Photo;
zzxap 2009-04-20
  • 打赏
  • 举报
回复
http://download.csdn.net/source/1080145
http://hi.baidu.com/jackvoilet/blog/item/d51a513fc71eb6ca7c1e7140.html
walkghost 2009-04-20
  • 打赏
  • 举报
回复
sqlconnection sqlcommand sqldatareader ....
很多class。
ximi82878 2009-04-20
  • 打赏
  • 举报
回复
楼上几位都已经解答完了,我就不献丑了
whitebo 2009-04-20
  • 打赏
  • 举报
回复
毕业设计自己做
lgqiu2008 2009-04-20
  • 打赏
  • 举报
回复
用数据流读取保存
claymore1114 2009-04-20
  • 打赏
  • 举报
回复
protected void Button2_Click(object sender, EventArgs e)
{
//把图片保存成数据库二进制形式
Stream ImageStream;
string Path = FileUpload1.PostedFile.FileName;// 文件名称
int Size = FileUpload1.PostedFile.ContentLength; // 文件大小
string Type = FileUpload1.PostedFile.ContentType; // 文件类型
ImageStream = FileUpload1.PostedFile.InputStream;
byte[] Content = new byte[Size];
int Status = ImageStream.Read(Content, 0, Size);

SqlConnection conn = new SqlConnection(Sql);
SqlCommand comm = new SqlCommand("insert into ImageStore(ImageData,ImageContentType,ImageDescription,ImageSize) values(@Image,@fileType,@filePath,@fileSize)", conn);

comm.CommandType = CommandType.Text;
comm.Parameters.Add("@Image", SqlDbType.Image).Value = Content;
comm.Parameters.Add("@filePath", SqlDbType.VarChar, 255).Value = Path;
comm.Parameters.Add("@fileType", SqlDbType.VarChar, 255).Value = Type;
comm.Parameters.Add("@fileSize", SqlDbType.BigInt).Value = Size;
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
//把数据库二进制图片转存到硬盘中

SqlConnection conn = new SqlConnection(Sql);
string sql = "select * from ImageStore";
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
byte[] bytes = (byte[])dr["ImageData"];
FileStream fs = new FileStream(@"E:\" + dr["ImgID"] + ".jpg", FileMode.Create, FileAccess.Write);
fs.Write(bytes, 0, bytes.Length);
fs.Flush();
fs.Close();
}
dr.Close();
conn.Close();

}
wzuomin 2009-04-19
  • 打赏
  • 举报
回复
Garnett_KG 2009-04-19
  • 打赏
  • 举报
回复

using System.Data;
using System.Data.SqlClient;
...

public void SaveimageToDB(string imgFileName)
{
//连接字串,请自己修改
SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=sa;database=tempdb");
conn.Open();
SqlCommand cmd = new SqlCommand("insert into tbTest (img) values (@i)", conn);

FileStream fs = new FileStream(imgFileName, FileMode.Open, FileAccess.Read);
byte[] imgdata=new Byte[fs.Length];
fs.Read(imgdata, 0,imgdata.Length);

cmd.Parameters.Add("@i", SqlDbType.Image, (int)fs.Length);
cmd.Parameters["@i"].Value = imgdata;

cmd.ExecuteNonQuery();
conn.Close();
}



Garnett_KG 2009-04-19
  • 打赏
  • 举报
回复

--用于保存图片的表
CREATE TABLE tbTest(img Image)
GO





using System.Data;
using System.Data.SqlClient;
...

public void SaveimageToDB(string imgFileName)
{
//连接字串,请自己修改
SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=sa;database=tempdb");
conn.Open();
SqlCommand cmd = new SqlCommand("insert into tbTest (img) values (@i)", conn);

FileStream fs = new FileStream(imgFileName, FileMode.Open, FileAccess.Read);
byte[] imgdata=new Byte[fs.Length];
fs.Read(ib, 0,imgdata.Length);

cmd.Parameters.Add("@i", SqlDbType.Image, (int)fs.Length);
cmd.Parameters["@i"].Value = ib;

cmd.ExecuteNonQuery();
conn.Close();
}



PandaIT 2009-04-19
  • 打赏
  • 举报
回复
你直接用数据库保存图片

有必要先作为文件保存吗?

多加一个字段image

jianhongzhao 2009-04-19
  • 打赏
  • 举报
回复
" 问题就是如何把这个本地的保存目录换成数据库" 是什么意思?
加载更多回复(3)

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧