473,767 Members | 8,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete a row in the database

Hello!

Here I have a working program that delete the specified row in the database.
It works fine but I want to fully understand it.
In this example the SQLCommandBuild er will automatically create Sqlcommands
for
update, insert and delete.

In this program I specify that I want to delete the row with primary key =
"ZACHI"
So I had hoped that this string would be included in the command
deleteCommand.C ommandText.

When I check this command just before the update command the string ZACHI is
not included at all
in the deleteCommand.

So how can this work if not the string ZACHI is included in the
deleteCommand ?

//Tony

static void Main(string[] args)
{
//Specify SQL Server-specific connection string

using ( SqlConnection thisConnection = new SqlConnection(
@"Server=UHT-DEMO1; Integrated Security=True;" +
"Database=north wind"))
{
//Create DataAdapter object for update and other operations
SqlDataAdapter thisAdapter = new SqlDataAdapter(
"Select CustomerID, CompanyName from Customers",
thisConnection) ;

//Create CommandBuilder object to build SQL commands
SqlCommandBuild er thisBuilder = new
SqlCommandBuild er(thisAdapter) ;

//Create DataSet to contain related data tables, rows, and
columns
DataSet thisDataSet = new DataSet();

//Fill DataSet using query previously defined for
DataAdapter
thisAdapter.Fil l(thisDataSet, "Customers" );

Console.WriteLi ne("# rows before change: {0}",
thisDataSet.Tab les["Customers"].Rows.Count);

//Set up keys object for defining primary key
DataColumn[] keys = new DataColumn[1];
keys[0] =
thisDataSet.Tab les["Customers"].Columns["CustomerID "];
thisDataSet.Tab les["Customers"].PrimaryKey = keys;

DataRow findRow =
thisDataSet.Tab les["Customers"].Rows.Find("ZAC HI");

if (findRow != null)
{
Console.WriteLi ne("ZACHI already in Customers table");
Console.WriteLi ne("Removing ZACHI . . .");

findRow.Delete( );

SqlCommand deleteCommand =
thisBuilder.Get DeleteCommand() ;
Console.WriteLi ne("SQL DELETE comamnd is = \n{0}\n",
deleteCommand.C ommandText);

thisAdapter.Upd ate(thisDataSet , "Customers" );
}

Console.WriteLi ne("# rows after change: {0}",
thisDataSet.Tab les["Customers"].Rows.Count);
}

Console.WriteLi ne("Program finished, press Enter/Return to
continue");
Console.ReadLin e();
}
Aug 25 '08 #1
5 2001
On Mon, 25 Aug 2008 11:54:56 +0200, Tony Johansson wrote:
>
In this program I specify that I want to delete the row with primary key
= "ZACHI"
So I had hoped that this string would be included in the command
deleteCommand.C ommandText.
The delete command has parameters:

delete from table where key = ?

The ? is the parameter and this is associated to a variable that contains
the key, in your case ZACHI.

Ken
Aug 25 '08 #2
Hello!

Is it possible to see the complete command after the parameter has been
inserted into the deleteCommand in any way ?

//Tony

"Ken Foskey" <rm**********@o ptushome.com.au skrev i meddelandet
news:48******** @dnews.tpgi.com .au...
On Mon, 25 Aug 2008 11:54:56 +0200, Tony Johansson wrote:

In this program I specify that I want to delete the row with primary key
= "ZACHI"
So I had hoped that this string would be included in the command
deleteCommand.C ommandText.

The delete command has parameters:

delete from table where key = ?

The ? is the parameter and this is associated to a variable that contains
the key, in your case ZACHI.

Ken

Aug 25 '08 #3
Hello!

I just wonder when will the parameter be replaced with the string ZACHI ?
Is it within the update method perhaps which will explain why I can't see it
when I look at the deleteCommand before
the update method.
"Tony Johansson" <jo************ *****@telia.com skrev i meddelandet
news:OC******** ********@TK2MSF TNGP06.phx.gbl. ..
Hello!

Is it possible to see the complete command after the parameter has been
inserted into the deleteCommand in any way ?

//Tony

"Ken Foskey" <rm**********@o ptushome.com.au skrev i meddelandet
news:48******** @dnews.tpgi.com .au...
On Mon, 25 Aug 2008 11:54:56 +0200, Tony Johansson wrote:
>
In this program I specify that I want to delete the row with primary
key
= "ZACHI"
So I had hoped that this string would be included in the command
deleteCommand.C ommandText.
The delete command has parameters:

delete from table where key = ?

The ? is the parameter and this is associated to a variable that
contains
the key, in your case ZACHI.

Ken


Aug 25 '08 #4
On Mon, 25 Aug 2008 12:57:21 +0200, Tony Johansson wrote:
Hello!

Is it possible to see the complete command after the parameter has been
inserted into the deleteCommand in any way ?
No.

Here is a simple SQL statement:

"DELETE FROM `mytable` WHERE `ID` = ?";

The delete command is handed to a prepare and the values are associated
later. I do not know the specifics of how .NET does the association
sorry.

You can build and execute your own SQL commands if you want or need to.

Ken
Aug 25 '08 #5
Ken Foskey wrote:
Here is a simple SQL statement:

"DELETE FROM `mytable` WHERE `ID` = ?";
The `` are MySQL specific.

Arne
Aug 26 '08 #6

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

Similar topics

1
8905
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB automatically deletes also all those rows in the child table whose foreign key values are equal to the referenced key value in the parent row. However:
2
11799
by: Bob Ganger | last post by:
Hello, I am working on a project using SQL Server 2000 with a database containing about 10 related tables with a lot of columns containing text. The total current size of the database is about 2 Gig. When I delete data from the database, it takes a lot of system resources and monopolizes the database so that all other query requests are slow as mud! Ideally, I would like to be able to issue delete commands to the database on a...
7
1725
by: Mike & Dyan | last post by:
I was able to figure out how to insert new data into my database. But for some reason and a lot of reading I can't seem to figure out how to delete any data. My app is going to be used for tracking info on vehicles. My app is MDI with 3 children. 1 is for selecting a car(already in the database) and input info.Then you can add that car and info to the database. Another is used for just adding or deleting a car to the database. The 3rd...
9
10666
by: Robert Schneider | last post by:
Hi to all, I don't understand that: I try to delete a record via JDBC. But I always get the error SQL7008 with the error code 3. It seems that this has something to do with journaling, since the table from which I want to delete has two foreign keys that references two other tables and it is also referenced by another table. But this shouldn't be a problem, since I set the commit mode to none (or *none) at all places where this makes...
3
2102
by: John Rivers | last post by:
Hello, I think this will apply to alot of web applications: users want the ability to delete a record in table x this record is related to records in other tables and those to others in other tables etc. in other words we must use cascade delete to do
13
2867
by: forbes | last post by:
Hi, I have a user that used the Query Wizard to create a query in Access. Now she claims that her master table is missing all the data that was excluded from the query. Can you create anything other than a select query using the Wizard? What do you think happened to her data? I am working remotely until Friday, so I can't get down to her office and check out what she did.
6
3858
by: polocar | last post by:
Hi, I'm writing a program in Visual C# 2005 Professional Edition. This program connects to a SQL Server 2005 database called "Generations" (in which there is only one table, called "Generations"), and it allows the user to add, edit and delete the various records of the table. "Generations" table has the following fields: "IDPerson", NamePerson", "AgePerson" and "IDParent". A record contains the information about a person (his name, his...
3
3426
by: bluez | last post by:
I want to design a webpage where user can search the data from the database and list out the related records. Each of the record got a delete button which allow user to delete the record. Filename : search_student.php <?php include("../user_access/user_access_control.php"); include("../Database/database.php"); $searchStudentControl = new Access_user;
36
5106
by: pearl146 | last post by:
Hi, I have some database files (.MDF, .LDF,...) on the server. When I try to delete them, the warning "Cannot delete file: There has been a sharing violation. The source or destination file may be in use." appears. Since I am new to the environment I don't know where the files come from and where they might be used.
10
2062
by: pythonnoob | last post by:
Hello everyone. New to python as well as this forum, but i must say ive learned a but already reading through some posts. Seems to be a pretty helpful community here. Before i post a question ill give you a little background. I have done programming in the past in Basic, VB, and a little C. I am not much of a programmer, its more of a hobby/curiosity. The following code is not mine, i am trying to modify a template of a mock database....
0
9575
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
10170
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
10014
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
9960
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
9841
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
8840
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
5425
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3534
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2808
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.