数据操作:
public bool ExecuteNonQuery(string SQL ,SqlParameter[] Prams)
{
bool Flag = false ;
try
{
cnn.Open() ;
SqlCommand cmd = new SqlCommand( SQL ,cnn ) ;
cmd.Parameters.Clear(); //清除SqlParameterCollection项

if(Prams!=null)
{
foreach(SqlParameter Sqlpara in Prams )
{
cmd.Parameters.Add(Sqlpara);
}
}
int iRow=cmd.ExecuteNonQuery();

if(iRow>0)
Flag = true ;
}
catch( SqlException ex )
{
throw new Exception("数据库执行出错——"+ex.Message.ToString()) ;
}
finally
{
this.CloseCon() ;
}
return Flag ;
}
方法:
public bool AddUpdateDocMould( byte[] DocMould)
{
if( this.ExistsDocMould() )
{
sql = " Update B_DocMould Set DocMould=@DocMould " ;
}
else
{
sql = " Insert Inot B_DocMould(DocMould) values(@DocMould) " ;
}
SqlParameter[] pa = new SqlParameter[]{new SqlParameter("@DocMould",SqlDbType.Image)} ;
pa[0].Value = DocMould ;

if( so.ExecuteNonQuery(sql,pa) )
{
return true ;
}
else
{
return false ;
}
}
页面调用:
this.openFileDlg.InitialDirectory = "c:" ;
this.openFileDlg.DefaultExt = "doc" ;
this.openFileDlg.Filter = "Word文件 (*.doc)|*.doc|All files (*.*)|*.*" ;
if( this.openFileDlg.ShowDialog() == DialogResult.OK )
{
FileStream fs = new FileStream(openFileDlg.FileName, FileMode.OpenOrCreate, FileAccess.Read);
byte[] MyData= new byte[fs.Length];
fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
if( obj.AddUpdateDocMould( MyData ) )
MessageBox.Show("ok") ;
else
MessageBox.Show("bad");
}
报错:
未处理的“System.Exception”类型的异常出现在 BeautyCulture.exe 中。
其他信息: 数据库执行出错——第 1 行: 'B_DocMould' 附近有语法错误。