papadi Development Blog

.NET and Software Development by Dimitris Papadimitriou 
Filed under

Code

 

Write dataset contents to zip file

Assume that you want to write the contents of a dataset to xml, in order to send that data via e-mail or any other means. Have you noticed how verbose xml files are? However, if you try to compress one, you will notice that size of the compressed file is only a small fragment of the original! Just for testing, I compressed an 200MB XML file into a zip file, to see that the resulting file was 9MB only!
So, is it possible to write the contents of a dataset into an xml but include that xml directly into a zip file, without even creating a temporary file? Yes it is! Here is how. Note that the following code uses SharpZipLib, an open source Zip, GZip, Tar and BZip2 library.

        ' create a sample dataset
        Dim data As New DataSet
        ' TODO : add code that fills the dataset here

        ' create a zip output stream
        Dim FileStream As IO.FileStream = System.IO.File.Create("c:\output.zip")
        Using ZipOutputStream As New Zip.ZipOutputStream(FileStream)
            ZipOutputStream.SetLevel(9) ' maximum compression level

            ' create a new empty entry in zip file
            ' myData.xml will be the name of the xml file inside the zip
            Dim objZipEntry As New Zip.ZipEntry("myData.xml")
            ZipOutputStream.PutNextEntry(objZipEntry)

            ' write dataset to zip stream
            data.WriteXml(ZipOutputStream)
        End Using

Click here to download:
Dataset2Zip._zip (86 KB)

Uses : Visual Studio 2005, VB8

Filed under  //   .NET   C#   Code  

Comments [0]

Clean up IIS and SQL Server and make your websites portable

Do the following mean anything to you?

  • Is your IIS full of test web projects that you once created and you keep them... just in case?
  • Do you have old projects that you want to look once in a while but you don't have the time to create virtual directories for them?
  • Do you have dozens of databases on your SQL Server for all that projects?
  • Do you find the procedure of copying files, IIS settings and SQL Server database to create a copy of a web site tedious?
  • Would you like your web sites to be easily portable by simple copying their content files, without IIS and SQL Server configurations?

If these sound familiar... here is your solution!

How to start a web site without IIS

You can use the ASP.NET virtual web server to start a web site without using the IIS. It's the same tool that opens when you create a web site under the file system from the Visual Studio. The usage is simple:

"%windir%\Microsoft.NET\Framework\v2.0.50727\WebDev.WebServer.EXE" /port:1001 /path:"%CD%" /vpath:"/myWebSite"
But to make it simpler just copy the StartVirtual.bat found inside the attached sample project into your web site's root folder. This batch file contains some comments (REM) for each parameter.

How to access a database without permanently attaching it to SQL Server

One great feature of SQL Server 2005 Express is the ability to dynamically attach a database to server by using the connection string. This way your database is attached when your application starts and is not permanetely connected to sql server using a standard file path, something that would make it difficult to move or copy. In the attached sample you can see one database configured to work this way. The database is placed under the App_Data folder of this web site and it's location is set in the connection string (see web.config) :
   

Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\Database.mdf; Integrated Security=True; User Instance=True"; providerName="System.Data.SqlClient"
If you haven't done already, download SQL Server 2005 express from here. Keep in mind that SQL Server 2000 database are compatible, so you don't have to worry if you have old web sites using SQL Server 2000 databases.

Sample

The attached sample assumes that .NET Framework 2 and SQL Server 2005 Express are installed on your machine.
Download the attached file, unzip it and double click StartVirtual.bat. An icon will show up in your windows tray area (right lower corner). Right click on that icon and then select 'Open in Web Browser'.... and here it is! The web site runs without IIS, without Visual Studio and without previously attaching the database to sql server.
Try making copies of this web site and starting different instances. Be sure to first edit the StartVirtual.bat and change the port number for each instance, if you want them to work simultaneously.

Click here to download:
PortableWebTest._zip (172 KB)

Filed under  //   .NET   Code   SQL Server   Web  

Comments [0]