终于自己解决更新SQL server数据库中的图片.
SqlConnection conn = null;
try
{
SqlDataAdapter photoAdapter;
DataSet photoDataSet;
DataTable photoTable;
DataRow photoRow; FileStream fs = null;
conn = new SqlConnection(clsUserInfo.strConnection);
conn.Open(); photoAdapter = new SqlDataAdapter(
"SELECT ID,picture FROM Test WHERE ID = " + txtNumber1.Text, conn); fs = new FileStream(openFileDlg.FileName, FileMode.Open, FileAccess.Read);
Byte[] blob = new Byte[fs.Length];
fs.Read(blob, 0, blob.Length);
fs.Close();
photoDataSet = new DataSet("Test");
photoAdapter.UpdateCommand = new SqlCommand
("UPDATE Test SET picture =@PHOTO " +
"WHERE ID = @MyID", conn);
photoAdapter.UpdateCommand.Parameters.Add("@PHOTO",
SqlDbType.Image, blob.Length, "picture");
photoAdapter.UpdateCommand.Parameters.Add("@MyID",
SqlDbType.Int, 0, "ID"); photoAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; // Configures the schema to match with Data Source
photoAdapter.FillSchema(photoDataSet, SchemaType.Source, "Test"); // Fills the DataSet with 'drivers' table data
photoAdapter.Fill(photoDataSet, "Test"); // Get the current driver ID row for updation
photoTable = photoDataSet.Tables["Test"];
int key;
key = Convert.ToInt32(txtNumber1.Text);
photoRow = photoTable.Rows.Find(key); // Start the edit operation on the current row in
// the 'drivvers' table within the dataset.
photoRow.BeginEdit();
// Assign the value of the Photo if not empty
if (blob.Length != 0)
{
photoRow["picture"] = blob;
}
// End the editing current row operation
photoRow.EndEdit(); // Update the database table 'drivers'
photoAdapter.Update(photoDataSet, "Test");
photoAdapter.Dispose();
photoDataSet.Dispose();
photoTable.Dispose();
}
catch (SqlException ex)
{
Console.Write("SQL Exception: " + ex.Message);
}
catch (Exception ex2)
{
Console.Write("Exception: " + ex2.Message);
}
conn.Close();
conn.Dispose();
SqlConnection conn = null;
try
{
SqlDataAdapter photoAdapter;
DataSet photoDataSet;
DataTable photoTable;
DataRow photoRow; FileStream fs = null;
conn = new SqlConnection(clsUserInfo.strConnection);
conn.Open(); photoAdapter = new SqlDataAdapter(
"SELECT ID,picture FROM Test WHERE ID = " + txtNumber1.Text, conn); fs = new FileStream(openFileDlg.FileName, FileMode.Open, FileAccess.Read);
Byte[] blob = new Byte[fs.Length];
fs.Read(blob, 0, blob.Length);
fs.Close();
photoDataSet = new DataSet("Test");
photoAdapter.UpdateCommand = new SqlCommand
("UPDATE Test SET picture =@PHOTO " +
"WHERE ID = @MyID", conn);
photoAdapter.UpdateCommand.Parameters.Add("@PHOTO",
SqlDbType.Image, blob.Length, "picture");
photoAdapter.UpdateCommand.Parameters.Add("@MyID",
SqlDbType.Int, 0, "ID"); photoAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; // Configures the schema to match with Data Source
photoAdapter.FillSchema(photoDataSet, SchemaType.Source, "Test"); // Fills the DataSet with 'drivers' table data
photoAdapter.Fill(photoDataSet, "Test"); // Get the current driver ID row for updation
photoTable = photoDataSet.Tables["Test"];
int key;
key = Convert.ToInt32(txtNumber1.Text);
photoRow = photoTable.Rows.Find(key); // Start the edit operation on the current row in
// the 'drivvers' table within the dataset.
photoRow.BeginEdit();
// Assign the value of the Photo if not empty
if (blob.Length != 0)
{
photoRow["picture"] = blob;
}
// End the editing current row operation
photoRow.EndEdit(); // Update the database table 'drivers'
photoAdapter.Update(photoDataSet, "Test");
photoAdapter.Dispose();
photoDataSet.Dispose();
photoTable.Dispose();
}
catch (SqlException ex)
{
Console.Write("SQL Exception: " + ex.Message);
}
catch (Exception ex2)
{
Console.Write("Exception: " + ex2.Message);
}
conn.Close();
conn.Dispose();
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货