Note:
I am a C # programmer, but once I had a requirement that I could only use C/C ++ to write, and the data to be read was stored in DB (SQL CE v3, I don't know how to use C/C ++ (the key is to use OleDB to access the database, which is really complicated), so I gave birth to the use of C # To write a COM component, the idea of calling with C/C ++. it is silly and naive. but it is also a way of thinking. If MS provides C APIs, the problem will be much simpler. but the fact is, MS's own. net cf uses C APIs, but exposes COM APIs to users ..... OK.
Main content:
- Use C # To create a simple COM component (through COM Interop)
- Use VC ++ to write a client to access the COM component. Use the TLB file on the client.
For ease of use, I imported Northwind to SQLServer and tested my code. (the sake of simplycity doesn't know what it means, is it because of paper diapers ....).
- Modify the name of the machine in the COM component to your SQL Server machine name (more than 2005 require the machine name Instance name)
- Of course, I also created a user in it.ScottThe password isTigerTo connect to the database. You can select this user name or create a new one.
Part I: use C # To create a simple COM Component
COM object is a type of Library. COM component that will generate DLL files. In the VS environment, create COM component. Select ....
File-> New-> Project-> VisualC # Projects-> Class Library.
CreateDatabase_COMObjectClass Library Project.
Remember: to treat the C # object as a COM object requires the following points...
- The class must be public.
- Attribute, method, and event must be public
- Attributes and methods must be defined in interfaces.
- The event must be in the event Interface
Not Defined in the interface, but is a public member in the implementation, is invisible to COM, but to other. NET program is visible. to expose attributes and methods to COM, you must define them in the interface and mark them with DispId attributes to implement (......) in the class (.....). the members defined in the interface only use vtable (virtual function table ). to expose an event, you must define the member in the event interface and mark the DispId attribute. class does not need to implement this interface (???). Class can implement interfaces (a class can implement multiple interfaces, and only the first interface is the default Interface .). the attribute methods exposed to COM are actually implemented in the class. they must be marked as public and comply with the definition in the interface. also, declare the events raised by the class here. they must be marked public and must match the declarations in the events interface. (These two sentences do not know the specific meaning, and there is no clue in the Code .)
To generate a GUID value.
This interface looks like this:
Show sourceview source Print?
[Guid(
"694C1820-04B6-4988-928F-FD858B95C880"
)]
public
interface
DBCOM_Interface
{
[DispId(1)]
void
Init(
string
userid ,
string
password);
[DispId(2)]
bool
ExecuteSelectCommand(
string
selCommand);
[DispId(3)]
bool
NextRow();
[DispId(4)]
void
ExecuteNonSelectCommand(
string
insCommand);
[DispId(5)]