PC Review


Reply
Thread Tools Rate Thread

C# ADO.NET console application

 
 
krukinews@gmail.com
Guest
Posts: n/a
 
      23rd Mar 2005
Hi,
I'm trying to write small program (console application) that reads from
Event Log end writes data to SQL Server. I'm new to C# and ADO.NET and
for last 3 days I have read many tutorials and I think that I
understand basic (only basic) concepts of ADO.NET.

But I simple can't create console application that uses Master Detail
concept.

Table structure (keys only).
tblServer
ID_Server (auto inc)

tblEventLogs
ID_EventLog (auto inc)
ID_Server

tblEventLogEntries
ID_EventLogEntry (auto inc)
ID_EventLog

//my code

public static SqlDataAdapter CreateDataAdapter(string selectSQL,
SqlConnection conn) {
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(selectSQL, conn);

SqlCommandBuilder cb = new SqlCommandBuilder(da);

da.InsertCommand = cb.GetInsertCommand();
da.UpdateCommand = cb.GetUpdateCommand();
da.DeleteCommand = cb.GetDeleteCommand();

return da;
}

SqlConnection connMain = new
SqlConnection("server=server;uid=uid;pwd=pws;database=database;");
connMain.Open();

SqlDataAdapter daServers = CreateDataAdapter("SELECT * FROM
tblServers", connMain);
SqlDataAdapter daEventLogs = CreateDataAdapter("SELECT * FROM
tblEventLogs", connMain);
SqlDataAdapter daEventLogEntries = CreateDataAdapter("SELECT * FROM
tblEventLogEntries", connMain);

DataSet dsMain = new DataSet();

daServers.FillSchema(dsMain, SchemaType.Source, "tblServers");
daEventLogs.FillSchema(dsMain, SchemaType.Source, "tblEventLogs");
daEventLogEntries.FillSchema(dsMain, SchemaType.Source,
"tblEventLogEntries");

daServers.Fill(dsMain, "tblServers");
daEventLogs.Fill(dsMain, "tblEventLogs");
daEventLogEntries.Fill(dsMain, "tblEventLogEntries");

dsMain.Tables["tblServers"].DefaultView.Sort = "ServerName";
dsMain.Tables["tblEventLogs"].DefaultView.Sort = "ID_Server,LogName";

//my implementation of Current record (any better idea?)
DataRow rowServer;
DataRow rowEventLog;

int tmp =
dsMain.Tables["tblServers"].DefaultView.Find(strComputerName);

if (tmp < 0) {
rowServer = dsMain.Tables["tblServers"].NewRow();
rowServer["ServerName"] = strComputerName;
dsMain.Tables["tblServers"].Rows.Add(rowServer);
daServers.Update(dsMain, "tblServers");
} else {
rowServer = dsMain.Tables["tblServers"].Rows[tmp];
}

Problem is that I need ID_Server of new record to create records in
tbl_EventLogs but rowServer["ID_Server"] of new record is always 0. I
think that this has to do something with diconnected record set (or
not).

Any sugestions are welcome.


Mihalic Krunoslav

 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      23rd Mar 2005
Mihalic,

Once you have the structure of the data in your data set, create
DataRelation instances that link the parent and the child tables correctly.
If you do this correctly, then when you run the tables through the data
adapter for update, it should detect the relations, and get the appropriate
id of the parent.

Hope this helps.



<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
> I'm trying to write small program (console application) that reads from
> Event Log end writes data to SQL Server. I'm new to C# and ADO.NET and
> for last 3 days I have read many tutorials and I think that I
> understand basic (only basic) concepts of ADO.NET.
>
> But I simple can't create console application that uses Master Detail
> concept.
>
> Table structure (keys only).
> tblServer
> ID_Server (auto inc)
>
> tblEventLogs
> ID_EventLog (auto inc)
> ID_Server
>
> tblEventLogEntries
> ID_EventLogEntry (auto inc)
> ID_EventLog
>
> //my code
>
> public static SqlDataAdapter CreateDataAdapter(string selectSQL,
> SqlConnection conn) {
> SqlDataAdapter da = new SqlDataAdapter();
> da.SelectCommand = new SqlCommand(selectSQL, conn);
>
> SqlCommandBuilder cb = new SqlCommandBuilder(da);
>
> da.InsertCommand = cb.GetInsertCommand();
> da.UpdateCommand = cb.GetUpdateCommand();
> da.DeleteCommand = cb.GetDeleteCommand();
>
> return da;
> }
>
> SqlConnection connMain = new
> SqlConnection("server=server;uid=uid;pwd=pws;database=database;");
> connMain.Open();
>
> SqlDataAdapter daServers = CreateDataAdapter("SELECT * FROM
> tblServers", connMain);
> SqlDataAdapter daEventLogs = CreateDataAdapter("SELECT * FROM
> tblEventLogs", connMain);
> SqlDataAdapter daEventLogEntries = CreateDataAdapter("SELECT * FROM
> tblEventLogEntries", connMain);
>
> DataSet dsMain = new DataSet();
>
> daServers.FillSchema(dsMain, SchemaType.Source, "tblServers");
> daEventLogs.FillSchema(dsMain, SchemaType.Source, "tblEventLogs");
> daEventLogEntries.FillSchema(dsMain, SchemaType.Source,
> "tblEventLogEntries");
>
> daServers.Fill(dsMain, "tblServers");
> daEventLogs.Fill(dsMain, "tblEventLogs");
> daEventLogEntries.Fill(dsMain, "tblEventLogEntries");
>
> dsMain.Tables["tblServers"].DefaultView.Sort = "ServerName";
> dsMain.Tables["tblEventLogs"].DefaultView.Sort = "ID_Server,LogName";
>
> //my implementation of Current record (any better idea?)
> DataRow rowServer;
> DataRow rowEventLog;
>
> int tmp =
> dsMain.Tables["tblServers"].DefaultView.Find(strComputerName);
>
> if (tmp < 0) {
> rowServer = dsMain.Tables["tblServers"].NewRow();
> rowServer["ServerName"] = strComputerName;
> dsMain.Tables["tblServers"].Rows.Add(rowServer);
> daServers.Update(dsMain, "tblServers");
> } else {
> rowServer = dsMain.Tables["tblServers"].Rows[tmp];
> }
>
> Problem is that I need ID_Server of new record to create records in
> tbl_EventLogs but rowServer["ID_Server"] of new record is always 0. I
> think that this has to do something with diconnected record set (or
> not).
>
> Any sugestions are welcome.
>
>
> Mihalic Krunoslav
>



 
Reply With Quote
 
Victor Pereira
Guest
Posts: n/a
 
      24th Mar 2005
Please.. use try and catch, to catch all exceptions, and protect your code.

Reguards,

Victor
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
> I'm trying to write small program (console application) that reads from
> Event Log end writes data to SQL Server. I'm new to C# and ADO.NET and
> for last 3 days I have read many tutorials and I think that I
> understand basic (only basic) concepts of ADO.NET.
>
> But I simple can't create console application that uses Master Detail
> concept.
>
> Table structure (keys only).
> tblServer
> ID_Server (auto inc)
>
> tblEventLogs
> ID_EventLog (auto inc)
> ID_Server
>
> tblEventLogEntries
> ID_EventLogEntry (auto inc)
> ID_EventLog
>
> //my code
>
> public static SqlDataAdapter CreateDataAdapter(string selectSQL,
> SqlConnection conn) {
> SqlDataAdapter da = new SqlDataAdapter();
> da.SelectCommand = new SqlCommand(selectSQL, conn);
>
> SqlCommandBuilder cb = new SqlCommandBuilder(da);
>
> da.InsertCommand = cb.GetInsertCommand();
> da.UpdateCommand = cb.GetUpdateCommand();
> da.DeleteCommand = cb.GetDeleteCommand();
>
> return da;
> }
>
> SqlConnection connMain = new
> SqlConnection("server=server;uid=uid;pwd=pws;database=database;");
> connMain.Open();
>
> SqlDataAdapter daServers = CreateDataAdapter("SELECT * FROM
> tblServers", connMain);
> SqlDataAdapter daEventLogs = CreateDataAdapter("SELECT * FROM
> tblEventLogs", connMain);
> SqlDataAdapter daEventLogEntries = CreateDataAdapter("SELECT * FROM
> tblEventLogEntries", connMain);
>
> DataSet dsMain = new DataSet();
>
> daServers.FillSchema(dsMain, SchemaType.Source, "tblServers");
> daEventLogs.FillSchema(dsMain, SchemaType.Source, "tblEventLogs");
> daEventLogEntries.FillSchema(dsMain, SchemaType.Source,
> "tblEventLogEntries");
>
> daServers.Fill(dsMain, "tblServers");
> daEventLogs.Fill(dsMain, "tblEventLogs");
> daEventLogEntries.Fill(dsMain, "tblEventLogEntries");
>
> dsMain.Tables["tblServers"].DefaultView.Sort = "ServerName";
> dsMain.Tables["tblEventLogs"].DefaultView.Sort = "ID_Server,LogName";
>
> //my implementation of Current record (any better idea?)
> DataRow rowServer;
> DataRow rowEventLog;
>
> int tmp =
> dsMain.Tables["tblServers"].DefaultView.Find(strComputerName);
>
> if (tmp < 0) {
> rowServer = dsMain.Tables["tblServers"].NewRow();
> rowServer["ServerName"] = strComputerName;
> dsMain.Tables["tblServers"].Rows.Add(rowServer);
> daServers.Update(dsMain, "tblServers");
> } else {
> rowServer = dsMain.Tables["tblServers"].Rows[tmp];
> }
>
> Problem is that I need ID_Server of new record to create records in
> tbl_EventLogs but rowServer["ID_Server"] of new record is always 0. I
> think that this has to do something with diconnected record set (or
> not).
>
> Any sugestions are welcome.
>
>
> Mihalic Krunoslav
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Console application. Console.Clear = IOException Dinsdale Microsoft VB .NET 6 14th Mar 2008 06:36 PM
Re: console application without visible console? Jon Skeet [C# MVP] Microsoft Dot NET Framework 0 8th Apr 2006 04:10 PM
How to run a console application without showing the console screen? Tee Microsoft C# .NET 2 11th Feb 2005 06:22 AM
How to run a console application without showing the console screen? Tee Microsoft VB .NET 2 11th Feb 2005 06:22 AM
How to set a fixed line in a console application liek UNIX console AA Microsoft C# .NET 1 6th Dec 2003 02:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:49 PM.