如何在sql server 2005中插入bmp图像 [英] How to insert bmp image in sql server 2005
问题描述
我想在sql server 2005中保存图片(bmp / jpg)。
我为数据库制作了DSN;
Table2(Name nvarchar(20),UserImage varbinary(max))
图像在mfc上的PictureControl上。
我已成功连接。
I want to save image(bmp/jpg) in sql server 2005.
I have made DSN for database;
Table2(Name nvarchar(20),UserImage varbinary(max))
image is on PictureControl in mfc.
I have made connection successfully.
void CSaveImageDlg::OnBnClickedButton1()
{
try
{
CDatabase m_database;
CRecordset m_recordset;
bool p=m_database.Open(_T("Dermalog2"),0,0,_T("ODBC;"),0);//_T("ODBC;")
m_recordset.m_pDatabase = &m_database; //tell CRecordset where to find the database
m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,_T("select * from Table1"),CRecordset::appendOnly);
//ASSERT(m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,_T("select * from Table1"),CRecordset::appendOnly));
HBITMAP h_bit=m_Image.GetBitmap();
CLongBinary clb;
clb.m_hData = h_bit;
clb.m_dwDataLength = ::GlobalSize(h_bit);
CBitmap bmp;
bmp.LoadBitmap (IDC_Image);
//Get the number of the selected records
long reCount=m_recordset.GetRecordCount();
if(m_recordset.CanAppend())
{
m_recordset.AddNew();
//m_recordset.SetFieldValue(_T("Name","UserImage"),"imran",clb);//Write our COleVariant to the table
m_database.ExecuteSQL(_T("insert into Table1 values('khan',clb)"));
//m_recordset.pdb.ExecuteSQL(_T("insert into Table1 values('khan',clb)"));
m_recordset.Update();
}
m_recordset.Close();
m_database.Close();
}
catch(CDBException* ex)
{
}
}
m_recordset.CanAppend()函数dioes不起作用。
我接下来该怎么办?
m_recordset.CanAppend() function dioes not work.
What should I do next?
推荐答案
你可以使用自定义记录集来实现这个目标。
1.创建你自己继承自CRecordset的类。
2.公开一个类成员,如下所示:
You could achieve this using custom recordset.
1. Create your own class inherited from CRecordset.
2. Put to public a class members like the following:
public:
LONG a;
CLongBinary b;
CString c;
BOOL d;
//etc...
3.通过定义 m_nFields 变量在构造函数中分配多个列。
4.通过定义DoFieldExchange,实现数据库字段与类成员之间的数据交换。 br />
3. Assign a number of columns in constructor by defining m_nFields variable.
4. Implement data exchange between fields of data base with class members by defining DoFieldExchange.
VOID MyRecordset::DoFieldExchange( CFieldExchange *ex ){
ex->SetFieldType(CFieldExchange::outputColumn);
RFX_Long( ex, "db_field_a", this->a );
RFX_LongBinagy( ex, "db_field_b", this->b );
// ...
}
5.打开你的记录集(就像你实际使用CRecordset对象一样)对目标表的任何查询
6.用AddNew()方法创建一个新记录
7.使用BYTE数组分配长二进制数据成员,并为其他成员分配相应的值
8.更新记录集。
这些文章可能有所帮助:
http://msdn.microsoft.com/en -us / library / s55kt8s3(v = vs.71).aspx [ ^ ]
http://msdn.microsoft.com/en-us/library/yd6x6wbd(v = vs.71).aspx [ ^ ]
<啊REF = http://msdn.microsoft.com/en-us/library/czxt1e3z.aspx> http://msdn.microsoft.com/en-us/library/czxt1e3z.aspx [<一个href =http://msdn.microsoft.com/en-us/library/czxt1e3z.aspxtarget =_ blanktitle =New Window> ^ ]
5. Open your recordset (as you actually did with CRecordset object) with any query to target table
6. Create a new record with AddNew() method
7. Assign your long binary data member with BYTE array and other members with respective values
8. Update your recordset.
These articles might help:
http://msdn.microsoft.com/en-us/library/s55kt8s3(v=vs.71).aspx[^]
http://msdn.microsoft.com/en-us/library/yd6x6wbd(v=vs.71).aspx[^]
http://msdn.microsoft.com/en-us/library/czxt1e3z.aspx[^]
这篇关于如何在sql server 2005中插入bmp图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!