//数据库sql示例
//数据库sql示例


//连接Sql Server数据库示例 
import com

conn = com.CreateObject("ADODB.Connection")
conn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;Data Source=www.*******.com,1234;Initial Catalog=db1234;Persist Security Info=True;User ID=sa;Password=******************";
conn.Open() 

rs = com.CreateObject("ADODB.RecordSet") 
rs.Open("select * from topdomains where email='web@ecranesoft.com'", conn )

io.open()

while(! rs.eof){ 
    io.print( string.trimright( rs("domain").value ),string.trimright( rs("email").value ) )
    rs.movenext()
}


rs.close();


//连接Access数据库示例

import com

conn = com.CreateObject("ADODB.Connection")
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + io.fullpath("/test.mdb")
conn.open();

adCmdText = 0x0001;
adChar = 129
adVarChar = 200
adParamInput = 0x0001
adParamOutput = 0x0002

cmd = com.CreateObject("ADODB.Command") 
cmd.ActiveConnection = conn;
cmd.CommandType =adCmdText;

try{

cmd.CommandText = "create table homepage2 (url char(30), comment char(20))" 
cmd.Execute( )

}
catch(e){

	tab  = com.CreateObject("ADOX.Table") 
	Cat  = com.CreateObject("ADOX.Catalog")  
	Cat.ActiveConnection=conn 
	tab.ParentCatalog=Cat 
	tab=Cat.Tables("homepage2") 
	
	io.open()
	//如果表已存在，打印出所有的列
	for index,field in com.each(tab.Columns) {
		io.print(field.name)
	} 

}

cmd.CommandText = "insert into homepage2(url,comment) values(?,?)" 
param1 = cmd.CreateParameter("@url",adChar, adParamInput ,30, "crane he" )
param2 = cmd.CreateParameter("@comment",adChar, adParamInput ,20, "hi!" )
cmd.Parameters.Append(param1); //debug模式下，可能会提示错误
cmd.Parameters.Append(param2);

cmd.Execute( )
conn.close();


/**数据库连接需要用到MDAC组件，数据库连接失败时，可以使用下面的方法来修复MDAC安装：

1、用隶属“administrators”管理员组的帐户执行以下操作。
2、找到系统目录下的Mdac.inf文件(C:\windows\inf\mdac.inf)，点鼠标右键选择“安装”。
3、按提示插入XP安装盘修复安装。 **/
