Get a list of ODBC data objects in the .NET platform

zhaozj2021-02-16  100

Although the .NET platform is also specifically directed to the ODBC's drive system.data.odbc, it is not provided in System.Data.OleDB.

GetoledBschematable's function, for this, using ODBC32.DLL, I wrote a special class for everyone.

using System; using System.Runtime.InteropServices; namespace SQLDialog.Data {///

/// ODBCDriver obtain a list of ODBC data objects /// Author: Xu Xia Jun /// Created: 2004-08-02 // / Completion time: 2004-08-02 18:40 /// public class odbcdriver {public odbcdriver () {// // Todo: Add constructor logic //}

private const short MAX_DATA_BUFFER = 255; private const short SQL_SUCCESS = 0; private const short SQL_SUCCESS_WITH_INFO = 1; private const short SQL_ERROR = -1; private const short SQL_NO_DATA_FOUND = 100; private const short SQL_CLOSE = 0; private const short SQL_DROP = 1; private const short SQL_CHAR = 1; private const short SQL_NUMERIC = 2; private const short SQL_DECIMAL = 3; private const short SQL_INTEGER = 4; private const short SQL_SMALLINT = 5; private const short SQL_FLOAT = 6; private const short SQL_REAL = 7; private const short SQL_DOUBLE = 8; private const short SQL_VARCHAR = 12; private const short SQL_DATA_SOURCE_NAME = 6; private const short SQL_USER_NAME = 8; private const short SQL_HANDLE_ENV = 1; private const short SQL_HANDLE_DBC = 2;

/ * Not used due to a temporary, annotations [DllImport ( "odbc32.dll", EntryPoint = "SQLAllocConnect")] private static extern short SQLAllocConnect (int env, ref int ldbc); [DllImport ( "odbc32.dll", EntryPoint = " SQLCloseCursor ")] private static extern short SQLCloseCursor (int lStmt); [DllImport (" odbc32.dll ", EntryPoint =" SQLAllocEnv ")] private static extern short SQLAllocEnv (ref int env); [DllImport (" odbc32.dll ", EntryPoint = "SQLDescribeCol")] private static extern short SQLDescribeCol (int lStmt, int colnum, int colname, int buflen, ref int colnamelen, ref int dtype, ref int dl, ref int ds, ref int n); [DllImport ( " odbc32.dll ", EntryPoint =" SQLDrivers ")] private static extern short SQLDrivers (int env, int dir, int descrip, int bflen, ref int descriplen, int attrib, int bfattrlen, ref int attriblen); [DllImport (" odbc32 .dll ", entrypoint =" sqlerror ")] Private Static Extern Short Sqlerror (int Env, int LDBC, INT LSTMT, INT SQLSTATE, REF INT NATIV eError, int Buffer, int Buflen, ref int Outlen); [DllImport ( "odbc32.dll", EntryPoint = "SQLExecDirect")] private static extern short SQLExecDirect (int lStmt, long sqlString, int sqlstrlen); [DllImport ( "odbc32 .dll ", entrypoint =" sqlfreeConnect ")] Private static extern short sqlfreeconnect (int LDBC);

[DllImport ( "odbc32.dll", EntryPoint = "SQLFreeEnv")] private static extern short SQLFreeEnv (int env); [DllImport ( "odbc32.dll", EntryPoint = "SQLGetInfo")] private static extern short SQLGetInfo (int ldbc , int hWnd, string szInfo, int cbInfoMax, ref int cbInfoOut); [DllImport ( "odbc32.dll", EntryPoint = "SQLNumResultCols")] private static extern short SQLNumResultCols (int lStmt, ref int numCols); * /

///

/// Assign a handle of a running environment that must be done /// //// // before connecting to the ODBC connection / /// [DLLIMPORT ("ODBC32.DLL")] Private Static Extern Short Sqlallochandle (Short HTYPE, INTPTR INPUTHANDLE (OUT INTPTR OUTPUTHANDLE); /// /// Releases a handle of a running environment /// /// < / param> /// /// [DLLIMPORT ("ODBC32.DLL")] Private static extern short sqlfreehandle (short htype, pointpete) ; /// /// Set ambient property /// /// /// /// /// /// [DLLIMPORT ("ODBC32.DLL")] Private Static Extern Short SqlseteTenvattr (INTPTR HENV, INT Attribute, INTPTR VALUEPTR, INT STRLENGTH); /// /// 分 //// /// /// [DLLIMPORT ("ODBC32.DLL", entrypoint = "sqlallocstmt") ] Private static extern short sqlallocstmt (system.intptr ldbc, out system.intptr LSTMT); /// /// acquisition with database /// /// <

/ param> /// /// /// // / /// /// /// < / returns> [DllImport ( "odbc32.dll", EntryPoint = "SQLConnect")] private static extern short SQLConnect (System.IntPtr ldbc, string Server, int serverlen, string uid, int uidlen, string pwd, int pwdlen); / //

/// disconnects to the database /// /// /// [DLLIMPORT (" ODBC32.DLL ", entrypoint =" sqldisconnect ")] Private static extern short sqldisconnect (system.intptr ldbc); /// / / / / / / / / Summary> /// /// /// /// /// /// /// /// /// [DLLIMPORT (" ODBC32.DLL ", entrypoint =" sqldriverconnect "

)] Private static extern short SQLDriverConnect (IntPtr hconn, IntPtr WindowHandle, string inString, int inStringLength, System.Text.StringBuilder outString, int outStringLength, out short outLengthNeeded, int f); ///

/// scroll cursor / // /// Statement handle //// [DLLIMPORT ("ODBC32.DLL", entrypoint = "sqlfetch")] private Static Extern int SQLFETCH (System.intptr LSTMT); /// /// Release assigned statement handle /// /// /////////// /// [DLLIMPORT ("ODBC32.DLL", entrypoint = "sqlfreeestmt")] Private static extern short sqlfrestmt (System.intptr Lstmt, int Endoption); /// /// Number /// /// Statement handle /// Column index /// column data type /// Target storage address /// Target value buffer size ///

Actually removed data size /// [DLLIMPORT ("ODBC32.DLL", entrypoint = "sqlgetdata")] Private static extern short sqlgetdata (System. INTPTR LSTMT, Short ColumnNumber, Short Targettype, System.Text.StringBuilder TargetValueptr, Int Dwbufflen, Out Int LPCBout;

///

/// /// /// // statement handle /// // Classification /// // length /// // Solution Name /// // Scheme name length /// / table name /// // Table name length /// Table Type /// length /// [ DllImport ( "odbc32.dll", EntryPoint = "SQLTables")] private static extern short SQLTables (System.IntPtr lStmt, string CatalogName, short CatalogNameLength, string SchemaName, short SchemaNameLength, string TableName, short TableNameLen, string TableType, int TableTypeLen) ;

///

/// Connect to ODBC, taking a list of data objects /// /// public tstringlist getDbobject (string sconn, sqldialog.enumObjectType OT) {System.IntPtr henv = IntPtr.Zero; TStringList list = new TStringList (); System.IntPtr hdbc = IntPtr.Zero; short retcode; retcode = SQLAllocHandle (SQL_HANDLE_ENV, System.IntPtr.Zero, out henv); #region if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {retcode = SQLSetEnvAttr (henv, 200, (IntPtr) 3, 0); if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {retcode = SQLAllocHandle (SQL_HANDLE_DBC, henv, out hdbc); if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {#region open database Connectivity System.Text.StringBuilder outStr = new System.Text.StringBuilder (255); short lenNeeded = 0; retcode = SQLDriverConnect (HDBC, System.intptr.zero, Sconn, Sconn.length, Outstr, Outstr.capacity, Out lenneed, 1); if (Retcode == SQL_SUCCESS || Retcode == SQL_SUCCE SS_WITH_INFO) {System.IntPtr stHandle = IntPtr.Zero; #region assigned a statement handle retcode = SQLAllocStmt (hdbc, out stHandle); if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {// get table string TableType; if (OT == Sqldialog.enumObjectType.none) TableType = ""; Else TableType = Ot.toString (); retcode = sqltables (sthandle, null, 0, null, 0, null, 0, tabletype, tabletype.length); if (Retcode == SQL_SUCCESS || RETCODE ==

SQL_SUCCESS_WITH_INFO) {// get the recording System.Text.StringBuilder TName; int sqlstatus; #region cursor fetch data while (true) {sqlstatus = SQLFetch (stHandle); if (sqlstatus == 65535 || sqlstatus == SQL_ERROR || sqlstatus = = SQL_NO_DATA_FOUND) {Break;} else {/// Take Data INT LEN; TNAME = New System.Text.StringBuilder (1024); Sqlgetdata (Sthandle, 3, SQL_CHAR, TNAME, MAX_DATA_BUFFER, OUT LEN); if (TName. }} #ENDREGON End Cursor data} #ENDREGON SQLDISCONNECT (HDBC);} #ENDREGION SQLDISCONNECT (HDBC);} #ENDREGION SQLFREEHANDLE (SQL_HANDLE_DBC, HDBC);}} SQLFreeHandle (SQL_HANDLE_ENV, HENV);} #ENDREGON RETURN LIST;}}}

转载请注明原文地址:https://www.9cbs.com/read-10994.html

New Post(0)