问题可能是您的数据表中的image字段的数据内容存在问题,建议先通过上面的步骤:实现将image文件写入数据表的image字段,然后再进行读取。
上述代码在如下的测试环境中运行通过:
数据库:MS SQL Server 2000
数据表students结构如下:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[students]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[students]
GO
CREATE TABLE [dbo].[students] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[age] [int] NULL ,
[image] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
开发环境:
Windowx XP Professional
Visual Studio.Net 2002 Version 7.0.9466
Microsoft .NET Framework 1.0 Version 1.0.3705
C#里获取SQL数据表的IMAGE类型字段内容(到PictureBox控件):
1,获取image字段的值,并转化为byte[]数组;
2,然后建立一个FileStream对象,并将byte[]数值写到一个临时的图象文件;
3,关闭FileStream对象,并在PictureBox控件中显示获得的临时图象文件。
下面提供一段示例代码,供您参考:
private void btnRetrieve_Click(object sender, System.EventArgs e)
{
// Fill a DataSet
DataSet ds = new DataSet();
string connString = "Server=SHA-RICKIE-01;DataBase=test;uid=user;pwd=user";
string sqlString ="Select * from Students";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(sqlString,conn);
sqlDataAdapter1.Fill(ds, "students");
DataRow myRow;
myRow = ds.Tables["students"].Rows[0];
byte[] myData = new byte[0];
if(myRow["image"].ToString()!="")
myData = (byte[])myRow["image"];
else
{
MessageBox.Show("The image field is null.");
return;
}
int ArraySize = new int();
ArraySize = myData.GetUpperBound(0);
// Create a Filestream for writing the byte array to a image file
FileStream fs = new FileStream("tmpImage", FileMode.OpenOrCreate, FileAccess.Write);
// Write the stream of data that we read in from the database to the filestream
// and close it.
// This will create the file tmpImage, which we can use to display in the picturebox
// Writes a block of bytes to this stream using data from a buffer
fs.Write(myData, 0,ArraySize+1);
fs.Close();
// Assign the temporary image file to the picture box
pictureBox1.Image = new Bitmap("tmpImage");
}
下面提供一段示例程序,供您参考:
private void btnAdd_Click(object sender, System.EventArgs e)
{
// Fill a DataSet
DataSet ds = new DataSet();
string connString = "Server=SHA-RICKIE-01;DataBase=test;uid=user;pwd=user";
string sqlString = "Select * from Students";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(sqlString,conn);
SqlCommandBuilder MyCB = new SqlCommandBuilder(sqlDataAdapter1);
sqlDataAdapter1.Fill(ds, "students");
DataTable TheTable = ds.Tables[0];
// Create a new row in Memory
DataRow aRow = TheTable.NewRow();
// Insert the information from the dialog into the Table
aRow["name"] = txtboxname.Text;
aRow["age"] = txtboxage.Text;
// 创建一个FileStream对象用来读取image文件,其中FileName 为该image的完整文件名
string FileName = txtboxFilename.Text;
FileStream fs = new FileStream (FileName, FileMode.OpenOrCreate,
FileAccess.Read);
// Read the Data into the Byte Array
byte[] MyData = new byte[fs.Length];
fs.Read(MyData, 0, (int)fs.Length);
fs.Close();
// Assign the DataRow Picture Column to the Byte Array to populate it in the DataRow
aRow["image"] = MyData;
// Add the DataRow to the DataTable
TheTable.Rows.Add(aRow);
// 写数据记录到数据库表中
sqlDataAdapter1.Update(ds, "students");
MessageBox.Show("A student added.");
}