-2

任何可以帮助将一小部分 VB.NET 转换为 VBScript 的人,我在编写测试应用程序时并没有意识到我要在其中使用它的应用程序都是 VBScript =(

代码从数据库中获取一个表,然后写入几行,然后将表写入一个制表符分隔的文件。我知道我可能必须用 Lambda 完全重写该部分?我可能不得不重新开始,但如果有人能做到这一点,我将不胜感激!

Private dataTable As New DataTable()

Protected Sub Page_Load(sender As Object, e As EventArgs)
PullData()
End Sub

Public Sub PullData()
'Get data from DB into a DataTable
Using conn As New SqlConnection("Server=.\sqlexpress;Database=DB;User Id=User;Password=Password;")
    Using cmd As New SqlCommand("SELECT areaID as 'Pond Number', storageDescription + SPACE(1) + areaID as 'Pond Name', " & vbCr & vbLf & "case when fishWeight = 0 then 0 else 1 end as 'Pondis Used', 1 as 'Volume', " & vbCr & vbLf & "FeedDensity AS 'Feed Density',round(cast(FeedDensity * revolution as float(25)),2)/*cast as float for correct rounding*/ AS 'Feed Multiplier'," & vbCr & vbLf & "feedType as 'Feed Type', feedName as 'Feed Name', batchID AS 'FishBatchCode'" & vbCr & vbLf & vbCr & vbLf & "FROM dbo.vwStorageMASTER" & vbCr & vbLf & vbCr & vbLf & "WHERE fkLocationID = 1 AND fkStorageIndicator <> 3  ORDER BY sequenceNumber ASC", conn)
        conn.Open()

        Dim da As New SqlDataAdapter(cmd)
        da.Fill(dataTable)
    End Using
End Using

'Output tab-delimited
Dim delim As String = vbTab
Dim sb = New StringBuilder()

sb.AppendLine("Trafalgar Master File" & vbCr & vbLf & vbCr & vbLf)
sb.AppendLine(String.Join(delim, "Number of Ponds: ", dataTable.Rows.Count.ToString() & vbCr & vbLf & vbCr & vbLf))

sb.AppendLine(String.Join(delim, dataTable.Columns.Cast(Of DataColumn)().[Select](Function(arg) arg.ColumnName)))
For Each dataRow As DataRow In dataTable.Rows
    sb.AppendLine(String.Join(delim, dataRow.ItemArray.[Select](Function(arg) arg.ToString())))
Next

'Prompt user to download tab-delimited file
Dim FileName As String = "test.xls"
Dim response As System.Web.HttpResponse = System.Web.HttpContext.Current.Response
response.ClearContent()
response.Clear()
response.ContentType = "text/plain"
response.AddHeader("Content-Disposition", "attachment; filename=" & FileName & ";")
response.Write(sb.ToString())
response.Flush()
response.[End]()
End Sub
4

2 回答 2

2

虽然 VB.Net 和 VBScript 共享相似的语法,但它们使用的对象是完全不同的动物。Vbscript 不了解 System.Web、System.Data、StringBuilder 等。相反,您可能需要查看 VB6 语法和后期绑定的 CreateObject 语法来创建 AdoDb 对象。

于 2013-02-13T21:45:27.970 回答
2

这就是我为我的应用程序重写它的方式。最后我基本上又开始了,但结果几乎一样;

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%Response.Buffer = True%>
<!--#include file="Conn/conn.asp" -->
<% 
Call DownloadFile()

Private Sub DownloadFile()
Dim Comm
Dim rst
Dim delim
delim = vbTab
response.Clear

set rst = Server.CreateObject("ADODB.Recordset")
rst.ActiveConnection = MM_Conn_STRING
rst.Open "Select COUNT(*) from vwTrafalgarMasterFile"

'Write titles
Response.Write("Trafalgar Master File" & vbcrlf & vbcrlf)
Response.Write("Number of Ponds: " & rst.Fields(0).Value & vbcrlf & vbcrlf)

If rst.State = 1 Then rst.Close 'Used 1 instead of AdStateOpen as this seemed to cause an error
rst.Open "Select * from vwTrafalgarMasterFile"

'Write headers
If Not rst.EOF Then
    For Each fld In rst.Fields
        Response.Write(fld.Name & delim)
   Next
   Response.Write vbcrlf
Else
    Response.Write("There was a problem retrieving data or no data could be retrieved")
    Response.End
    Exit sub
End if

'Write rows
With rst
.MoveFirst
Do Until .EOF
    For Each fld In rst.Fields
        Response.Write(fld.Value & delim)
    Next
    Response.Write vbcrlf
        .Movenext
    Loop
End With

Dim FileName
FileName = "TrafalgarMasterFile.xls"
response.ContentType = "text/plain"
response.AddHeader "Content-Disposition", "attachment; filename=" + FileName + ";"
response.Flush
response.End

'Clean up
adoCMD.Close
Set Comm = Nothing
Set rst = Nothing
Set fld = Nothing
End Sub
%>
于 2013-02-14T14:15:23.430 回答