日期:2014-05-18  浏览次数:20408 次

SQL2005数据库中能放图片么
谁知道求实例!! 万分感谢!!

------解决方案--------------------
1.数据库里能存图片,数据类型为varbinary(max),.net的存取方式,百度一下,一大把例子。
2.也可以把图片的文件路径在存为一个字段,读取之然后调用图片。百度一下,一大把例子。
------解决方案--------------------
http://www.cnblogs.com/tuyile006/archive/2007/01/08/614718.html
归根到底任何东西在电脑里面都是101010110,所以喽,只需要把图片转换成二进制就可以啦
------解决方案--------------------
private void btnLoad_Click(object sender, EventArgs e) //读取代码
{
try
{
//创建数据库连接字符串
string strConn = "server=.;database=db_PWMS;uid=sa;pwd=584520";
//创建SqlConnection对象
SqlConnection connection = new SqlConnection(strConn);
//打开数据库连接
connection.Open();
//创建SQL语句
string sql = "select blodid,blobdata from Images order by blodid";
//创建SqlCommand对象
SqlCommand command = new SqlCommand(sql, connection);
//创建DataAdapter对象
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
//创建DataSet对象
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet, "BLOBTest");
int c = dataSet.Tables["BLOBTest"].Rows.Count;
if (c > 0)
{
Byte[] mybyte = new byte[0];
mybyte = (Byte[])(dataSet.Tables["BLOBTest"].Rows[c - 1]["BLOBData"]);
MemoryStream ms = new MemoryStream(mybyte);
pictureBox1.Image = Image.FromStream(ms);
}
connection.Close();

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}


}
private void btnSave_Click(object sender, EventArgs e)//写入代码
{
try
{
string strConn = "server=.;database=db_PWMS;uid=sa;pwd=584520";
SqlConnection connection = new SqlConnection(strConn);
string sql = "insert into Images (BLOBData) values (@BLOBData)";
SqlCommand command = new SqlCommand(sql, connection);


FileStream fs = new FileStream(picturePath, FileMode.Open, FileAccess.Read);
//声明Byte数组
Byte[] mybyte = new byte[fs.Length];
//读取数据
fs.Read(mybyte,0,mybyte.Length);
fs.Close();
//转换成二进制数据,并保存到数据库
SqlParameter prm = new SqlParameter("@BLOBData", System.Data.SqlDbType.VarBinary, mybyte.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, mybyte);
command.Parameters.Add(prm);
//打开数据库连接
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnOpen_Click(object sender, EventArgs e)//打开图片代码
{
OpenFileDialog Opfd = new OpenFileDialog();
Opfd.Filter = "*.jpg|*jpg|*.bmp|*bmp|*.gif|*gif|*.pdf|*pdf|所有文件|*.*";
if (Opfd.ShowDialog() == DialogResult.OK)
{
try