Console applications and databases? 
Author Message
 Console applications and databases?

I am writing a console application that reads log files and provides output
to html pages.  Currently I use a few fairly large (4096X4096) arrays to
handle the mass acumulation of data.  This has been too cpu intensive and
doesn't scale well for larger logfiles.  I would like to change the
application so that it would dump the array data into a SQL database on the
fly.  I would be doing all INSERT and UPDATE commands so I don't need any
visible control.

It doesn't appear that this is available for console applications.  Is there
a way around this or will i need to convert this into a Windows Form
Application?

thanks,

-BB



Tue, 01 Feb 2005 09:47:29 GMT  
 Console applications and databases?
Look at this: (It isn't mine)

using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

[Serializable]
public class Car
{
public string name;
public int age;
public Car(string name,int age)
{
this.name=name;
this.age=age;

Quote:
}

public void Up()
{
age++;

Quote:
}
}

public class Test
{
public static void Main()
{
Console.Write("name:");
string sname=Console.ReadLine();
Console.Write("age:");
string sage=Console.ReadLine();
Car car=new Car(sname,Int32.Parse(sage));
MemoryStream ms=new MemoryStream();
BinaryFormatter b=new BinaryFormatter();
b.Serialize(ms,car);

SqlConnection con=new
SqlConnection("server=localhost;uid=sa;pwd=;database=test");
con.Open();

SqlDataAdapter ad=new SqlDataAdapter("select * from tst",con);
DataSet ds=new DataSet();
ad.Fill(ds,"tblFile");
DataTable dbTable = ds.Tables["tblFile"];

DataRow dbRow = dbTable.NewRow();

// Store data in the row
dbRow["id"] = "kimlee";
dbRow["obj"] = ms.GetBuffer();
dbRow["len"]=ms.Length;
dbTable.Rows.Add(dbRow);

// Update data source
SqlCommandBuilder cb=new SqlCommandBuilder(ad);
ad.Update(ds, "tblFile");

SqlCommand comd=new SqlCommand("select * from tst",con);
SqlDataReader sr=comd.ExecuteReader();
while(sr.Read())
{
byte[] cc=new byte[(int)sr["len"]];
sr.GetBytes(1,0,cc,0,(int)sr["len"]);

MemoryStream mss=new MemoryStream(cc);
Car dcar=(Car)b.Deserialize(mss);
dcar.Up();
Console.Write("name:");
Console.WriteLine(dcar.name) ;
Console.Write("age:");
Console.WriteLine(dcar.age) ;

Quote:
}
con.Close();
}
}

Best Regards,
--
Antonio Cangiano
Webmaster di www.visualCSharp.it
Fondatore di free.it.comp.lang.csharp


Tue, 01 Feb 2005 14:37:36 GMT  
 Console applications and databases?
Sorry, I should have mentioned that I was using VB.NET.  Is there a
comparable method?

thanks,

BB


Quote:
> Look at this: (It isn't mine)

> using System;
> using System.IO;
> using System.Data;
> using System.Data.SqlClient;
> using System.Runtime.Serialization;
> using System.Runtime.Serialization.Formatters.Binary;

> [Serializable]
> public class Car
> {
> public string name;
> public int age;
> public Car(string name,int age)
> {
> this.name=name;
> this.age=age;
> }
> public void Up()
> {
> age++;
> }

> }

> public class Test
> {
> public static void Main()
> {
> Console.Write("name:");
> string sname=Console.ReadLine();
> Console.Write("age:");
> string sage=Console.ReadLine();
> Car car=new Car(sname,Int32.Parse(sage));
> MemoryStream ms=new MemoryStream();
> BinaryFormatter b=new BinaryFormatter();
> b.Serialize(ms,car);

> SqlConnection con=new
> SqlConnection("server=localhost;uid=sa;pwd=;database=test");
> con.Open();

> SqlDataAdapter ad=new SqlDataAdapter("select * from tst",con);
> DataSet ds=new DataSet();
> ad.Fill(ds,"tblFile");
> DataTable dbTable = ds.Tables["tblFile"];

> DataRow dbRow = dbTable.NewRow();

> // Store data in the row
> dbRow["id"] = "kimlee";
> dbRow["obj"] = ms.GetBuffer();
> dbRow["len"]=ms.Length;
> dbTable.Rows.Add(dbRow);

> // Update data source
> SqlCommandBuilder cb=new SqlCommandBuilder(ad);
> ad.Update(ds, "tblFile");

> SqlCommand comd=new SqlCommand("select * from tst",con);
> SqlDataReader sr=comd.ExecuteReader();
> while(sr.Read())
> {
> byte[] cc=new byte[(int)sr["len"]];
> sr.GetBytes(1,0,cc,0,(int)sr["len"]);

> MemoryStream mss=new MemoryStream(cc);
> Car dcar=(Car)b.Deserialize(mss);
> dcar.Up();
> Console.Write("name:");
> Console.WriteLine(dcar.name) ;
> Console.Write("age:");
> Console.WriteLine(dcar.age) ;
> }
> con.Close();
> }

> }

> Best Regards,
> --
> Antonio Cangiano
> Webmaster di www.visualcsharp.it
> Fondatore di free.it.comp.lang.csharp



Tue, 01 Feb 2005 22:13:53 GMT  
 Console applications and databases?
Here is the same sample code in VB.NET:

Imports System
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary

<Serializable()>  _

Public Class Car
   Public name As String
   Public age As Integer

   Public Sub New(name As String, age As Integer)
      Me.name = name
      Me.age = age
   End Sub 'New

   Public Sub Up()
      age += 1
   End Sub 'Up
End Class 'Car
 _

Public Class Test

   Public Shared Sub Main()
      Console.Write("name:")
      Dim sname As String = Console.ReadLine()
      Console.Write("age:")
      Dim sage As String = Console.ReadLine()
      Dim car As New Car(sname, Int32.Parse(sage))
      Dim ms As New MemoryStream()
      Dim b As New BinaryFormatter()
      b.Serialize(ms, car)

      Dim con As New
SqlConnection("server=localhost;uid=sa;pwd=;database=test")
      con.Open()

      Dim ad As New SqlDataAdapter("select * from tst", con)
      Dim ds As New DataSet()
      ad.Fill(ds, "tblFile")
      Dim dbTable As DataTable = ds.Tables("tblFile")

      Dim dbRow As DataRow = dbTable.NewRow()

      ' Store data in the row
      dbRow("id") = "kimlee"
      dbRow("obj") = ms.GetBuffer()
      dbRow("len") = ms.Length
      dbTable.Rows.Add(dbRow)

      ' Update data source
      Dim cb As New SqlCommandBuilder(ad)
      ad.Update(ds, "tblFile")

      Dim comd As New SqlCommand("select * from tst", con)
      Dim sr As SqlDataReader = comd.ExecuteReader()
      While sr.Read()
         Dim cc(CInt(sr("len"))) As Byte
         sr.GetBytes(1, 0, cc, 0, CInt(sr("len")))

         Dim mss As New MemoryStream(cc)
         Dim dcar As Car = CType(b.Deserialize(mss), Car)
         dcar.Up()
         Console.Write("name:")
         Console.WriteLine(dcar.name)
         Console.Write("age:")
         Console.WriteLine(dcar.age)
      End While
      con.Close()
   End Sub 'Main
End Class 'Test

HTH

David

Quote:
> Sorry, I should have mentioned that I was using VB.NET.  Is there a
> comparable method?

> thanks,

> BB



> > Look at this: (It isn't mine)

> > using System;
> > using System.IO;
> > using System.Data;
> > using System.Data.SqlClient;
> > using System.Runtime.Serialization;
> > using System.Runtime.Serialization.Formatters.Binary;

> > [Serializable]
> > public class Car
> > {
> > public string name;
> > public int age;
> > public Car(string name,int age)
> > {
> > this.name=name;
> > this.age=age;
> > }
> > public void Up()
> > {
> > age++;
> > }

> > }

> > public class Test
> > {
> > public static void Main()
> > {
> > Console.Write("name:");
> > string sname=Console.ReadLine();
> > Console.Write("age:");
> > string sage=Console.ReadLine();
> > Car car=new Car(sname,Int32.Parse(sage));
> > MemoryStream ms=new MemoryStream();
> > BinaryFormatter b=new BinaryFormatter();
> > b.Serialize(ms,car);

> > SqlConnection con=new
> > SqlConnection("server=localhost;uid=sa;pwd=;database=test");
> > con.Open();

> > SqlDataAdapter ad=new SqlDataAdapter("select * from tst",con);
> > DataSet ds=new DataSet();
> > ad.Fill(ds,"tblFile");
> > DataTable dbTable = ds.Tables["tblFile"];

> > DataRow dbRow = dbTable.NewRow();

> > // Store data in the row
> > dbRow["id"] = "kimlee";
> > dbRow["obj"] = ms.GetBuffer();
> > dbRow["len"]=ms.Length;
> > dbTable.Rows.Add(dbRow);

> > // Update data source
> > SqlCommandBuilder cb=new SqlCommandBuilder(ad);
> > ad.Update(ds, "tblFile");

> > SqlCommand comd=new SqlCommand("select * from tst",con);
> > SqlDataReader sr=comd.ExecuteReader();
> > while(sr.Read())
> > {
> > byte[] cc=new byte[(int)sr["len"]];
> > sr.GetBytes(1,0,cc,0,(int)sr["len"]);

> > MemoryStream mss=new MemoryStream(cc);
> > Car dcar=(Car)b.Deserialize(mss);
> > dcar.Up();
> > Console.Write("name:");
> > Console.WriteLine(dcar.name) ;
> > Console.Write("age:");
> > Console.WriteLine(dcar.age) ;
> > }
> > con.Close();
> > }

> > }

> > Best Regards,
> > --
> > Antonio Cangiano
> > Webmaster di www.visualcsharp.it
> > Fondatore di free.it.comp.lang.csharp



Wed, 02 Feb 2005 00:41:28 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. console application -- i need the console to stay visible after program has finished

2. controlling a console application from a VB.NET application

3. Web Browser control in a console application ?

4. Console Application and Scheduled Task.

5. How to pause console Application for a minute and then let it run itself

6. Any way to close CONSOLE without ending application?

7. Console Window IN VB .NET GUI Application?

8. write to console from windows service application

9. How to put a Prompt to my console application

10. Web Browser control in a console application ?

11. Console Application CommandLine Parameters

12. writing in console application

 

 
Powered by phpBB® Forum Software