473,835 Members | 2,159 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

tblEventLogEntr ies
ID_EventLogEntr y (auto inc)
ID_EventLog

//my code

public static SqlDataAdapter CreateDataAdapt er(string selectSQL,
SqlConnection conn) {
SqlDataAdapter da = new SqlDataAdapter( );
da.SelectComman d = new SqlCommand(sele ctSQL, conn);

SqlCommandBuild er cb = new SqlCommandBuild er(da);

da.InsertComman d = cb.GetInsertCom mand();
da.UpdateComman d = cb.GetUpdateCom mand();
da.DeleteComman d = cb.GetDeleteCom mand();

return da;
}

SqlConnection connMain = new
SqlConnection(" server=server;u id=uid;pwd=pws; database=databa se;");
connMain.Open() ;

SqlDataAdapter daServers = CreateDataAdapt er("SELECT * FROM
tblServers", connMain);
SqlDataAdapter daEventLogs = CreateDataAdapt er("SELECT * FROM
tblEventLogs", connMain);
SqlDataAdapter daEventLogEntri es = CreateDataAdapt er("SELECT * FROM
tblEventLogEntr ies", connMain);

DataSet dsMain = new DataSet();

daServers.FillS chema(dsMain, SchemaType.Sour ce, "tblServers ");
daEventLogs.Fil lSchema(dsMain, SchemaType.Sour ce, "tblEventLogs") ;
daEventLogEntri es.FillSchema(d sMain, SchemaType.Sour ce,
"tblEventLogEnt ries");

daServers.Fill( dsMain, "tblServers ");
daEventLogs.Fil l(dsMain, "tblEventLogs") ;
daEventLogEntri es.Fill(dsMain, "tblEventLogEnt ries");

dsMain.Tables["tblServers "].DefaultView.So rt = "ServerName ";
dsMain.Tables["tblEventLo gs"].DefaultView.So rt = "ID_Server,LogN ame";

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

int tmp =
dsMain.Tables["tblServers "].DefaultView.Fi nd(strComputerN ame);

if (tmp < 0) {
rowServer = dsMain.Tables["tblServers "].NewRow();
rowServer["ServerName "] = strComputerName ;
dsMain.Tables["tblServers "].Rows.Add(rowSe rver);
daServers.Updat e(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 7912
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*******@gmai l.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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

tblEventLogEntr ies
ID_EventLogEntr y (auto inc)
ID_EventLog

//my code

public static SqlDataAdapter CreateDataAdapt er(string selectSQL,
SqlConnection conn) {
SqlDataAdapter da = new SqlDataAdapter( );
da.SelectComman d = new SqlCommand(sele ctSQL, conn);

SqlCommandBuild er cb = new SqlCommandBuild er(da);

da.InsertComman d = cb.GetInsertCom mand();
da.UpdateComman d = cb.GetUpdateCom mand();
da.DeleteComman d = cb.GetDeleteCom mand();

return da;
}

SqlConnection connMain = new
SqlConnection(" server=server;u id=uid;pwd=pws; database=databa se;");
connMain.Open() ;

SqlDataAdapter daServers = CreateDataAdapt er("SELECT * FROM
tblServers", connMain);
SqlDataAdapter daEventLogs = CreateDataAdapt er("SELECT * FROM
tblEventLogs", connMain);
SqlDataAdapter daEventLogEntri es = CreateDataAdapt er("SELECT * FROM
tblEventLogEntr ies", connMain);

DataSet dsMain = new DataSet();

daServers.FillS chema(dsMain, SchemaType.Sour ce, "tblServers ");
daEventLogs.Fil lSchema(dsMain, SchemaType.Sour ce, "tblEventLogs") ;
daEventLogEntri es.FillSchema(d sMain, SchemaType.Sour ce,
"tblEventLogEnt ries");

daServers.Fill( dsMain, "tblServers ");
daEventLogs.Fil l(dsMain, "tblEventLogs") ;
daEventLogEntri es.Fill(dsMain, "tblEventLogEnt ries");

dsMain.Tables["tblServers "].DefaultView.So rt = "ServerName ";
dsMain.Tables["tblEventLo gs"].DefaultView.So rt = "ID_Server,LogN ame";

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

int tmp =
dsMain.Tables["tblServers "].DefaultView.Fi nd(strComputerN ame);

if (tmp < 0) {
rowServer = dsMain.Tables["tblServers "].NewRow();
rowServer["ServerName "] = strComputerName ;
dsMain.Tables["tblServers "].Rows.Add(rowSe rver);
daServers.Updat e(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*******@gmai l.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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

tblEventLogEntr ies
ID_EventLogEntr y (auto inc)
ID_EventLog

//my code

public static SqlDataAdapter CreateDataAdapt er(string selectSQL,
SqlConnection conn) {
SqlDataAdapter da = new SqlDataAdapter( );
da.SelectComman d = new SqlCommand(sele ctSQL, conn);

SqlCommandBuild er cb = new SqlCommandBuild er(da);

da.InsertComman d = cb.GetInsertCom mand();
da.UpdateComman d = cb.GetUpdateCom mand();
da.DeleteComman d = cb.GetDeleteCom mand();

return da;
}

SqlConnection connMain = new
SqlConnection(" server=server;u id=uid;pwd=pws; database=databa se;");
connMain.Open() ;

SqlDataAdapter daServers = CreateDataAdapt er("SELECT * FROM
tblServers", connMain);
SqlDataAdapter daEventLogs = CreateDataAdapt er("SELECT * FROM
tblEventLogs", connMain);
SqlDataAdapter daEventLogEntri es = CreateDataAdapt er("SELECT * FROM
tblEventLogEntr ies", connMain);

DataSet dsMain = new DataSet();

daServers.FillS chema(dsMain, SchemaType.Sour ce, "tblServers ");
daEventLogs.Fil lSchema(dsMain, SchemaType.Sour ce, "tblEventLogs") ;
daEventLogEntri es.FillSchema(d sMain, SchemaType.Sour ce,
"tblEventLogEnt ries");

daServers.Fill( dsMain, "tblServers ");
daEventLogs.Fil l(dsMain, "tblEventLogs") ;
daEventLogEntri es.Fill(dsMain, "tblEventLogEnt ries");

dsMain.Tables["tblServers "].DefaultView.So rt = "ServerName ";
dsMain.Tables["tblEventLo gs"].DefaultView.So rt = "ID_Server,LogN ame";

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

int tmp =
dsMain.Tables["tblServers "].DefaultView.Fi nd(strComputerN ame);

if (tmp < 0) {
rowServer = dsMain.Tables["tblServers "].NewRow();
rowServer["ServerName "] = strComputerName ;
dsMain.Tables["tblServers "].Rows.Add(rowSe rver);
daServers.Updat e(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
5389
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 application (VB.NET) will start a process, redirect its stdout and capture that process' output, displaying it in a window. I've written a component for this, and a test application for the component. It allows me to specify a command to execute,...
5
4058
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 batch interface. my problem is that i can output the string to the dos prompt when running the exe file at dos prompt: c:\>test.exe /?
6
3238
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 I have all the code set up to do this, however when I issue a Console.WriteLine instruction, nothing gets written to the console. In fact, the command prompt comes back before the program has finished executing. What do I need to do?
5
11268
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 to avoid the overhead of writing to the console if it is not visible, eg I am running inside a WinForm application. thanks
17
4243
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 console using System.Console.WriteLine if thge project has its "Output Type" to "Windows Application". However I can write to stdio if i set output type to "Console Application". When I do this I unfortunately get a "console box" as well
3
15034
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
6208
by: Mythran | last post by:
Is it possible to attach Windows WndProc hooks into a Console application window? Thanks, Mythran
6
5718
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 possible to use Console.Writeln when you have a Webservice. I don't think it's possible but just to be sure I ask you?
10
6351
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 so that the window for the Console application is not visible. However, when using some of the 'advanced' properties of the StartInfo object, like Username, Password and Domain, the WindowsStyle property of the StartInfo object is ignored....
12
6552
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 combination to hide the console window at launch time. The application (for legacy reasons) hangs around by waiting on an old- fashioned Console.ReadLine() statement.
0
10811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10523
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10233
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7766
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6966
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5804
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4434
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3088
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.