在C#中,可以通过如下的步骤来实现将p_w_picpath文件写入数据表的p_w_picpath字段:
1,创建一个FileStream对象,通过该FileStream对象来读取指定的p_w_picpath文件;
2,根据生成FileStream对象,创建byte[]数据,通过FileStream对象的Read方法将数据写入byte[]数组中;
3,将上述byte[]数组赋予数据表的p_w_picpath字段,写入数据表中;
关于将数据表中的iamge字段显示在PictureBox控件中,请参考如下答复:
http://www.csdn.net/expert/topic/675/675865.xml?temp=.655163
下面提供一段示例程序,供您参考:
- 在C#中,可以通过如下的步骤来实现将p_w_picpath文件写入数据表的p_w_picpath字段:
- 1,创建一个FileStream对象,通过该FileStream对象来读取指定的p_w_picpath文件;
- 2,根据生成FileStream对象,创建byte[]数据,通过FileStream对象的Read方法将数据写入byte[]数组中;
- 3,将上述byte[]数组赋予数据表的p_w_picpath字段,写入数据表中;
- 关于将数据表中的iamge字段显示在PictureBox控件中,请参考如下答复:
- http://www.csdn.net/expert/topic/675/675865.xml?temp=.655163
- 下面提供一段示例程序,供您参考:
- 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对象用来读取p_w_picpath文件,其中FileName 为该p_w_picpath的完整文件名
- 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["p_w_picpath"] = MyData;
- // Add the DataRow to the DataTable
- TheTable.Rows.Add(aRow);
- // 写数据记录到数据库表中
- sqlDataAdapter1.Update(ds, "students");
- MessageBox.Show("A student added.");
- }
关于FileStream类的更详细信息,请参考MSDN:
ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemIOFileStreamMembersTopic.htm