views:

301

answers:

2

I have a text file which needs to be constantly updated (regular intervals).

All I want is the syntax and possibly some code that outputs data from a SQL Server database using ASP.Net. The code I have so far is :

<%@ Import Namespace="System.IO" %>
<script language="vb" runat="server">
  sub Page_Load(sender as Object, e as EventArgs)
  Dim FILENAME as String = Server.MapPath("Output.txt")

    Dim objStreamWriter as StreamWriter
   ' If Len(Dir$(FILENAME)) > 0 Then Kill(FILENAME)
   objStreamWriter = File.AppendText(FILENAME)

    objStreamWriter.WriteLine("A user viewed this demo at: " & DateTime.Now.ToString())

    objStreamWriter.Close()

    Dim objStreamReader as StreamReader
    objStreamReader = File.OpenText(FILENAME)

    Dim contents as String = objStreamReader.ReadToEnd()

    lblNicerOutput.Text = contents.Replace(vbCrLf, "<br>")

    objStreamReader.Close()
  end sub
</script>

<asp:label runat="server" id="lblNicerOutput" Font-Name="Verdana" />

With PHP, it is a breeze, but with .Net I have no clue. If you could help me with the database connectivity and how to write the data in pipe delimited format to an Output.txt file, that had be awesome. Thanks guys!

+1  A: 

I would start by at least putting this code into a code-behind file so that your page at least follows the principle of least surprise.

Easiest way to write stuff to a file is to use the File.WriteAllText method (assuming your application has appropriate permissions for the file system).

You can access the database using the SqlConnection class and execute whatever commands you need with a SqlCommand. Once you've gotten data back from the database, just turn it into an array (assuming there's not a massive amount of data) and call String.Join method like so String.Join("|", yourData).

R0MANARMY
This ASP.Net page doesn't display anything to the user. This is just a workaround for the limitations with 1and1 (Windows, Linux hosts database incompatibility). So, I don't really care about code behind model. Thanks for the prompt reply.
ThinkCode
Just because you don't care doesn't mean the next guy who has to maintain this page won't care.
R0MANARMY
It is a temporary fix and no one else will maintain this. Thanks..
ThinkCode
Fair enough, what does the query you want to run look like? (I'll write up a code sample for you to start from).
R0MANARMY
The query would be : select id, title, createdate, expirydate from contact where active = 1;I would like to write these to an Output.txt file (this file is re-created every one hour). The Output.txt file would then be processed using PHP (MySQL load INFILE ...).Thank you so much for your time.
ThinkCode
A: 
  Dim dr As SqlDataReader

  Dim FILENAME as String = Server.MapPath("Output.txt")

  Dim objStreamWriter as StreamWriter
  objStreamWriter = File.CreateText(FILENAME)

  sqlConn.Open()
   'opening the connection
  myCommand = New SqlCommand("SELECT  id, title, expirydate, creationdate from tbl where tbl.isactive=1 and getdate()<=ExpiryDate order by  creationdate asc", sqlConn)
  'executing the command and assigning it to connection 
  dr = myCommand.ExecuteReader()
  While dr.Read()

      objStreamWriter.WriteLine("{0}|{1}|{2:yyyy-MM-dd}|{3:yyyy-MM-dd}", dr(0),  dr(1), dr(2), dr(3))

  End While
  dr.Close()
  sqlConn.Close()

  objStreamWriter.Close()

  Dim objStreamReader as StreamReader
  objStreamReader = File.OpenText(FILENAME)
ThinkCode