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 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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,...
|
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 /?
|
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?
|
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
|
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
| |
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?
|
by: Mythran |
last post by:
Is it possible to attach Windows WndProc hooks into a Console application
window?
Thanks,
Mythran
|
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?
|
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....
|
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.
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |