ask database on deleting if some row references another row (= database.Can_I_delete_some_table_row?)

Hello,

is there way to ask database if I can delete some table row?

I have tables with foreign key constraints and I need "try delete" some row in this table. But there is possibility
that the row is referenced by another table, so it throws an exception..

I now that I can use in my .net code try, catch statement to catch unpossibility of execution sql statement...
but It makes me feel that this is "hard" way (throwing exception, catching..) -- maybe here is another more efficient way..Wink

I need do st.like that:

IDbConnection connection = Connection;
// ask database if I can execute the statement (=can I delete table row?...)
// if yes ->

    CreateCommand(statement, connection).ExecuteNonQuery();
connection.Close();

How can I implement bold part of code?

Thanks,
VIPI


vipi
0
vipi
2/21/2007 12:37:21 PM
asp.net.object-datasource 16182 articles. 0 followers. Follow

3 Replies
816 Views

Similar Articles

[PageSpeed] 22

VIPI

One option is add the following to your delete stored procedure

IF EXISTS(SELECT * FROM theReferenceTable)

BEGIN

     execute the delete statement

END

 

0
justSomeone
2/21/2007 12:47:07 PM

Hi VIPI,

In SQL Server, we can check for dependencies for certain objects like Stored procedures, Tables and Views with sp_depends. However, We cannot check for a certain row for dependecies.

Fore more information for sp_depends, please check

http://msdn2.microsoft.com/en-us/library/ms345449.aspx
http://msdn2.microsoft.com/en-us/library/ms189487.aspx


Sincerely,
Kevin Yu
Microsoft Online Community Support

Please remember to click “Mark as Answer” on the post that helps you, and to click “Mark as Not Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
0
Kevin
2/23/2007 5:57:27 AM

Thanks for your replies,

we using our own data access layer which use object relational map pattern.
The problem comes, when we need implement inheritance in our classes hirearchy from business layer and save it (update, delete etc..) properly to database
-- when we deleting child, we need to delete parent, but it can be referenced by another child...

So finally our solutions consist of try delete parent which is implmented in DAL by try, catch statement.. ;/

vipi.


vipi
0
vipi
2/23/2007 8:08:54 AM
Reply:

Similar Artilces:

DataSet rows being deleted, but after the update , the sql database is not updated. The delete rows still in the database.
 Stepping thru the code with the debugger shows the dataset rows being deleted.   After executing the code, and getting to the page presentation. Then I stop the debug and start the page creation process again ( Page_Load ).    The database still has the original deleted dataset rows. Adding rows works, then updating works fine, but deleting rows, does not seem to work.   The dataset is configured to send the DataSet updates to the database. Use the standard wizard to create the dataSet.     cDependChildTA.Fill(cDependChildDs._ClientDependentC...

why the size of the database become bigger when I deleted all rows of the database
I used sqlanywhere 5.5.0.4 database to store some pictures in a long binary field , the database file became very big soon , when I want to shrink the size of the database file , I deleted all rows , however the database file became bigger (In this database I didn't use a log file),who can tell me why ? Thanks! Bo, Deleted rows are not removed until you rebuild the database. I don't know why the database got bigger, but I'm sure that if you rebuild the database, it will physically remove the deleted rows. Richard Bo Song wrote in message +ADw-3733CA2D.7D...

Coping Database objects from one database to another blank database.
I want to create a duplicate database   in sql 2000 using asp.net from a webform I created a database using CREATE DATABASE ....... But how to copy tables, views, stored procedures to newly created database from old using asp.net from webform Is there any another method to create a duplicate database with another name from existing database on same server ? yes you can do have another DB with different name and that has everything the same .....!!!Fadil Alnassar www.fadilalnassar.com | FREE Nodil Tab Controlhttp://www.mefranchising.com One way it can be done is with...

Using a row selection in a gridview to create database rows for another gridview
Hi there, I have 2 gridviews on a form, the first displays some template rows from a database table.  The second displays some user specific rows (with additional data added to the template info) from the same table. They both work fine for displaying the info.  Now what I want to do, is allow users to 'select' a template row on the first gridview, and have that action automatically: create a new user-specific row, with added fields from profile data etc. insert it into the table then refresh the second gridview in order to show the new added row. C...

How to delete duplicate rows from remote database when they have successfully replicated back to the consolidated database?
Hello, I have a SQL remote replication setup with a central consolidated database and many remote databases. For simplicity sake I have a table called period_sale (sale_code, period_number, sale_amount, branch_id) which participates in replication and the publication to which this table belongs has a SUBSCRIBE BY branch_id. Also the subscription that has been created for each of the remote branch to this publication has a subscription value of branch_id. In other words the subscription for branch_id of HAR001 was created using CREATE SUBSCRIPTION TO pub_dailytrans ('HAR0...

Delete rows in SQL database using vb.net
Pls help: I have two tables: Tb and seller In the seller table: these are the following fields:ContactNo,StudentName,AdminNo,Serialcode In the tb table : these are the fields : Title, Author, Serialcode ***Each Title has its own unique Serialcode I wish to delete ContactNo, StudentName, AdminNo, Serialcode when i choose the particular Title. I not sure of the coding to delete rows....Pls help Coding: Dim objConnection As New SqlConnection(strConnection) Dim strSQL As String = "DELETE Seller.AminNo, Seller.ContactNo, Seller.StudentName, Seller.Serialcode FROM Sel...

(Fwd) How to loop through a database, row by row, and select and update one row at a time
----- Forwarded message from Troy Mulder <mulderta@gmail.com> ----- Date: Mon, 12 Apr 2010 17:48:37 -0400 From: Troy Mulder <mulderta@gmail.com> To: Tim.Bunce@pobox.com Subject: How to loop through a database, row by row, and select and update one row at a time Hello Tim (is it Dr. Bunce?), My name is Troy Mulder, and I am trying to get a perl script to interface with a PostgreSQL database. I am trying to step through each row of the database, and read one column of the row, and update another column of the row. When I follow the online tutorial a...

how to delete a row from database
hello i am using sqldatasource 2005: i am using this <sqldatasource id="a"> deletecommand=delete from table where rec_key=@rec_key <deleteparameters> <asp: sessionparameter sessionfield="session_rec_key" type=string> this works fine but if i use this deletecommand=delete from table where rec_key=@rec_key and name=@name <deleteparameters> <asp: sessionparameter sessionfield="session_rec_key" fieldname="rec_key" type=string> <asp: sessionparameter sessionfield="session_name" fieldname="...

How do we traverse through the records row-by-row in a database?
Hi guys, I used a CurrencyManager in a windows form to traverse row-by-row on the records in a database. Can anyone help me out in using a similar feature in asp.net to traverse through the records row by row. Is there any in-built feature? I tried using a dataset to traverse through but I think it might be better to use an in-built feature as te code used in it might be optimised. ThanksKarthik MuthuswamyProgrammer AnalystChennai Did you try using a gridview??Click on 'Mark as Answer' if this post is helpful.-- Randy If you want to traverse row by row in the database itse...

How to get data from database row by row
I try to get data from a database row by row, I am using DataReader, the return result is not I expected, don't know how to solve it. Here is my sample code: --------------------------------------------Dim commandText As String = "select * from customer "Dim con As New OleDbConnection("Data Source=c:\company.mdb;Persist Security Info=False; Provider=Microsoft.Jet.OLEDB.4.0;")Dim Cmd As New OleDbCommand(commandText, con)Dim reader As OleDbDataReader = Cmd.ExecuteReader()             Using con       &nbs...

Delete (Hide) Row from Gridview without deleting from DataBase
I am populating some data in a GridView.I added a column with a checkbox.Now I want to generate a sort of print-preview...When Ich check some buttons and hit my printpriview button. I only want these (selected rows displayed).How can I do this?The checkbox thing is already working What I basically need is something to delete data temporaly from a GridView (not Database).I tryed GridViewRow.Dispose() which worked but then the paging was still implementet and the user sees still all pages =(I have tryed GridViewRow.Enabled = false which only turned the colors =(I have used GridViewRow.Visible ...

Deleting rows in access database
Hey, I think this question is bugging not only me, but I have not found anything related to it so..listen. The "DELETE" function doenst actually deletes the records, it only puts the property "deleted" on the record, so.....my database file is about 15MB, eventhough I dont have any records on it..... Does anybody know how can I delete those deleted records. I know in FoxPro there is a "PACK" function that cleans the db. Thank you for your help. =] You need to do a "Compact and Repair". Others have posted code for this. Howeve...

Database SPACE after Delete Rows
After deletion of many rows in 2 tables, the size of my database (SqlAnyWhere Studio 6.0) is the same. How can i schrink the database ? The free space will be re-used for new data so it is not wasted. If you really want to shrink the size of the file you must dbunload, delete and recreate the database with dbinit, and then run reload.sql via dbisql. Breck On Fri, 22 Feb 2002 10:08:06 -0500, Didier wrote: >After deletion of many rows in 2 tables, the size of my database >(SqlAnyWhere Studio 6.0) is the same. >How can i schrink the database ? > bcarter@ris...

deleteing all rows from remote database
"Jim Egan" <dontspam.dbaguru@eganomics.com> wrote in message news:MPG.15e714c547645aa198b5cd@forums.sybase.com... > Pierre_Rene wrote... > > Is there an easy way to instruct mobilink to delete all the rows in a table > > in the remote database. I know there is a download delete cursor I could > > use but it will be very hard to select and isolate the recently uploaded > > rows. What I am looking for is some kind of "wipe after upload" type event > > where no select has to be done. I know I want ALL rows removed from this ...

Web resources about - ask database on deleting if some row references another row (= database.Can_I_delete_some_table_row?) - asp.net.object-datasource

Resources last updated: 12/20/2015 6:46:45 AM