星期三, 6月 03, 2009

將DataTable裡的Byte column存入access

string[] myPicArrayTitle = new string[] { "Pic1", "Pic2"};
foreach(string title in myPicArrayTitle )
{
string strSQL = string.Format("Update PicInfo Set {0}=@{0} Where PicSN={1}", title, PicSN);
accessAdapter.MdbAccessOLE(strSQL , title,(byte[])dt.Rows[0][title]);
}
public bool MdbAccessOLE(string strSQL,string ColName ,byte[] FileByteArray)
{
this.my_StrConnection = "Provider=Microsoft.JET.OLEDB.4.0;data source=" + this.my_TablePath;
OleDbConnection my_conn = new OleDbConnection(this.my_StrConnection);
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = my_conn;
cmd.CommandText = strSQL;
cmd.Parameters.Add("@"+ColName, System.Data.OleDb.OleDbType.Binary, FileByteArray.Length).Value = FileByteArray;

bool result = true;
try
{
cmd.Connection.Open();
cmd.ExecuteNonQuery();

}
catch (Exception ex)
{
result = false;
SharedAPI.Log_Error("MDB操作失敗, 錯誤訊息:" + ex.Message + " SQL:" + strSQL);
}
finally
{
cmd.Connection.Close();
}
return result;
}

沒有留言: