
通常对用户上传的需要保存到数据库中。解决方法一般有两种:一种是将保存的路径存储到数据库;另一种是将以二进制数据流的形式直接写入数据库字段中。以下为具体方法:
一、保存的上传路径到数据库:
string uppath="";//用于保存上传路径
//获取上传的文件名
string fileFullname = thisFileUpload1FileName;
//获取上传的时间,以时间作为的名字可以防止重名
string dataName = DateTimeNowToString("yyyyMMddhhmmss");
//获取的文件名(不含扩展名)
string fileName = fileFullnameSubstring(fileFullnameLastIndexOf("\\") + 1);
//获取扩展名
string type = fileFullnameSubstring(fileFullnameLastIndexOf("") + 1);
//判断是否为要求的格式
if (type == "bmp" || type == "jpg" || type == "jpeg" || type == "gif" || type == "JPG" || type == "JPEG" || type == "BMP" || type == "GIF")
{
//将上传到指定路径的文件夹
thisFileUpload1SaveAs(ServerMapPath("~/upload") + "\\" + dataName + "" + type);
//将路径保存到变量,将该变量的值保存到数据库相应字段即可
uppath = "~/upload/" + dataName + "" + type;
}
二、将以二进制数据流直接保存到数据库:
引用如下命名空间:
using SystemDrawing;
using SystemIO;
using SystemDataSqlClient;
设计数据库时,表中相应的字段类型为iamge
保存:
//路径
string strPath = thisFileUpload1PostedFileFileNameToString ();
//读取
FileStream fs = new SystemIOFileStream(strPath, FileModeOpen, FileAccessRead);
BinaryReader br = new BinaryReader(fs);
byte[] photo = brReadBytes((int)fsLength);
brClose();
fsClose();
//存入
SqlConnection myConn = new SqlConnection("Data Source=;Initial Catalog=stumanage;User ID=sa;Password=123");
string strComm = " INSERT INTO stuInfo(stuid,stuimage) VALUES(107,@photoBinary )";// *** 作数据库语句根据需要修改
SqlCommand myComm = new SqlCommand(strComm, myConn);
myCommParametersAdd("@photoBinary", SqlDbTypeBinary, photoLength);
myCommParameters["@photoBinary"]Value = photo;
myConnOpen();
if (myCommExecuteNonQuery() > 0)
{
thisLabel1Text = "ok";
}
myConnClose();
读取:
连接数据库字符串省略
myconOpen();
SqlCommand command = new
SqlCommand("select stuimage from stuInfo where stuid=107", mycon);//查询语句根据需要修改
byte[] image = (byte[])commandExecuteScalar ();
//指定从数据库读取出来的的保存路径及名字
string strPath = "~/Upload/zhangsanJPG";
string strPhotoPath = ServerMapPath(strPath);
//按上面的路径与名字保存文件
BinaryWriter bw = new BinaryWriter(FileOpen(strPhotoPath,FileModeOpenOrCreate));
bwWrite(image);
bwClose();
//显示
thisImage1ImageUrl = strPath;
采用俩种方式可以根据实际需求灵活选择。
是可以放到数据库里面的,以ACCESS为例子,你要使用OLE数据类型,
然后把转化为长二进制数据存入,读取的时候可以生成系统隐藏的图
片然后在控件中显示
我的回答肯定没错,因为这个东西我做过,如果还不明白可以到我的空间
里留言,然后留下****,我会把代码给你,我使用的是VB,用VB把
放到ACCESS中,然后再读取出来,实际上是个很简单的小程序
最近无事还是贴上来吧
Dim cc() As Byte
Dim i
Dim t
Private Sub Command1_Click()
CommonDialog1Filter = "JPG(BMP)|BMP"
CommonDialog1ShowOpen
If CommonDialog1FileName <> "" Then
Picture1Picture = LoadPicture(CommonDialog1FileName)
strname = CommonDialog1FileName
Open strname For Binary As #1
ReDim cc(LOF(1) - 1)
t = cc(LOF(1) - 1) '在读取二进制时,如果是用文件 *** 作那么涉及到到底从文件中取多少字节的问题,所以字节数组要指定大小
Get #1, , cc
Close
Else
MsgBox "没有选中"
End If
End Sub
Private Sub Command2_Click()
Adodc1Refresh
Adodc1RecordsetAddNew
Adodc1RecordsetFields("id") = i
Adodc1RecordsetFields("qq") = cc
i = i + 1
Adodc1RecordsetUpdate
Adodc1Refresh
End Sub
Private Sub Command3_Click()
Adodc1Refresh
Dim P() As Byte
Adodc1RecordSource = "select qq from 表一 where id='" & Text1Text & "'"
P = Adodc1RecordsetFields("qq") '当把一组二进制数据赋值给一个二进制数组时就不用指定数组的大小了,因为是全部赋值
Open AppPath & "\oobmp" For Binary As #1
Put #1, , P
Close
End Sub
Private Sub Command4_Click()
Open AppPath & "\oobmp" For Binary As #1
Put #1, , cc
Close
End Sub
Private Sub Form_Load()
i = 1
End Sub
我是 Control_World
请设置我为最佳答案吧
建议存路径
下面是上传并保存路径到数据库
显示有很多中方法,如果在gridview里面显示的就如截图这样设置
protected void Button4_Click(object sender, EventArgs e){
string FilePath = "";
if (FileUpload1FileName != "")
{
if (FileUpload1PostedFileContentLength <= 0)
{
// PublicFunPublicFunctionshowMsg(this, "上传文件为空,请重新选择");
labmsgText = "上传文件为空,请重新选择";
return;
}
else
{
}
if (FileUpload1HasFile)
{
if (FileUpload1PostedFileContentLength > 4196304)
{
// PublicFunPublicFunctionshowMsg(this, "上传文件过大");
// return;
}
else
{
// FilePath = ServerMapPath("~/Download/Dload1");//服务器文件路径
}
FilePath = ServerMapPath("~/DownLoad/SignImg");//服务器文件路径
FileLoadFunUpLoad(FilePath, FileUpload1, DropDownList2SelectedValue);
UploadURL = "~/DownLoad/SignImg/" + DropDownList2SelectedValue + "_" + FileUpload1FileName;
UploadURL2 = UploadURL;
sql = " update yp_insproom_base_t set SignURL='" + UploadURL2 + "' " +
" where UserID='" + DropDownList2SelectedValue + "' ";
MySqlHelperExecuteNonQuery(PublicFunPublicFunctionGetDBconstr("ce_manage_db"), sql);
labmsgText = "上传成功";
databind();
}
}
else
{
// PublicFunPublicFunctionshowMsg(this, "上传文件路径错误");
labmsgText = "上传文件路径错误";
return;
}
}
一般都是这样的,就是在你服务器有一个专门放置的文件夹,然后数据库保存的是你服务器的路径。需要用的时候就去数据库里面取路径。得到路径以后你想怎么处理是你的事情了。
至于如何去数据库取路径这个就是简单的db *** 作。
加载驱动类:
ClassforName(DBDriver);
获取连接:
Connection conn = DriverManagergetConnection(url,username,password);
创建 *** 作对象:
PreparedStatement stmt = conprepareStatement(sql);
执行 *** 作:
ResultSet rs = stmtexecuteQuery();
遍历结果:
List list = new ArrayList();
while(rsnext()){
//具体 *** 作,通常用rsgetString(name)取值
Image img = new Image();//类对应你数据库中表格
imgsetSrc(rsgetString("src"));//假设你数据库中image表中地址字段是src
listadd(img);
}
记得关闭资源:
rsclose();
stmtclose();
conclose();
看你的意思是已经取出来了不知道怎么显示:
你取出来之后可以把放在一个list里面然后去页面上遍历这个list
<c:forEach var="chakan1" items="list">
<tr>
<td>
<img src="${chakan1src}"/>
</td>
</tr>
</c:forEach>
大致应该是这样
写入的时候:
byte[]buffer
=
FileReadAllBytes("路径");
cmdParametersAdd("@myphoto",
SqlDbTypeImage);
SqlParameter
sp
=
cmdParameters[0];
//假设只有一个字段写入
spValue
=
buffer;
取出:
commCommandText
=
"select
top
1
picture
from
info"; //因为你用ExecuteScaler,只取出第一行第一列的东西。
byte[]
data
=
(byte[])commExecuteScalar();
MemoryStream
ms
=
new
MemoryStream(data);
Image
image
=
ImageFromStream(ms);
return
image;
以上就是关于怎么用C++语言将图片存入MYSQL数据库全部的内容,包括:怎么用C++语言将图片存入MYSQL数据库、怎么做一个可以录入图片的数据库、亲啊我怎么把图片存入到数据库呢,然后再显示出来等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)