|
Hello !
I have a vb.net that use Entity Framework and SQL server database.
On your opinion , is there any way to mimic the role of Windows's Recycle Bin on this application ?
so anything I can do with file on recycle bin , I want to do with records in database ( I mean Delete , Restore , empty recycle bin...).
If yes , what would be the best way ?
Thank you !
|
|
|
|
|
That's relatively easy: You would have to have a field like "Deleted" in every entity which gets set to true when the user deletes an entity first. In all your queries you add a condition "where Deleted=false" unless the user explicitly wants to see the deleted entities. And eventually a user can "empty the recycle bin" where you actually delete the entities which have Deleted=true with the EF method ObjectContext.DeleteObject(entity) (or with direct SQL). Alternatively to "Deleted" being a boolean field you could make it a nullable DateTime field like "DeletedDate" where null means that it's not deleted and otherwise the date of deletion.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Hello !
It seems very easy reading your explanation.
But I don't understand very well the "Empty Recycle bin" idea.
Could you write some code to illustrate this ( I mean , the model may have 100 entities. How can I delete all the object from all entities that have "Deleted=True".
( I'm using DBContext with Database First ).
Thank you !
|
|
|
|
|
There's no way to delete all entities (with "Deleted=true") regardless of their type with a single "command" (unless you create a stored procedure for that in which case the "trouble" would just be moved there).
Options:
1) Via the context (pseudo code):
entities1 = context.Entity1.Where(e => e.Deleted).ToList()
foreach(entity in entities1)
context.DeleteObject(entity)
context.SaveChanges()
Not beautiful because of code repetition.
2) Via the context with reflection:
find all entity types in the context
foreach found entity type
dynamically build a query like above (1) and run the query
foreach found entity
context.DeleteObject(entity)
context.SaveChanges()
No code repetition but considerably more effort.
3) Via direct SQL:
You would have to have a list of all table names in the database (could be obtained via the information schema).
foreach(tableName in tableNames)
context.DataBase.ExecuteSqlCommand("DELETE FROM " + tableName + " WHERE Deleted = True")
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Use your tools to generate code. You have SQL, it can generate a statement.
It sounds as if you are using Sql Server; that means you can query which tables exist in your database. That means you can write a SQL-query that constructs a SQL statement for each entity.
SELECT 'DELETE FROM ' + [TableName] + ' WHERE Deleted = 1' FROM SYS.TABLES Copy and paste the result into Sql Server Management Studio, wrap it in a transaction, and execute*.
*) might wanna check the syntax, did not verify it in a IDE.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I have problem with this case :
A Parent Record has the field Deleted=True
All its childs have Deleted=True.
But the sql code produce error , can't delete the Parent because has childs.
|
|
|
|
|
Delete the children before you delete the parent, or use cascading deletes.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
The problem is how to create the sql expression that take care about this detail ?
For example how should I modify your suggested code ?
The idea is to do the deletion with a single query.
(I don't want to use the cascade delete)
|
|
|
|
|
DELETE FROM [ORDERDETAILS] WHERE [DELETE] IS NOT NULL
DELETE FROM [ORDERS] WHERE [Delete] IS NOT NULL
satc wrote: The idea is to do the deletion with a single query. That is technically not possible. You can execute them as a single command that success as fails or a whole. The code would obivously need to be in a transaction.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Ok , but if the database has 100+ tables , if I get one by one the tables master and Childs as you suggest , do you think is a good solution ?
|
|
|
|
|
I am not going to repeat my answer.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi. I have to create an console application, that grabs an xml file via a network share (unc-path)
With the following function I read the xml-file.
Dim filePath As String = "C:\temp\demo.xml"
dsJobStatus.ReadXml(filePath)
dvJobStatus = New DataView(dsJobStatus.Tables(0))
Works fine if the user that runs the exe has access rights to that fileshare; but somtimes I do have to pass username / Password as parameter. Any idea, how I can access the unc-path with passing access credentials?
regards
|
|
|
|
|
net use[^]
Execute the command on a console before executing your code or from your code and wait until it exits.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Appologize if this is childish question but i am in too much confusion.
I learnt first language vb6.0 in 2008 then i was student later and i continue it many years then 2 years ago i shifted to vb.net but in markete there is demand of c#. Many peoples told that c# is better than vb.net that,s why there is no value of vb.net in markete. I still can,t find any job to work in vb.net while i know vb well. Then i learnt C++ and i learnt it well and then i moved to c# and now i can code in c#.
But problem is that i can,t code in c# same as in vb.net. I mean i always prefer vb. In vb i can solve any problem. but in c# i have to search from start and i don,t know why i like vb.net too much. In other words i don,t want to leave vb but markete always force to work in c# and foget vb. If vb is too bad in markete then why microsoft still supports it. Microsoft must close vb because they can,t give good markete to vb. Due to this reasons vb only developers are still poor in markete. They can switch to c# easly but their hearts shall always stick with vb.
I know many languages like vb,c#,java,asp.net,php,mysql,sql etc but my heart always stick with vb.
|
|
|
|
|
|
Message Closed
modified 5-Jan-17 22:39pm.
|
|
|
|
|
But javascript is only scripting language. It can,t create desktop or android applications. I mean it is not equal to vb.net because in vb.net we can develop in desktop,web and cell phone also.
|
|
|
|
|
Hello !
I have a vb.net program , where I want to save on a database several things , and the actual time.
The problem is that the local pc clock may have problems.
Is there any way that if internet connection is present , to get the real date and time ( for my time zone ) from internet and to save to a variable ?
Thank you !
|
|
|
|
|
A Google search will find all you need to know about NTP servers.
|
|
|
|
|
In a VB.net 2010 desktop application, reports obtain their values to execute by users selecting values from the various vb.net controls. In 2 columns where there are date pickers, the default value of the date pickers is to use today's date.
The application passes values from the controls using the following code:
Private Sub dtEnddate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtEnddate.ValueChanged
Variables.g_strEndDate = dtEnddate.Text
End Sub
Private Sub dtEnddate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtEnddate.Click
Variables.g_strEndDate = dtEnddate.Text
End Sub
The problem is if the user keeps todays date, and does not change the date, the variables.g_strEndDate never gets a value. These variables I am referring to are in a Public Class Variables and the variable is define as Public Shared g_strEndDate As String.
Thus can you tell me the best way to give this column default values of today's date? What code would you use and where would you place the code?
|
|
|
|
|
classy_dog wrote: Thus can you tell me the best way to give this column default values of today's
date? You could initialize it with a value when the application starts.
classy_dog wrote: What code would you use I would recommend removing the global variable altogether. If it is a dialog, then the values should be returned upon closing the dialog and passed to the method that needs them.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Is there some reason that you cannot simply give the variables default values when you declare them?
You can lead a developer to CodeProject, but you can't make them think.
The Theory of Gravity was invented for the sole purpose of distracting you from investigating the scientific fact that the Earth sucks.
|
|
|
|
|
Wrong approach!
Store dates as dates, not string! String represenation may differ in different localization/culture.
Always initialize variables! It can save you from stress
|
|
|
|
|
In a VB.NET 2010 desktop application, I am using the following new logic to load values into a combo box.
Public dirAccessFiles As String() = Directory.GetFiles("H:\FilesTest", "*.accdb")
Try
Dim dir As String
For Each dir In dirAccessFiles
cboAccessFile.Items.Add(Path.GetFileNameWithoutExtension(dir))
Next
Catch except As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
The user wants the file names to be in descending order in the combo box. Thus, can you tell me how to modify the code list above and/or would you show me code on how I can load file names into the combo box in descending order?
|
|
|
|
|
That means you need to set the Sorted property to False , and sort the names manually before adding them to the Combo.
|
|
|
|