473,796 Members | 2,741 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

missing fundamentals of updating a database

I have written a few applications to read data from a database but now, as
I turn my attention to writing to a database, I am apparently missing some
fundamentals. I created a new database with a new (empty) table called
Version. I then created a new data source in the Data Sources pane in
Visual Studio 2005, yielding a DataSet named MyTestDataSet to reference
the Version table. I then created a short WinForms program to just run
this method:

private void TestIt()
{
MyTestDataSet myTestDataSet = new MyTestDataSet() ;
MyTestDataSetTa bleAdapters.Ver sionTableAdapte r versionTableAda pter
= new MyTestDataSetTa bleAdapters.Ver sionTableAdapte r();
MyTestDataSet.V ersionRow newRow = myTestDataSet.V ersion.NewVersi onRow();
newRow.Server = "server A";
newRow.Version = "v1";
newRow.RecordDa teTime = DateTime.Now;
versionTableAda pter.Update(new Row);
}

It runs without error or exception, but the table remains empty. Why?
Oct 4 '06 #1
8 1358
michael,

I have shied away from using datasets. My day will come though. If you
want to insert/edit data then it looks like the prefered way is through
a data reader.

Also look up parametrized queries.

example: (almost quaranteed not to compile but hopefully I am near)

private void InsertRecord()
{
string sSQLStatement;
SqlCommand ProcLotCmd;
SqlTransaction myTransaction = null;

string sPersonName = "Gina";
string sPersonID = "1";

// Create the connection and transaction objects.
SqlConnection myPrivConnectio n = new SqlConnection(" user
id=myuserid;" +

"password=mypwd ;server=myserve r;" +
"database=m ydb; "
+
"connection
timeout=30");
myPrivConnectio n.Open();
try
{
myTransaction = myPrivConnectio n.BeginTransact ion();
try
{
sSQLStatement = "INSERT INTO People (PeopleID, PeopleName) " +
sPersonID + ", " + sPersonName;
ProcLotCmd = new SqlCommand(sSQL Statement, myPrivConnectio n);

ProcLotCmd.Tran saction = myTransaction;
ProcLotCmd.Exec uteNonQuery();
myTransaction.C ommit();
}
catch (Exception ex)
{
myTransaction.R ollback();
}
}
finally
{
// Close the connection.
if (myPrivConnecti on.State == ConnectionState .Open)
myPrivConnectio n.Close();
}
}
On Oct 4, 1:19 pm, "michael sorens" <m_j_sor...@com munity.nospam>
wrote:
I have written a few applications to read data from a database but now, as
I turn my attention to writing to a database, I am apparently missing some
fundamentals. I created a new database with a new (empty) table called
Version. I then created a new data source in the Data Sources pane in
Visual Studio 2005, yielding a DataSet named MyTestDataSet to reference
the Version table. I then created a short WinForms program to just run
this method:

private void TestIt()
{
MyTestDataSet myTestDataSet = new MyTestDataSet() ;
MyTestDataSetTa bleAdapters.Ver sionTableAdapte r versionTableAda pter
= new MyTestDataSetTa bleAdapters.Ver sionTableAdapte r();
MyTestDataSet.V ersionRow newRow = myTestDataSet.V ersion.NewVersi onRow();
newRow.Server = "server A";
newRow.Version = "v1";
newRow.RecordDa teTime = DateTime.Now;
versionTableAda pter.Update(new Row);
}

It runs without error or exception, but the table remains empty. Why?
Oct 4 '06 #2

sorry my example covers writing to the database. If you want to read
from it use the datareader.

~Gina~

On Oct 4, 4:45 pm, "Gina_Maran o" <ginals...@gmai l.comwrote:
michael,

I have shied away from using datasets. My day will come though. If you
want to insert/edit data then it looks like the prefered way is through
a data reader.

Also look up parametrized queries.

example: (almost quaranteed not to compile but hopefully I am near)

private void InsertRecord()
{
string sSQLStatement;
SqlCommand ProcLotCmd;
SqlTransaction myTransaction = null;

string sPersonName = "Gina";
string sPersonID = "1";

// Create the connection and transaction objects.
SqlConnection myPrivConnectio n = new SqlConnection(" user
id=myuserid;" +

"password=mypwd ;server=myserve r;" +
"database=m ydb; "
+
"connection
timeout=30");
myPrivConnectio n.Open();
try
{
myTransaction = myPrivConnectio n.BeginTransact ion();
try
{
sSQLStatement = "INSERT INTO People (PeopleID, PeopleName) " +
sPersonID + ", " + sPersonName;
ProcLotCmd = new SqlCommand(sSQL Statement, myPrivConnectio n);

ProcLotCmd.Tran saction = myTransaction;
ProcLotCmd.Exec uteNonQuery();
myTransaction.C ommit();
}
catch (Exception ex)
{
myTransaction.R ollback();
}
}
finally
{
// Close the connection.
if (myPrivConnecti on.State == ConnectionState .Open)
myPrivConnectio n.Close();
}

}On Oct 4, 1:19 pm, "michael sorens" <m_j_sor...@com munity.nospam>
wrote:
I have written a few applications to read data from a database but now, as
I turn my attention to writing to a database, I am apparently missing some
fundamentals. I created a new database with a new (empty) table called
Version. I then created a new data source in the Data Sources pane in
Visual Studio 2005, yielding a DataSet named MyTestDataSet to reference
the Version table. I then created a short WinForms program to just run
this method:
private void TestIt()
{
MyTestDataSet myTestDataSet = new MyTestDataSet() ;
MyTestDataSetTa bleAdapters.Ver sionTableAdapte r versionTableAda pter
= new MyTestDataSetTa bleAdapters.Ver sionTableAdapte r();
MyTestDataSet.V ersionRow newRow = myTestDataSet.V ersion.NewVersi onRow();
newRow.Server = "server A";
newRow.Version = "v1";
newRow.RecordDa teTime = DateTime.Now;
versionTableAda pter.Update(new Row);
}
It runs without error or exception, but the table remains empty. Why?- Hide quoted text -- Show quoted text -
Oct 4 '06 #3
Hi Michael,

The reason that the new record isn't saved to the DB is that the new row
has not been added to the Version datatable before you call
versionTableAda pter.Update(new Row).

You should add the following line of code before you call the Update method
of the versionTableAda pter.

myTestDataSet.V ersion.Rows.Add (newRow);

Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 5 '06 #4

Michael

Add a few

Console.WriteLi ne(myTestDataSe t.GetXml());
lines into your debugging process.

That will show you more quickly than anything whats going on.

But yes, another poster is correct.

myTestDataSet.V ersion.AddVersi onRow (newRow);
(or something like that) is needed.


"michael sorens" <m_********@com munity.nospamwr ote in message
news:op******** *******@spo-cont-2-dt.itron.com...
I have written a few applications to read data from a database but now, as
I turn my attention to writing to a database, I am apparently missing some
fundamentals. I created a new database with a new (empty) table called
Version. I then created a new data source in the Data Sources pane in
Visual Studio 2005, yielding a DataSet named MyTestDataSet to reference
the Version table. I then created a short WinForms program to just run
this method:

private void TestIt()
{
MyTestDataSet myTestDataSet = new MyTestDataSet() ;
MyTestDataSetTa bleAdapters.Ver sionTableAdapte r versionTableAda pter
= new MyTestDataSetTa bleAdapters.Ver sionTableAdapte r();
MyTestDataSet.V ersionRow newRow = myTestDataSet.V ersion.NewVersi onRow();
newRow.Server = "server A";
newRow.Version = "v1";
newRow.RecordDa teTime = DateTime.Now;
versionTableAda pter.Update(new Row);
}

It runs without error or exception, but the table remains empty. Why?
Oct 5 '06 #5
The suggestion to add the new row to the dataset, as in --
myTestDataSet.V ersion.Rows.Add (newRow);

--does in fact work, but it leaves me befuddled.
If the xxxTableAdapter .Update method relies on the underlying DataSet, why
does the Update method require an argument at all? And what effect does
that choice of argument make?

My reading of the MSDN docs led me to believe that an Update with a
DataSet would essentially compare the entire DataSet with the entire
database table, then execute inserts, updates, or deletes to make the
database match the DataSet. So when I saw that the Update method could be
given an argument of a DataRow array, I thought--aha!--it would then only
insert or update the items in the DataRows, leaving the rest of the
database table alone. But obviously my interpretation was wrong. Once I
added the above statement, then either of these:
versionTableAda pter.Update(new Row);
versionTableAda pter.Update(myT estDataSet);
produce the identical result, i.e. to add the new row to the database
table, and leave everything else in the database table alone.

Some clarification would be appreciated :-)
Oct 5 '06 #6
Hi Michael,

You're almost right.

When we call the Update method of the TableAdapter with a DataSet or
DataTable, all the changes including insert, update and delete in the
DataSet or DataTable would be saved to the DB.

When we call the Update method of the TableAdapetr with a DataRow, changes
including insert, update and delete in the DataRow will be saved to the DB,
leaving the rest rows of the datatable alone. Note that the precondition is
that the datarow BELONGS TO a datatable.

If we create a new row, we should add it to a DataTable before calling the
TableAdapter.Up date method with the new row. Otherwise, TableAdapter won't
know which table the new row would be saved to.

Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

Oct 6 '06 #7
After I read your note a couple times, Linda, I think I realized
something, but still need some confirmation.
Update(DataSet) will examine every row in the DataSet (not in the
underlying DB table:-), then see if it needs updating
or needs inserting into the underlying DB table. That explains why the two
lines I indicated produce the same result, because the DataSet contained
just the one new row:
versionTableAda pter.Update(new Row);
versionTableAda pter.Update(myT estDataSet);

The one part I am missing is when would a delete operation be needed? If I
delete a row from the DataSet it is no longer there to be compared to the
DB table, right? Or is it retained in the DataSet and just marked as
"deleted" so the Update() will then know to delete it from the underlying
DB table?

Final thought: If I want an Update() to mirror the entire underlying DB
table, I could simply initialize it to the entire table, then proceed to
add rows, delete rows, modify rows... When I do an Update() in this case,
it will examine every row in the DataSet (and thus every row in the
underlying DB table) and synchronize them, right?
Oct 6 '06 #8
Hi Michael,

As for the question of delete operation, the second comment is correct.
When we delete a row from a DataTable, this row is just marked as
'deleted'. In fact, this row won't be removed from the DatTable until we
call DataTable.Accep tChanges method.

As for your last question, your thought is right. We could add, delete or
modify rows in the DataTable and then call the Update method to save all
these changes to the DB at one time.

If you have any concerns, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

Oct 9 '06 #9

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

Similar topics

11
16231
by: Jason | last post by:
Let's say I have an html form with 20 or 30 fields in it. The form submits the fields via POST to a php page which updates a table in a database with the $_POST vars. Which makes more sense? 1) simply UPDATING the values for all fields in the table, whether or not any particular field has actually changed 2) running a second SELECT statement and comparing the $_POST vars to the returned values, and only UPDATING those that have...
2
6399
by: btober | last post by:
I run the following script to export some data from my development database and then update or insert the records into to the quality assurance testing database, but I get a warning notice that I don't understand. Aside from that notice, the script appears to work as intended, i.e., updating existing report definitions and adding any new ones defined in the dev environment but not appearing in QAT. Here is the script: \set...
1
2042
by: gaosul | last post by:
I am non-programming scientist and I am using a Program called Easyarticles from Synaptosoft Inc., which is based the database program Access. Unfortunately, the owner of this company has disappeared even though his website still functions. As the program requires updating, I was wondering whether anyone has any suggestions, as to what I should do. The program allows you to search a public database called PubMed. You can mark abstracts...
3
1407
by: Bucko | last post by:
How important do you guys feel locking a database is while updating/adding information? Do you do it with every app you make? Only very high volume (traffic) apps? I'm trying to decide if my app needs it.
5
2041
by: junglist | last post by:
Hi guys, I've been trying to implement an editable datagrid and i have been succesful up to the point where i can update my datagrid row by row. However what used to happen was that once i updated one row, all of them were updated so i immediatelly figured out that i have to include the id of every entry in the update statement. This is where the problem is raised. My database is an Access database. The table i am updating contains a Date...
10
5688
by: jaYPee | last post by:
does anyone experienced slowness when updating a dataset using AcceptChanges? when calling this code it takes many seconds to update the database SqlDataAdapter1.Update(DsStudentCourse1) DsStudentCourse1.AcceptChanges() i'm also wondering because w/ out AcceptChanges the data is still save into the database and it is now faster.
2
2863
by: Alexey.Murin | last post by:
The application we are developing uses MS Access 2003 database (with help of ADO). We have noticed that during massive records updating the size of the mdb file increases dramatically (from 3-4 to 200-300 Mb). After compacting the file shrinks back to 3-4 Mb. I have performed the following experiment. I created a test database containing only one table with two columns (Key number, Value text(50)). The table contains 10 thousands...
3
17474
by: kaisersose1995 | last post by:
Hi, The problem i'm having is that, i've been updating an access 97 database that was originally created in an NT4 environment. My machine is running xp and the same developers edition of access 97. The current database runs fine on the users machine with no erros at all. but when using my updated database all Date() fields appears as Name? and throw error "missing project or library" when used in code. I also have problems using Set...
10
3419
tpgames
by: tpgames | last post by:
I just inherited a desktop with ubuntu installed and a bunch of other cool apps. The issue is that the person who gave me the computer accidentally deleted the folder that update manager uses to actually update the programs after one oks updating. Thus, I can't update anything using manager. The folder was called "pool" I think. Question: If the last update of anything was around Firefox 2.00.1 for all programs, including GIMP, Open Office,...
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10467
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
10244
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...
1
10201
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
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...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5454
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2931
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.