使用MYSQL和PHP实现,首先建立数据库代码如下:
<?phpinclude_once('conn.php')//数据库连接公共文件
$sql = "CREATE TABLE clothesImg(
`id` int(4) NOT NULL AUTO_INCREMENT PRIMARY KEY, //图片id,自动编号
`title` VARCHAR(32) CHARACTER SET utf-8 COLLATE utf-8_general_ci NULL,//图片标题
`path` VARCHAR(125) CHARACTER SET utf-8 COLLATE utf-8_general_ci NULL,//图片地址
`remarks` VARCHAR(200) CHARACTER SET utf-8 COLLATE utf-8_general_ci NULL,//备注内容
`date` DATETIME NULL //创建日期
)ENGINE = MyISAM"
mysql_query($sql)
//... ...以上省略
?>
数据库建立好后,再写个PHP上传文件的页面就可以将图片上传保存到数据库里。接下来,在图片显示页面使用PHP的GD库+JAVASCRIPT实现你想要的效果。 (MicBo【麦客博】www.micbo.net 联系QQ:119567604)
我给你一个小例子,你自己看看吧,可能会由于我们数据库软件不同,可能需要修改一下。(我用的是 SQL Server)
下面是我自己做的一个类,实例化后即可使用。
using System
using System.Collections.Generic
using System.ComponentModel
using System.Data
using System.Data.OleDb
using System.Drawing
using System.Text
using System.Windows.Forms
using System.Data.SqlClient
using System.IO
namespace SQL_Query.MyClass
{
class ConnectionSQLClass
{
private string _server = string.Empty
private string _database = string.Empty
private string _uid = string.Empty
private string _pwd = string.Empty
private string _sqlConnection = string.Empty
public ConnectionSQLClass(string server, string database, string uid, string pwd)
{
_server = server
_database = database
_uid = uid
_pwd = pwd
_sqlConnection = "server=" + _server + "database=" + _database + "uid=" + _uid + "pwd=" + _pwd
}
//插入图片(table 表名、fieldName 存储图片字段名、imagePath 图片完整路径)
public bool Insert_Image(string table, string fieldName, string imagePath)
{
try
{
FileStream fs = new FileStream(imagePath, FileMode.Open)
byte[] imagebytes = new byte[fs.Length]
BinaryReader br = new BinaryReader(fs)
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length))
//打开数所
SqlConnection con = new SqlConnection(_sqlConnection)
con.Open()
SqlCommand com = new SqlCommand("insert into " + table + "(" + fieldName + ")" + " values(@ImageList)", con)
com.Parameters.Add("ImageList", SqlDbType.Image)
com.Parameters["ImageList"].Value = imagebytes
com.ExecuteNonQuery()
con.Close()
}
catch { return false}
return true
}
//读取 fieldName 该字段中符合条件的所有图片(sql SQL查询语句、fieldName 存储图片字段名)
public List<Image>Get_Image(string sql, string fieldName)
{
List<Image>InformatoinCollection = new List<Image>()
SqlConnection cn = new SqlConnection(_sqlConnection)
try
{
cn.Open()
SqlCommand cm = new SqlCommand(sql, cn)
SqlDataReader dr = cm.ExecuteReader()
//MessageBox.Show(dr.HasRows.ToString())
if (!dr.HasRows)
{
return null
}
while (dr.Read())
{
MemoryStream ms1 = new MemoryStream((byte[])dr[fieldName])
Image image = Image.FromStream(ms1, true)
InformatoinCollection.Add(image)
}
dr.Close()
cn.Close()
}
catch
{
InformatoinCollection = null
}
return InformatoinCollection
}
}
}
希望对你有帮助!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)