How to force Sqlservices.Uninstall to delete old data too? - asp.net

Does I use SqlServices.Uninstall() to uninstall ASP.NET Membership tables and other stuff programmatically from database. But when tables hold old data, it does not work with following error message:
Cannot uninstall the specified feature(s) because the SQL table 'aspnet_Membership' in the database '[DBNAME]' is not empty. You must first remove all rows from the table.
Is there a way to tell SqlServices or any other class in .NET to erase those old data too?

You can use the following that deletes every user from your database.
foreach (MembershipUser user in Membership.GetAllUsers())
Membership.DeleteUser(user.UserName, true);
SqlServices.Uninstall()
Be careful:
The code above executes 1 + N (DELETE Statements) + SQL Statements from the Uninstall method,
where N == Number of Users in the database. So it's not very efficient.
If you want something more efficient you have to write your own Stored Procedure.

Related

SQLite Importer will overwrite my database when I load my application?

I have an Ionic App using SQLite. I don't have any problems with implementation.
The issue is that I need to import an SQL file using SQLitePorter to populate the database with configuration info.
But also, on the same database I have user info, so my question is:
Everytime I start the app, it will import the sql file, fill the database and probably overwrite my user data too? Since it is all on the same base?
I assume that you can always init your table using string queries inside your code. The problem is not that you are importing a .sql file. Right?
According to https://www.sqlitetutorial.net/sqlite-create-table/ it is obvious that you always create a table with [IF NOT EXISTS] switch. Writing a query like :
CREATE TABLE [IF NOT EXISTS] [schema_name].table_name (
column_1 data_type PRIMARY KEY);
you let sqlite to decide if it's going to create a table with the risk to overwrite an existing table. It is supposed that you can trust that sqlite is smart enough, not to overwrite any information especially if you use 'BEGIN TRANSACTION' - 'COMMIT' procedure.
I give my answer assuming that you have imported data and user data in distinct tables, so you can manipulate what you populate and what you don't. Is that right?
What I usually do, is to have a sql file like this:
DROP TABLE configutation_a;
DROP TABLE configutation_b;
CREATE TABLE configutation_a;
INSERT INTO configutation_a (...);
CREATE TABLE configutation_b;
INSERT INTO configutation_b (...);
CREATE TABLE IF NOT EXIST user_data (...);
This means that every time the app starts, I am updating with the configuration data I have at that time (that's is why we use http.get to get any configuration file from a remote repo in the future) and create user data only if user_data table is not there (hopefully initial start).
Conclusion: It's always a good practice, in my opinion, to trust a database product 100% and abstractly let it do any transaction that might give you some risk if you implemented your self in your code; since it gives a tool for that.For example, the keyword [if not exists], is always safer than implementing a table checker your self.
I hope that helps.
PS: In case you refer in create database procedure, SQLite, connects to a database file and it doesn't exist, it creates it. For someone comfortable in sqlite command line, when you type
sqlite3 /home/user/db/configuration.db will connect you with this db and if the file is not there, it will create it.

Object Not Found when ASP.NET app is trying to access a stored procedure using global temp table

I have an ASP.NET application accessing an Oracle 12 database. I have written a stored procedure using a global temp table to help.
The global temp table is created with 'ON COMMIT DELETE ROWS'.
Basically the stored procedure does the following:
Get some data. Add a char column. Insert to the global temp table.
Use a key (in each row) to call another stored procedure.
The stored procedure returns a 'Y' or 'N'.
The return value will be updated to the corresponding row in the global temp table.
Once done all rows, return like this: open refcursor for select * from global_temp_table
I test the stored procedure. It works fine.
I then in my ASP.NET project, add another function in web service (asmx). I try to involve, but it says: "object not found".
I have read some posts and say change to 'ON COMMIT PRESERVE ROWS'. I don't want to leave data around. Is there any way to fix this issue? I have searched for sometime already.
[Edit] Other team mates have added many other stored procedures in database. They don't have to grant any right to it. The stored procedures are correctly executed when calling from web service. Of course, their sp do not use global temp table.
I have searched the web. I have seen similar posts out there. Calling an Oracle stored procedure using global temp table within ASP.NET will show the same error - object not found.
Anyway, I have found a way round. That is to use the WITH clause. Here is a reference link: https://oracle-base.com/articles/misc/with-clause
[/Edit]

Dynamic Sql VS Temporary Tables

I have a stored procedure, in which temporary tables "on delete preserve rows" are created dynamically and data was inserted, when i try to execute any other dynamic sql statement, data in the temporary tables are deleted. But I need a data for further process.
Can any one tell me why data is losing, and what is the solution for this.
Thank you.
Three possible reasons for this:
There is an explicit commit.
There is an implicit commit (a DDL statement, typically).
You are are closing the session and starting a new one.
If you cannot avoid these then you'll have to create a permanent table.

Knowing web sql database is already created or not

Is there any way to know wheather new database is created or connected to existing one, when calling window.openDatabase() ? I think i have to create tables only when newly created.
Don't worry about the openDatabase() command. Instead modify your SQL so it only creates the tables if they don't exist like so:
CREATE TABLE IF NOT EXISTS DEMO (id unique, data)

Handling Constraint SqlException in Asp.net

Suppose I have a user table that creates strong relationships (Enforce Foreign Key Constraint) with many additional tables. Such orders table ..
If we try to delete a user with some orders then SqlException will arise.. How can I catch this exception and treat it properly?
Is this strategy at all?
1) first try the delete action if an exception Occur handel it?
2) Or maybe before the delete action using code adapted to ensure that offspring records throughout the database and alert according to .. This piece of work ...
So how to do it?
--Edit:
The goal is not to delete the records from the db! the goal is to inform the user that this record has referencing records. do i need to let sql to execute the delete command and try to catch SqlException? And if so, how to detect that is REFERENCE constraint SqlException?
Or - should I need to write some code that will detect if there are referencing records before the delete command. The last approach give me more but its a lot of pain to implement this kind of verification to each entity..
Thanks
Do you even really want to actually delete User records? Instead I'd suggest having a "deleted" flag in your database, so when you "delete" a user through the UI, all it does is update that record to set the flag to 1. After all, you wouldn't want to delete users that had orders etc.
Then, you just need to support this flag in the appropriate areas (i.e. don't show "deleted" users in the UI).
Edit:
"...but just for the concept, assume that i do want delete the user how do i do that?"
You'd need to delete the records from the other tables that reference that user first, before deleting the user record (i.e. delete the referencing records first then delete the referenced records). But to me that doesn't make sense as you would be deleting e.g. order data.
Edit 2:
"And if so, how to detect that is REFERENCE constraint SqlException?"
To detect this specific error, you can just check the SqlException.Number - I think for this error, you need to check for 547 (this is the error number on SQL 2005). Alternatively, if using SQL 2005 and above, you could handle this error entirely within SQL using the TRY...CATCH support:
BEGIN TRY
DELETE FROM User WHERE UserId = #MyUserId
END TRY
BEGIN CATCH
IF (ERROR_NUMBER() = 547)
BEGIN
-- Foreign key constraint violation. Handle as you wish
END
END CATCH
However, I'd personally perform a pre-check like you suggested though, to save the exception. It's easily done using an EXISTS check like this:
IF NOT EXISTS(SELECT * FROM [Orders] WHERE UserId=#YourUserId)
BEGIN
-- User is not referenced
END
If there are more tables that reference a User, then you'd need to also include those in the check.

Resources