c图片上传数据库
一楼开玩笑了!!
可以保存到数据库的。
首先,你的数据库里要有一个存放二进制数据的字段。
然后,用一个文件选择控件,让用户选择图片。
用FileStream.Read把图片文件按照二进制读取到byte[]中,
接下来,链接数据库,用sql语句,进行相应的插入操作,将数据库的二进制数据的字段赋值为byte[]就行了。
以下是保存和显示的代码:
private void SaveImage(string fileName)
{
// Read the file into a byte array
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
byte[] imageData = new Byte[fs.Length];
fs.Read(imageData, 0, (int)fs.Length);
using (SqlConnection conn = new SqlConnection(connectionString))
{
string sql = "insert into image (imagefilename,blobdata) values (@filename,@blobdata)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@filename",SqlDbType.Text);
cmd.Parameters["@filename"].Direction = ParameterDirection.Input;
cmd.Parameters.Add("@blobdata", SqlDbType.Image);
cmd.Parameters["@blobdata"].Direction = ParameterDirection.Input;
// Store the byte array within the image field
cmd.Parameters["@filename"].Value = fileName;
cmd.Parameters["@blobdata"].Value = imageData;
conn.Open();
if (cmd.ExecuteNonQuery() == 1)
{
MessageBox.Show("Done");
}
}
}
}
private void LoadImage(string fileName)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
string sql = "select blobdata from Image where ImageFileName like @filename";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@filename", SqlDbType.Text);
cmd.Parameters["@filename"].Value = fileName;
conn.Open();
object objImage = cmd.ExecuteScalar();
byte[] buffer = (byte[])objImage;
BinaryWriter bw = new BinaryWriter(new FileStream("C:\\abcd.png", FileMode.Create));
bw.Write(buffer);
bw.Close();
MemoryStream ms = new MemoryStream(buffer);
Image bgImage = Image.FromStream(ms);
ms.Close();
this.BackgroundImage = bgImage;
}
}
------------------------------------------------------------------
PS:用空情报我踩踩空间,谢谢。
http://hi..com/kxl361
② VS.C#如何向数据数据库中存入和读取图片的
首先,在数据库中要建立相应的字段能保存Bytes,例如在SQL Server中用Image类型来定义字段。我所用到的数据库大致结构如下:
字段名
类型
备注
FileID
Int
自增字段
FileName
Varchar(256)
FullName
Varchar(1024)
FileData
Image
然后就是写入数据库,代码如下:
FileInfo fi = new FileInfo( txtFileName.Text );// Replace with your file name
if ( fi.Exists)
{
byte[] bData = null;
int nNewFileID = 0;
// Read file data into buffer
using ( FileStream fs = fi.OpenRead() )
{
bData = new byte[fi.Length];
int nReadLength = fs.Read( bData,0, (int)(fi.Length) );
}
// Add file info into DB
string strQuery = "INSERT INTO FileInfo "
+ " ( FileName, FullName, FileData ) "
+ " VALUES "
+ " ( @FileName, @FullName, @FileData ) "
+ " SELECT @@IDENTITY AS 'Identity'";
SqlCommand sqlComm = new SqlCommand( strQuery, sqlConn );
sqlComm.Parameters.Add( "@FileName", fi.Name );
sqlComm.Parameters.Add( "@FullName", fi.FullName );
sqlComm.Parameters.Add( "@FileData", bData );
// Get new file ID
SqlDataReader sqlReader = sqlComm.ExecuteReader();
if( sqlReader.Read() )
{
nNewFileID = int.Parse(sqlReader.GetValue(0).ToString());
}
sqlReader.Close();
sqlComm.Dispose();
if( nNewFileID > 0 )
{
// Add new item in list view
ListViewItem itmNew = lsvFileInfo.Items.Add( fi.Name );
itmNew.Tag = nNewFileID;
}
}
而读出的代码如下:
// Get new file name
string strFullName = dlgFBSave.SelectedPath;
if( strFullName[strFullName.Length - 1] != '\\' )
strFullName += @"\";
strFullName += lsvFileInfo.SelectedItems[0].Text;
string strQuery = "SELECT FileData FROM FileInfo "
+ " WHERE FileID = " + lsvFileInfo.SelectedItems[0].Tag.ToString();
SqlDataAdapter sqlDAdapter = new SqlDataAdapter(strQuery,sqlConn);
DataSet sqlRecordSet = new DataSet();
byte[] bData = null;
//Get file data from DB
try
{
sqlDAdapter.Fill( sqlRecordSet, "FileInfo" );
foreach( DataRow dr in sqlRecordSet.Tables["FileInfo"].Rows)
{
if( dr["FileData"] != DBNull.Value )
bData = ( byte[] )dr["FileData"];
}
}
catch(SqlException sqlErr)
{
MessageBox.Show( sqlErr.Message );
}
catch
{
MessageBox.Show( "Failed to read data from DB!" );
}
sqlRecordSet.Dispose();
sqlDAdapter.Dispose();
if( bData != null )
{
// Save file
FileInfo fi = new FileInfo( strFullName );
if( !fi.Exists )
{
//Create the file.
using (FileStream fs = fi.Create())
{
fs.Write( bData, 0, bData.Length);
}
}
else
{
//Create the file.
using (FileStream fs = fi.OpenWrite())
{
fs.Write( bData, 0, bData.Length);
}
}
}
不过需要提的一点,如果把大量的文件存入数据库的话,会造成数据库的臃肿,而且访问量也会增大。所以现在比较流行的做法,是把文件上传到服务器上,而在数据库上只保存文件的相对路径即可。那么访问的时候,先通过数据库得到文件的相对路径,然后再访问服务器上的文件
③ 如何将上传图片和文字写进数据库
图片放在本地的某个目录中如“c:\image”,然后再把这个目录+图片名存入数据库"c:\image\image1.jpg"。读出时只要使用<img
href="c:\image\image1.jpg">就OK了。除此之外还可以把上传的图片作为字节流读入数据库,读出时使用cgi或Servlet程序把读出的流以图片显示出来。
④ C#winform 中上传图片保存到数据库中
就是2中方法:
1:上传图片的相对路径到数据库中相应字段里,读取显示时,将控件(假设用的是Image控件)的ImageUrl属性指向该相对路径即可。
2:将图片以二进制流的方式整体上传到数据库里,读取显示时,以二进制流的方式整体读出。这种方法稍微麻烦一点,但保存的是图片整体到数据库里。
⑤ vc mfc中怎么通过按钮 “上传图片”把本机上的图片读入SQL数据库
图片就是文件嘛,把文件数据全部读入到内存然后插入到sql数据库中就可以了,但不建议这样做,因为图片数据比较大,存入数据库会很慢,检索也会很慢,建议只存入图片的路径,比如你要存入的图片为c:\\test.bmp,那么你存入数据的信息就为c:\\test.bmp,当有地方要使用该图片时,只需取出c:\\test.bmp这个路径就可以操作该图片了。
⑥ C#控制台程序怎么将本地图片批量插入sql server 2008数据库,请看补充,请大神帮忙给出代码,有追加!
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.IO;
usingSystem.Data.SqlClient;
usingSystem.Data;
namespaceImportImage
{
classProgram
{
staticvoidMain(string[]args)
{
stringconn="server=.;database=testDB;Uid=sa;Pwd=sa";
using(SqlConnectionmyconn=newSqlConnection(conn))
{
myconn.Open();
using(SqlCommandmycomm=newSqlCommand())
{
DirectoryInfodir=newDirectoryInfo(@"C:rimages");
foreach(FileInfoitemindir.GetFiles("*.jpg"))
{
stringstr=string.Format("insertinto[数据库中的表的名字](imagAns)values(@file)",item.Name);//假设你的id是自动增长的
mycomm.CommandText=str;
mycomm.Connection=myconn;
FileStreamfs=newFileStream(item.FullName,FileMode.Open);
BinaryReaderbr=newBinaryReader(fs);
Byte[]byData=br.ReadBytes((int)fs.Length);
fs.Close();
mycomm.Parameters.Add("@file",SqlDbType.Binary,byData.Length);
mycomm.Parameters["@file"].Value=byData;
mycomm.ExecuteNonQuery();
}
}
}
}
}
}
⑦ 有一个图片保存在"c:/123/123.jpg",如何把它的路径保存到数据库并读出来
在wwwroot目录建立一个目录,例如images,将图片保存在这个目录中,在数据库建立一个字段,例如LJ,在这个字段中输入:/images/123.jpg即可。
⑧ vc mfc中怎么通过按钮 “上传图片”把本机上的图片读入SQL数据库
图片就是文件嘛,把文件数据全部读入到内存然后插入到
sql数据库
中就可以了,但不建议这样做,因为图片数据比较大,存入数据库会很慢,检索也会很慢,建议只存入图片的路径,比如你要存入的图片为c:\\test.bmp,那么你存入数据的信息就为c:\\test.bmp,当有地方要使用该图片时,只需取出c:\\test.bmp这个路径就可以操作该图片了。
