vbs连接sql server及写文件操作

此段代码是连接SQL SERVER的 代码内connMMSQL的参数要根据实际情况传入

Function connMMSQL(ip,user,pwd,database,strsql)
	Dim conn,Re,arr(2)
	set conn = CreateObject("ADODB.Connection")
	conn.ConnectionString="Provider=SQLOLEDB.1;Password="+pwd+";Persist Security Info=True;User ID="+user+";Initial Catalog="+database+";Data source="+ip   
	conn.Open
	If conn.state=0 Then
		arr(0) = -1  
	    connMMSQL = arr
	ElseIf conn.state=1 Then
	   set Re =  conn.execute(strSql)
	   If Re.EOF = False Then 
	   	arr(0) = 1
	   	arr(1) = Re.GetRows
	   	connMMSQL = arr
	   ElseIf Re.EOF = True Then 
	   	arr(0) = 0
	   	connMMSQL = arr
	   End If 	   	
	End If
	conn.close
	Set conn = Nothing
End Function 

 下面是文件操作代码  

 

'判断文件是否存在
Function IsExist(file)
	Dim a,b
	Set a = CreateObject("Scripting.FileSystemObject")
	b = a.FileExists(file)
	If b = True Then 
		IsExist = 1
	ElseIf b = False Then 
		IsExist = -1
	Else 
		IsExist = 0 
	End If 
	Set b = Nothing 
	Set a = Nothing 
End Function

'读取文件内容
Function ReadContent(file)
	If IsExist(file)= 1 Then
		Dim a,b,c
		Set a = CreateObject("Scripting.FileSystemObject")
		Set b = a.OpenTextFile(file,1,False)
		c = ""
		Do While Not b.AtEndOfLine
			c = c+b.ReadLine+vbcrlf 
		Loop
		ReadLine = c
		b.Close
		Set b = Nothing 
		Set a = Nothing 
	ElseIf IsExist(file)=-1 Then 
		ReadLine = -1
	ElseIf IsExist(file)=0 Then 
		ReadLine = 0
	End If 
End Function 
'文件写入内容
Function WriteContent(file,content)
	Dim a,b,c,Msg
	Set a = CreateObject("Scripting.FileSystemObject")
	If IsExist(file)=1 Then
		Msg = MsgBox("是:即删除原文件并新建文件"&vbCrLf&"否:即在该文本未尾追加内容"&vbCrLf&"取消:即自行更改名字再执行",vbYesNoCancel,"系统提示:该文件存在")
		If Msg=6 Then 
			Set b = a.OpenTextFile(file,2,False) 
			b.Write content
			b.Close
			Set b = Nothing  
			WriteContent = 3
		ElseIf Msg=7 Then 
			Set b = a.OpenTextFile(file,8,False)
			b.Write content
			b.Close
			Set b = Nothing 
			WriteContent = 2
		ElseIf Msg=2 Then 
			WriteContent = 0			 
		End If 		
	ElseIf IsExist(file)=-1 Then 
		Set b = a.OpenTextFile(file,2,True)
		b.Write content
		b.Close
		Set b = Nothing 
		Set a = Nothing 
		WriteContent = 1
	ElseIf IsExist(file)=0 Then 
		WriteContent = -1
	End If 
	Set a = Nothing 
End Function
'MsgBox WriteContent("e:/test.txt","这是个测试程序")

 

  

 

posted @ 2015-05-13 17:27  Tester_ABX  阅读(1450)  评论(0编辑  收藏  举报