Reputation: 469
I have an example in C# code, but it is using streamWriter
. It must be involving with FileSystemObject
rite. If yes, what are methods should I use? I want to code using VBScript WSH, and my database is MS SQL Server 2005.
Any solution, references, or guide are helpful.
using (StreamWriter tw = File.AppendText("c:\\INMS.txt"))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
tw.WriteLine("id, ip address, message, datetime");
while (reader.Read())
{
tw.Write(reader["id"].ToString());
tw.Write(", " + reader["ip"].ToString());
tw.Write(", " + reader["msg"].ToString());
tw.WriteLine(", " + reader["date"].ToString());
}
tw.WriteLine("Report Generate at : " + DateTime.Now);
tw.WriteLine("---------------------------------");
tw.Close();
reader.Close();
}
}
Upvotes: 1
Views: 5349
Reputation: 189457
In VBScript you need ADODB objects and the FileSystemObject
from the Scripting
library. Something akin to:-
Dim conn: Set conn = CreateObject("ADODB.Connection")
conn.Open "an ole DB mysql connection string", usernameIfneeded, passwordIfNeeded
Dim cmd : Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandText = "your SQL code here"
cmd.CommantType = 1 ''# adCmdText Command text is a SQL query
Dim rs : Set rs = cmd.Execute
Dim fs : Set fs = CreateObject("Scripting.FileSystemObject")
Dim textStream : Set textStream = fs.OpenTextFile("c:\inms.txt", 8, True)
textStream.WriteLine "id, ip address, message, datetime"
Do Until rs.EOF
textStream.Write rs("id") & ","
textStream.Write rs("ip") & ","
textStream.Write rs("msg") & ","
textStream.WriteLine rs("date")
rs.MoveNext
Loop
textStream.Close
rs.Close
conn.Close
Upvotes: 3