473,508 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# ADO.NET console application

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;datab ase=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

Nov 17 '05 #1
2 7891
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.

<kr*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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;datab ase=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

Nov 17 '05 #2
Please.. use try and catch, to catch all exceptions, and protect your code.

Reguards,

Victor
<kr*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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;datab ase=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

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5350
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
5
4034
by: Mullin Yu | last post by:
i want to build an application of both gui and batch interface by using windows application project. i check either passing any args or not. if no, then open the gui application. if yes, use the...
6
3223
by: Mark Allison | last post by:
Hi, I have an application that I want to be to run in Console mode and GUI mode. If no params are entered, I want the GUI fired up, if params are entered, then go into console mode. I believe...
5
11193
by: Barry Mossman | last post by:
Hi, can I detect whether my class is running within the context of a Console application, vs say a WinForm's application ? also does anyone know whether the compiler or runtime is smart enough...
17
4194
by: MumboJumbo | last post by:
Hi I have a really basic question hopefully some can help me with: Can you write a (i.e. one) C# project that works from the cmd line and gui? I seems if i write a GUI app it can't write to...
3
15012
by: inpreet | last post by:
I am trying to build a console application in C#.Net. This application is suppose to run in background without user interaction. How can I hide console to appear?
6
6162
by: Mythran | last post by:
Is it possible to attach Windows WndProc hooks into a Console application window? Thanks, Mythran
6
5702
by: tony | last post by:
Hello! When you have windows forms you have the same possibility as when you have a Console application to use Console.Writeln to write whatever on the screen. Now to my question: Is it...
10
6288
by: Stephany Young | last post by:
When one uses the System.Diagnostics.Process.Start method to launch a common or garden Console application, one can set the WindowStyle property of the StartInfo object to ProcessWindowStyle.Hidden...
12
6497
by: Dilip | last post by:
Hi All I have a server based C# console application. This application must hide its console window when its launched out on the field. So I dutifully P/Invoke'd FindWindow/ShowWindow...
0
7227
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7127
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7331
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7391
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
424
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.