public int AddVeinMessage(byte[] data)//插入数据库 { using (BCSSqlConnection = new MySqlConnection(strConn)) { try { dbHelper = new DBHelper(BCSSqlConnection); StringBuilder sbSql = new StringBuilder(); sbSql.Append("insert into table1("); sbSql.Append("data)"); sbSql.Append(" values ("); sbSql.Append(" ?data)"); string strInsertSql = sbSql.ToString(); MySqlCommand cmd = dbHelper.GetSqlStringCommond(strInsertSql); dbHelper.AddInParameter(cmd, "?data", MySqlDbType.VarBinary, FINGERDATA); return dbHelper.ExecuteNonQuery(cmd); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show("添加失败了" + ex.Message); return 0; //表示数据库异常 } finally { dbHelper.CloseSqlConnection(); //关闭数据库 } } }
从数据库中取出
public DataTable SelectVeinMessage() { using (BCSSqlConnection = new MySqlConnection(strConn)) { try { StringBuilder sbSql = new StringBuilder(); sbSql.Append("select data from table"); //设置MYSQL查询语句用于查询符合查询条件的记录 string strSql = sbSql.ToString(); dbHelper = new DBHelper(BCSSqlConnection); //调用DBHelper类中的GetSqlStringCommond()方法将返回的MySqlCommand对象赋给cmd MySqlCommand cmd = dbHelper.GetSqlStringCommond(strSql); // dbHelper.AddInParameter(cmd, "?FINGERDATA", MySqlDbType.VarChar, FINGERDATA); //调用DBHelper类的ExecuteDataSet()方法 DataTable dt = dbHelper.ExecuteDataTable(cmd); //返回DataTable对象dt return dt; } catch (Exception e) { throw new Exception(e.Message); } finally { dbHelper.CloseSqlConnection(); } } }
DataTable dt = mm.SelectVeinMessage1(); byte[] TemplateBuff = new byte[512]; for (int i = 0; i < dt.Rows.Count; i++) { TemplateBuff = (byte[])dt.Rows[i]["data"]; }