扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
[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)] string GetColumnData(int pos); } |
// 事件接口Database_COMObjectEvents [Guid(";47C976E0-C208-4740-AC42-41212D3C34F0";), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface DBCOM_Events { } |
[Guid(";9E5E5FB2-219D-4ee7-AB27-E4DBED8E123E";), ClassInterface(ClassInterfaceType.None), ComSourceInterfaces(typeof(DBCOM_Events))] public class DBCOM_Class : DBCOM_Interface { |
ClassInterface(ClassInterfaceType.None), ComSourceInterfaces(typeof(DBCOM_Events))] |
using System; using System.Runtime.InteropServices; using System.IO; using System.Text; using System.Data.SqlClient; using System.Windows.Forms ; namespace Database_COMObject { [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)] string GetColumnData(int pos); } // 事件接口Database_COMObjectEvents [Guid(";47C976E0-C208-4740-AC42-41212D3C34F0";), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface DBCOM_Events { } [Guid(";9E5E5FB2-219D-4ee7-AB27-E4DBED8E123E";), ClassInterface(ClassInterfaceType.None), ComSourceInterfaces(typeof(DBCOM_Events))] public class DBCOM_Class : DBCOM_Interface { private SqlConnection myConnection = null ; SqlDataReader myReader = null ; public DBCOM_Class() { } public void Init(string userid , string password) { try { string myConnectString = ";user id=";+userid+";;password=";+password+ ";;Database=NorthWind;Server=SKYWALKER;Connect Timeout=30";; myConnection = new SqlConnection(myConnectString); myConnection.Open(); MessageBox.Show(";CONNECTED";); } catch(Exception e) { MessageBox.Show(e.Message); } } public bool ExecuteSelectCommand(string selCommand) { if ( myReader != null ) myReader.Close() ; SqlCommand myCommand = new SqlCommand(selCommand); myCommand.Connection = myConnection; myCommand.ExecuteNonQuery(); myReader = myCommand.ExecuteReader(); return true ; } public bool NextRow() { if ( ! myReader.Read() ) { myReader.Close(); return false ; } return true ; } public string GetColumnData(int pos) { Object obj = myReader.GetValue(pos); if ( obj == null ) return ";"; ; return obj.ToString() ; } public void ExecuteNonSelectCommand(string insCommand) { SqlCommand myCommand = new SqlCommand(insCommand , myConnection); int retRows = myCommand.ExecuteNonQuery(); } } } |
sn -k Database_COM_Key.snk 打开AssemblyInfo.cs,并修改下面一行的内容: [assembly: AssemblyKeyFile(";Database_COM_Key.snk";)] |
CoInitialize(NULL); Database_COMObject::DBCOM_InterfacePtr p(__uuidof(Database_COMObject::DBCOM_Class)); db_com_ptr = p ; db_com_ptr->;Init(";scott"; , ";tiger";); |
char cmd[1024]; sprintf(cmd , ";SELECT COMPANYNAME , CONTACTNAME , CONTACTTITLE , ADDRESS FROM CUSTOMERS WHERE CUSTOMERID = '%s'"; , m_id ); const char *p ; bool ret = db_com_ptr->;ExecuteSelectCommand(cmd); if ( ! db_com_ptr->;NextRow() ) return ; _bstr_t mData = db_com_ptr->;GetColumnData(3); p = mData ; m_address = (CString)p ; |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者