如何调出设置ADO连接的对话框?(100分)

  • 主题发起人 duckstar
  • 开始时间
D

duckstar

Unregistered / Unconfirmed
GUEST, unregistred user!
Delphi中可以用PromptDataSource调用该对话框,C#中如何用呢?
比如点击某按钮,则弹出该对话框,并且获得设置后的连接字符串。
不会用C#,看了相关的帮助,还是不懂。
帮别人问的,最好能详细点,多谢了。
 
UP一下,我也很关心这个问题
 
use AdoConEd;
....
EditConnectionString(TADOConnection);
....
 
quantum99: 能详细点吗?我刚才查了帮助,找不到EditConnectionString,需要引用什么吗?
 
quantum99:谢谢,不过我问的是C#里怎么用,不是Delphi。
 
AdoConEd 单元在系统目录中你搜一下,考到你的工程目录中即可。
 
等一下,这里有一个MS例子我没没用过,供你参考
/////////////////////////////////////////////////////////////////
// myCreateDataSource
//
// This function creates an OLE DB DataSource object for a
// provider selected by the user, sets initialization properties
// for the DataSource, and initializes the DataSource. The
// function returns a pointer to the DataSource object's
// IUnknown in *ppUnkDataSource.
//
/////////////////////////////////////////////////////////////////
HRESULT myCreateDataSource
(
IUnknown ** ppUnkDataSource
)
{
HRESULT hr;
IDataInitialize * pIDataInitialize = NULL;
IDBPromptInitialize * pIDBPromptInitialize = NULL;
IDBInitialize * pIDBInitialize = NULL;
CLSID clsid = CLSID_MSDASQL;
// Use the Microsoft Data Links UI to create the DataSource
// object;
this will allow the user to select the provider
// to connect to and to set the initialization properties
// for the DataSource object, which will be created by the
// Data Links UI.
if( g_dwFlags &
USE_PROMPTDATASOURCE )
{
// Create the Data Links UI object and obtain the
// IDBPromptInitialize interface from it
XCHECK_HR(hr = CoCreateInstance(
CLSID_DataLinks, //clsid -- Data Links UI
NULL, //pUnkOuter
CLSCTX_INPROC_SERVER, //dwClsContext
IID_IDBPromptInitialize, //riid
(void**)&pIDBPromptInitialize //ppvObj
));
// Invoke the Data Links UI to allow the user to select
// the provider and set initialization properties for
// the DataSource object that this will create
XCHECK_HR(hr = pIDBPromptInitialize->PromptDataSource(
NULL, //pUnkOuter
GetDesktopWindow(), //hWndParent
DBPROMPTOPTIONS_PROPERTYSHEET, //dwPromptOptions
0, //cSourceTypeFilter
NULL, //rgSourceTypeFilter
NULL, //pwszszzProviderFilter
IID_IDBInitialize, //riid
(IUnknown**)&pIDBInitialize //ppDataSource
));
// We've obtained a DataSource object from the Data Links UI. This
// object has had its initialization properties set, so all we
// need todo
is Initialize it
XCHECK_HR(hr = pIDBInitialize->Initialize());
}
// We are not using the Data Links UI to create the DataSource object.
// Instead, we will enumerate the providers installed on this system
// through the OLE DB Enumerator and will allow the user to select
// the ProgID of the provider for which we will create a DataSource
// object.
else
{
// Use the OLE DB Enumerator to obtain a rowset of installed
// providers, then
allow the user to select a provider from this
// rowset
CHECK_HR(hr = myCreateEnumerator(CLSID_OLEDB_ENUMERATOR, &clsid));
// We will create the DataSource object through the OLE DB service
// component IDataInitialize interface, so we need to create an
// instance of the data initialization object
XCHECK_HR(hr = CoCreateInstance(
CLSID_MSDAINITIALIZE, //clsid -- data initialize
NULL, //pUnkOuter
CLSCTX_INPROC_SERVER, //dwClsContext
IID_IDataInitialize, //riid
(void**)&pIDataInitialize //ppvObj
));
// Use IDataInitialize::CreateDBInstance to create an uninitialized
// DataSource object for the chosen provider. By using this service
// component method, the service component manager can provide
// additional functionality beyond what is natively supported by the
// provider if the consumer requests that functionality
XCHECK_HR(hr = pIDataInitialize->CreateDBInstance(
clsid, //clsid -- provider
NULL, //pUnkOuter
CLSCTX_INPROC_SERVER, //dwClsContext
NULL, //pwszReserved
IID_IDBInitialize, //riid
(IUnknown**)&pIDBInitialize //ppDataSource
));
// Initialize the DataSource object by setting any required
// initialization properties and calling IDBInitialize::Initialize
CHECK_HR(hr = myDoInitialization(pIDBInitialize));
}
CLEANUP:
*ppUnkDataSource = pIDBInitialize;
if( pIDataInitialize )
pIDataInitialize->Release();
if( pIDBPromptInitialize )
pIDBPromptInitialize->Release();
return hr;
}
/////////////////////////////////////////////////////////////////
// myDoInitialization
//
// This function sets initialization properties that tell the
// provider to prompt the user for any information required to
// initialize the provider, then
calls the provider's
// initialization function.
//
/////////////////////////////////////////////////////////////////
HRESULT myDoInitialization
(
IUnknown * pIUnknown
)
{
HRESULT hr;
IDBInitialize * pIDBInitialize = NULL;
IDBProperties * pIDBProperties = NULL;
HWND hWnd = GetDesktopWindow();

const ULONG cProperties = 2;
DBPROP rgProperties[cProperties];
DBPROPSET rgPropSets[1];
// In order to initialize the DataSource object most providers require
// some initialization properties to be set by the consumer. For
// instance, these might include the data source to connect to and the
// user ID and password to use to establish identity. We will ask the
// provider to prompt the user for this required information by setting
// the following properties:
myAddProperty(&rgProperties[0],DBPROP_INIT_PROMPT,VT_I2,DBPROMPT_COMPLETE);
myAddProperty(&rgProperties[1],DBPROP_INIT_HWND, VT_I4,(LONG)hWnd);
rgPropSets[0].rgProperties = rgProperties;
rgPropSets[0].cProperties = cProperties;
rgPropSets[0].guidPropertySet = DBPROPSET_DBINIT;
// Obtain the needed interfaces
XCHECK_HR(hr = pIUnknown->QueryInterface(IID_IDBProperties,
(void**)&pIDBProperties));
XCHECK_HR(hr = pIUnknown->QueryInterface(IID_IDBInitialize,
(void**)&pIDBInitialize));
// If a provider requires initialization properties, it must support
// the properties that we are setting (_PROMPT and _HWND). However,
// some providersdo
not need initialization properties and may
// therefore not support the _PROMPT and _HWND properties. Because of
// this, we will not check the return value from SetProperties
hr = pIDBProperties->SetProperties(1, rgPropSets);
// Now that we've set our properties, initialize the provider
XCHECK_HR(hr = pIDBInitialize->Initialize());
CLEANUP:
if( pIDBProperties )
pIDBProperties->Release();
if( pIDBInitialize )
pIDBInitialize->Release();
return hr;
}
 
这个是C#自己的帮助啊,我就是编译不通过才来问的,因为我不会调试,也不会用C#。
 
楼上骗谁呢!拿C++的代码来骗分呀!
 
哈哈哈哈,建一个udl扩展名,运行这个udl文件即可
 
天空还下着沙:能详细点吗?
 
用delhi做一个DLL
用C#调用,这样不行吗?
 
同甘共苦:如果从解决问题角度考虑,不失为一个办法,但是我要学习在C#中调用,而且C#调用MS自己的东西总不会没有解决办法的吧。
 
可行的办法估计是建立一个udl文件,象delphi中一样使用
 
会有那么复杂吗?Delphi中只需调用PromptDataSource就可以了。
 
参考AdoConEd 单元的实现,Delphi里边的实现其实就是通过调用Windows ADO的函数产生的
 
用ms-c#,中文版的,轻松连接数据库。
 
很简单
ADOConnection1.ConnectionString := PromptDataSource(Handle, ADOConnection1.ConnectionString);
拿分来。
 
chendaren:我要的是C#下的,不是Delphi下的,看清问题。
 
添加引用COM对象
Microsoft ActiveX Data Objects 2.7
Microsoft OLEDB Service Component 1.0 Type Library
然后在buttonClick里面调用:‘
MSDASC.DataLinks mydlg = new MSDASC.DataLinks();
ADODB._Connection ADOcon;
ADOcon = (ADODB._Connection) mydlg.PromptNew();
ADOcon.Open("","","",0);
if (ADOcon.State == 1) {
MessageBox.Show("Connection Opened");
ADOcon.Close();
}
else
{
MessageBox.Show("Connection Failed");
}
 

Similar threads

回复
0
查看
658
不得闲
D
回复
0
查看
746
DelphiTeacher的专栏
D
D
回复
0
查看
748
DelphiTeacher的专栏
D
顶部