Delete all tables in sql database

Hi all,

Recently I encountered a bizarre request, I had to delete all the tables from an Sql database.
The reason is even more bizarre. The client had a system auto creating them without making sure if they exist first. Ao, I had to write some code to first delete all the tables.

I thought maybe other people like me may encounter the same issue, so I wrote some code for it.

The code can either print out the Drop statements for each table or execute it.

Good luck!

Tested on sql server 2005

   1: DECLARE @dropSql nvarchar(1000)

   2: DECLARE DropSequence CURSOR LOCAL FAST_FORWARD

   3:

   4: FOR

   5:     SELECT

   6:         N'DROP TABLE ' + QUOTENAME(TABLE_SCHEMA) + N'.' + QUOTENAME(TABLE_NAME)

   7:     FROM

   8:         INFORMATION_SCHEMA.TABLES

   9:     WHERE

  10:         TABLE_TYPE = 'BASE TABLE'

  11:             AND

  12:         OBJECTPROPERTY(OBJECT_ID(QUOTENAME(TABLE_SCHEMA) + N'.' + QUOTENAME(TABLE_NAME)), 'IsMSShipped') = 0

  13:

  14:     OPEN DropSequence

  15:     WHILE 1 = 1

  16:         BEGIN

  17:             FETCH NEXT FROM DropSequence INTO @dropSql

  18:

  19:                 IF @@FETCH_STATUS <> 0 BREAK

  20:                     RAISERROR (@dropSql , 0, 1) WITH NOWAIT

  21:

  22:             --EXEC(@dropSql )

  23:             PRINT @dropSql

  24:         END

  25:

  26: CLOSE DropSequence

  27: DEALLOCATE DropSequence

Using jQuery tree for database data

Hi,

This blog post is actually to answer a really nice guy from a forum (.Net) I run in Hebrew, but I thought it will be really nice to write about it in general.

I get lots of questions regarding the concept of using jQuery (in general) with .net and database data.

I actually blame Microsoft for having the tutorials in their website teaching only SqlDataSource, which I never used and recommend that you don’t use too.

So, the majority of the questions I get are not at all about code. They are all about concept: How to communicate with data, How to retrieve data and more.

Ok, so this tutorial is all about jQuery, LINQ to Sql, and the jQuery tree. The only difference is that I’m not going to display files in folders, but pages from a Sql table, every page has a “ParentID” fields, so it will be very easy to understand.
This tutorial is also to clarify you can use the tree for any relational database or file data.

So, Let’s Start!

First, let’s go and download the 2 packages.
jQuery
jQuery File Tree

After downloading the 2 packages, we’ll open up a new website in Visual Studio.
Our files should look like this:
2009-05-22_1726

What do we have here?

Well, we have all the css from the jQuery file tree library, we have the Images also from this library, we have the js files from: jQuery, jQuery file tree and another js file which we created for our custom functions.

Alright, now let’s create our page:

2009-05-22_1728

We have all the script file and css files in our page. Also, we wrote a div called “myFirstTree” in our page, this div will hold our tree.

What else do we need?

Well, like I said, we are displaying database data here, so let’s create a very simple database with one table, also we will create a LINQ mapping file for it.
B.T.W, you can use any kind of data access, you can use nHibernate, or whatever you may find comfortable for your use.
I use linq for this example, don’t ask why, just felt like it :-)

2009-05-22_1734

So, we have these fields:
pageID – Identity field
pageName
pageParentID

Let’s create our data, just randomly fill our table with some data, and then we can go back and retrieve it from the client side using some jQuery.

2009-05-22_1738

So, we have our data. I don’t think i need to further explain this no? :-)

Now, let’s write some js code to retrieve the data from the server for us.

Our js code should look like this:

$(document).ready(function() {
	$('#myFirstTree').fileTree({
        root: '0',
        script: 'jQueryTreeHandler.ashx',
        multiFolder: true,
        loadMessage: 'Tree loading...'
    		}, function(file) {
        		alert(file);
    });
});

Let’s explain this code a bit shall we?

First, we chose our pre crated div “myFirstTree”. Then we initialized the fileTree object with these parameters:

root: the first parameter that will be passed to our Handler

script: our path to the handler

multiFolder: whether our tree will be multi folder or not, when this is false, clicking each folder will collapse all the other folders in the tree.

loadingMessage: this message will be displayed when data is being retrieved from the server.

fileFunction: this function will be fired when a file from the tree is selected.

All pretty straight forward up until now, let’s go on and create our handler:

The parameter we know is passed in is called “dir”, so that is what we will use here.

So, this is what your handler code should look like:

<%@ WebHandler Language="C#" Class="jQueryTreeHandler" %>
using System;
using System.Web;
using System.Linq;

public class jQueryTreeHandler : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        int parentID = Convert.ToInt32(context.Request.Form["dir"]);

        string list = "";
        jQueryTreeDataContext db = new jQueryTreeDataContext();
        var pages = from p in db.Pages
                    where p.pageParentID == parentID
                    select p;

        foreach (Page item in pages)
        {
            list += String.Format("<li class=\"directory collapsed\"><a href=\"#\" rel=\"{0}\">{1}</a></li>", item.pageID.ToString(), item.pageName);
        }

        context.Response.Write(String.Format("<ul class=\"jqueryFileTree\" style=\"display: none;\">{0}</ul>", list));
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}

Comment: I didn’t take care of a situation where a row has no children, it wasn’t necessary for my example. However, in real life code you should, and if a row does not have any children, you shouldn’t display a folder, but a file instead.

And this is how our code looks like in the final result (Html output):

2009-05-22_1840

Comment: I had to tweak the jQuery file tree code in order for it to work with database data. Normally it has a regullar expression validating the “rel” attribute actually contains data with “/”, that needed to be changes.

I uploaded the file to here for you, so download away…
[download id="2"]

What is Kenso…?

Hi all,

A lot of people are reading my Web Development blog or following me on twitter have been asking me, what is Kenso? What does that word mean?
Well, I’ll tell you what it isn’t, It’s not my last name :-)

Kenso is a word in Japanese meaning “economical and simple”.

In case you don’t believe me, here’s a screenshot from a dictionary online.

2009-05-10_2124

That’s it :-)

Interview Questions (flex) – What do you say?

Hi all,

as you know, I work as a full time freelancer in RIA and web development.

Once in a while like every service provider I have to meet with new clients to maybe start working with them on new projects, or work with the company as a consultant.

Usually when I go to this kind of a meeting, my portfolio speaks for itself and we get down to business in no time.

Today was different, I was in such a meeting and the guy sitting with me asked me to take a short test (3 questions test).

It seemed odd, but I didn’t refuse and took it.

Well, I passed :-) he said with flying colors (he’s words), but I wanted to see what you guys think and to see your answers.

1st Question

Describe all the ways you can send paramaters into a flash object.

2nd Question

You have an array of zeros and ones 0/1/1/1/1/0/1/0/1/0/1/1/1/0.

Write a function that sorts this array without using the sort function (:-))

3rd Question

You have an XML holding commercial data.
Each XmlNode holds the t1 (time to start) and t2 (time to end).

Write a function that takes the commercials and displays them on the canvas.
No need to parse the XML.

Time: 15 minutes

I will publish my answers in a different post in a few days.
You can either comment or send me the answer via email.