search
HomeDatabaseMysql TutorialTcl访问SQLServer等数据库的方法

Tcl访问SQLServer等数据库的方法

Jun 07, 2016 pm 03:12 PM
sqlservertcluseCandatabasemethodaccess

可以使用tcom来 访问 ADO,下面是Script.NET中封装的一个 访问 ADO的类,在Script.NET中可以找到这个类的代码。( http://www.blueantstudio.net ) ################################################################# # TclDB. tcl # Author : blueant # Ve

可以使用tcom来访问ADO,下面是Script.NET中封装的一个访问ADO的类,在Script.NET中可以找到这个类的代码。(http://www.blueantstudio.net)
#################################################################
# TclDB.tcl
# Author     : blueant
# Version    : 1.0
# Date       : 2007-6-27
# Description: Tcl Database
#################################################################
package provide TclDB 1.0
package require tcom
package require Itcl
::itcl::class TAdoDb {

# 数据库字段类型定义
public common DBTYPE_EMPTY 0
public common DBTYPE_NULL 1
public common DBTYPE_I2 2
public common DBTYPE_I4 3
public common DBTYPE_R4 4
public common DBTYPE_R8 5
public common DBTYPE_CY 6
public common DBTYPE_DATE 7
public common DBTYPE_BSTR 8
public common DBTYPE_IDISPATCH 9
public common DBTYPE_ERROR 10
public common DBTYPE_BOOL 11
public common DBTYPE_VARIANT 12
public common DBTYPE_IUNKNOWN 13
public common DBTYPE_DECIMAL 14
public common DBTYPE_UI1 17
public common DBTYPE_I1 16
public common DBTYPE_UI2 18
public common DBTYPE_UI4 19
public common DBTYPE_I8 20
public common DBTYPE_UI8 21
public common DBTYPE_GUID 72
public common DBTYPE_FILETIME 64
public common DBTYPE_BYTES 128
public common DBTYPE_STR 129
public common DBTYPE_WSTR 130
public common DBTYPE_NUMERIC 131
public common DBTYPE_UDT 132
public common DBTYPE_DBDATE 133
public common DBTYPE_DBTIME 134
public common DBTYPE_DBTIMESTAMP 135

# 内部变量定义
protected variable m_cnstr "" ;# 数据库连接字符串
protected variable m_cn "" ;# Connection对象句柄
protected variable m_rs "" ;# Recordset对象句柄

# 数据集的游标类型3=adOpenStatic
protected variable m_CursorType 3
# 数据集的锁定类型1=adLockReadOnly
protected variable m_LockType 1

constructor {} {
# 创建ADO对象
set ret [catch {set m_cn [::tcom::ref createobject "ADODB.Connection"]} msg]
if {$ret} {
error "ADO连接创建失败,原因:$msg"
}
set ret2 [catch {set m_rs [::tcom::ref createobject "ADODB.Recordset"]} msg]
if {$ret} {
error "ADO纪录集创建失败,原因:$msg"
}
}

destructor {
Close
catch {unset m_cn m_rs}
}

public method GetConnectionString {} {return $m_cnstr} ;# 获取连接字符串
public method Open {{cnstr ""}} ;# 打开数据库连接
public method OpenMdb {mdbpath} ;# 打开MDB数据库
public method Close {} ;# 关闭数据库连接
public method ExecSql {sqlstr} ;# 执行SQL语句,有数据则返回数据列表
public method QueryTables {{type TABLE}};# 获取Table列表
public method QueryColumn {tablename {detail ""}};# 查询表的列名
public method CreateTable {tablename fields}; # 创建表
}
#-------------------------------------------------------------
#  Open Database
#  if cnstr is empty, then prompt user to select a database
#-------------------------------------------------------------
::itcl::body TAdoDb::Open {{cnstr ""}} {
# 关闭连接
Close
# 建立连接
if {$cnstr == ""} {
set ret [catch {set dl [::tcom::ref createobject "Datalinks"]} msg]
if {$ret} {
error "ADO Datalinks对象创建失败,原因:$msg"
}

set ret [catch {
set conn [$dl PromptNew]
set cnstr [$conn ConnectionString]
unset conn
unset dl
} msg]
if {$ret} {
#error "获取连接字符串失败,原因:$msg!"
set m_cnstr ""
return
}
}
set ret [catch {$m_cn Open $cnstr} msg]
if {$ret} {
error "$msg/n打开数据库连接失败,请检查连接字符串!/n$cnstr"
}

# 保存连接字符串
set m_cnstr $cnstr

#pwait 10
return
}
#-------------------------------------------------------------
#  Open Access Database
#-------------------------------------------------------------
::itcl::body TAdoDb::OpenMdb {mdbpath} {
Open "provider=Microsoft.Jet.OLEDB.4.0;data source=$mdbpath"
return
}
#-------------------------------------------------------------
#  Close Database
#-------------------------------------------------------------
::itcl::body TAdoDb::Close {} {
# 关闭连接
catch {$m_rs Close}
catch {$m_cn Close}
#pwait 10
return
}
#-------------------------------------------------------------
#  Exec SQL
#  if search a recordset, then return recordset data
#-------------------------------------------------------------
::itcl::body TAdoDb::ExecSql {sqlstr} {
set m_rowcount 0

# 关闭Recordset
catch {$m_rs Close}

# 执行查询
set ret [catch {$m_rs Open $sqlstr $m_cn $m_CursorType $m_LockType} msg]
if {$ret} {
error "$msg/n执行SQL语句失败:/n$sqlstr"
}

# 检查SQL语句是否返回了数据
catch {set m_rowcount [$m_rs RecordCount]}
if {$m_rowcount catch {$m_rs Close}
return
}

set flds [$m_rs Fields]
set m_colcount [$flds Count]
set m_data {}

# 数据
catch {
for {set j 1} {$j set line {}
for {set i 0} {$i lappend line [string trimright [$m_rs Collect $i]]
}

lappend m_data $line
$m_rs MoveNext
}
}

# 关闭Recordset
catch {$m_rs Close}

# 创建并返回数据列表
return $m_data
}
#-------------------------------------------------------------
#  Query all tables
#  default is query all TABLE, return table name
#  if type is null, then return list of table name and type
#-------------------------------------------------------------
::itcl::body TAdoDb::QueryTables {{type TABLE}} {
# SchemaEnum 20=adSchemaTables
if {[catch {set srs [$m_cn OpenSchema 20]} msg]} {
error $msg
}

set data {}
while {[$srs EOF] == 0} {
if {($type != "") && ($type != "-all")} {
if {[$srs Collect TABLE_TYPE] == $type} {
lappend data [$srs Collect TABLE_NAME]
}
} else {
lappend data [list [$srs Collect TABLE_NAME] [$srs Collect TABLE_TYPE]]
}
$srs MoveNext
}

catch {$srs Close}

return $data
}
#-------------------------------------------------------------
#  Query one table's all column information
#  if follow -detail parameter, then return column detail info
#  detail is column's: Name, HasDefault, Default, NullAble,
#                      Data Type, Max Length
#-------------------------------------------------------------
::itcl::body TAdoDb::QueryColumn {tablename {detail ""}} {
# SchemaEnum 4=adSchemaColumns
if {[catch {set srs [$m_cn OpenSchema 4]} msg]} {
error $msg
}

set data {}
while {[$srs EOF] == 0} {
if {[$srs Collect TABLE_NAME] == $tablename} {
if {$detail == "-detail"} {
lappend data [list  [$srs Collect COLUMN_NAME] /
[$srs Collect COLUMN_HASDEFAULT] /
[$srs Collect COLUMN_DEFAULT] /
[$srs Collect IS_NULLABLE] /
[$srs Collect DATA_TYPE] /
[$srs Collect CHARACTER_MAXIMUM_LENGTH] /
]
} else {
lappend data [$srs Collect COLUMN_NAME]
}
}
$srs MoveNext
}

catch {$srs Close}

return $data
}
#-------------------------------------------------------------
#  Create new table
#  field parameter is a list of field, every field is a list
#  of field name, type, size, default value, not null, auto
#  increment, primary key or index or unique
#-------------------------------------------------------------
::itcl::body TAdoDb::CreateTable {tablename fields} {
set lsTable [QueryTables]
if {[lsearch $lsTable $tablename] != -1} {
error "数据库中已经存在名为 $tablename 的对象。"
}

set sql "CREATE TABLE $tablename/("
set field_count 0
foreach field $fields {
set field_name [lindex $field 0]
if {$field_name == ""} {
continue;
}

set field_type [lindex $field 1]
set field_size [lindex $field 2]
set field_default [lindex $field 3]

set field_notnull ""
if {[lsearch [lrange $field 4 end] "notnull"] != -1} {
set field_notnull "notnull"
}

set field_extend ""
if {[lsearch [lrange $field 4 end] "AUTO_INCREMENT"] != -1} {
set field_extend "AUTO_INCREMENT"
}

set field_key ""
if {[lsearch [lrange $field 4 end] "primary"] != -1} {
set field_key primary
} elseif {[lsearch [lrange $field 4 end] "index"] != -1} {
set field_key index
} elseif {[lsearch [lrange $field 4 end] "unique"] != -1} {
set field_key unique
}

if {$field_count > 0} {
set sql "$sql ,"
}
set sql "$sql $field_name $field_type"
if {($field_size != "") && ($field_size != "0")} {
set sql "$sql/($field_size/)"
}
if {$field_notnull != ""} {
set sql "$sql NOT NULL"
}
if {$field_default != ""} {
if {[lsearch $field_type {"TEXT" "LONGTEXT" "VARCHAR"}] != -1} {
set sql "$sql DEFAULT '$field_default'"
} else {
set sql "$sql DEFAULT $field_default"
}
}
if {$field_extend == "AUTO_INCREMENT" } {
set sql "$sql AUTONUMBER"
}
switch $field_key {
primary { set sql "$sql PRIMARY KEY" }
index {}
unique {}
}

incr field_count
}

set sql "$sql /)"

ExecSql $sql
}

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are some tools you can use to monitor MySQL performance?What are some tools you can use to monitor MySQL performance?Apr 23, 2025 am 12:21 AM

How to effectively monitor MySQL performance? Use tools such as mysqladmin, SHOWGLOBALSTATUS, PerconaMonitoring and Management (PMM), and MySQL EnterpriseMonitor. 1. Use mysqladmin to view the number of connections. 2. Use SHOWGLOBALSTATUS to view the query number. 3.PMM provides detailed performance data and graphical interface. 4.MySQLEnterpriseMonitor provides rich monitoring functions and alarm mechanisms.

How does MySQL differ from SQL Server?How does MySQL differ from SQL Server?Apr 23, 2025 am 12:20 AM

The difference between MySQL and SQLServer is: 1) MySQL is open source and suitable for web and embedded systems, 2) SQLServer is a commercial product of Microsoft and is suitable for enterprise-level applications. There are significant differences between the two in storage engine, performance optimization and application scenarios. When choosing, you need to consider project size and future scalability.

In what scenarios might you choose SQL Server over MySQL?In what scenarios might you choose SQL Server over MySQL?Apr 23, 2025 am 12:20 AM

In enterprise-level application scenarios that require high availability, advanced security and good integration, SQLServer should be chosen instead of MySQL. 1) SQLServer provides enterprise-level features such as high availability and advanced security. 2) It is closely integrated with Microsoft ecosystems such as VisualStudio and PowerBI. 3) SQLServer performs excellent in performance optimization and supports memory-optimized tables and column storage indexes.

How does MySQL handle character sets and collations?How does MySQL handle character sets and collations?Apr 23, 2025 am 12:19 AM

MySQLmanagescharactersetsandcollationsbyusingUTF-8asthedefault,allowingconfigurationatdatabase,table,andcolumnlevels,andrequiringcarefulalignmenttoavoidmismatches.1)Setdefaultcharactersetandcollationforadatabase.2)Configurecharactersetandcollationfor

What are triggers in MySQL?What are triggers in MySQL?Apr 23, 2025 am 12:11 AM

A MySQL trigger is an automatically executed stored procedure associated with a table that is used to perform a series of operations when a specific data operation is performed. 1) Trigger definition and function: used for data verification, logging, etc. 2) Working principle: It is divided into BEFORE and AFTER, and supports row-level triggering. 3) Example of use: Can be used to record salary changes or update inventory. 4) Debugging skills: Use SHOWTRIGGERS and SHOWCREATETRIGGER commands. 5) Performance optimization: Avoid complex operations, use indexes, and manage transactions.

How do you create and manage user accounts in MySQL?How do you create and manage user accounts in MySQL?Apr 22, 2025 pm 06:05 PM

The steps to create and manage user accounts in MySQL are as follows: 1. Create a user: Use CREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password'; 2. Assign permissions: Use GRANTSELECT, INSERT, UPDATEONmydatabase.TO'newuser'@'localhost'; 3. Fix permission error: Use REVOKEALLPRIVILEGESONmydatabase.FROM'newuser'@'localhost'; then reassign permissions; 4. Optimization permissions: Use SHOWGRA

How does MySQL differ from Oracle?How does MySQL differ from Oracle?Apr 22, 2025 pm 05:57 PM

MySQL is suitable for rapid development and small and medium-sized applications, while Oracle is suitable for large enterprises and high availability needs. 1) MySQL is open source and easy to use, suitable for web applications and small and medium-sized enterprises. 2) Oracle is powerful and suitable for large enterprises and government agencies. 3) MySQL supports a variety of storage engines, and Oracle provides rich enterprise-level functions.

What are the disadvantages of using MySQL compared to other relational databases?What are the disadvantages of using MySQL compared to other relational databases?Apr 22, 2025 pm 05:49 PM

The disadvantages of MySQL compared to other relational databases include: 1. Performance issues: You may encounter bottlenecks when processing large-scale data, and PostgreSQL performs better in complex queries and big data processing. 2. Scalability: The horizontal scaling ability is not as good as Google Spanner and Amazon Aurora. 3. Functional limitations: Not as good as PostgreSQL and Oracle in advanced functions, some functions require more custom code and maintenance.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software