views:

432

answers:

1

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();
    }
}
+3  A: 

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
AnthonyWJones
Tqvm for the solution, but after close, no need to set both rs and conn = Nothing ? And all sql command must use and set to ADODB.Command?
Ordinarily this sort of code should be compartmentalised into functions. As local variable rs and conn will pass out of scope and be released as the function exits. I personally don't like adding unnecessary set to nothings but some people seem get a warm fuzzy comforting feeling doing so.
AnthonyWJones
understand. tqvm!