Copying database schema and data using C sharp

To copy the database structure and data content from one database to another, you can use C# codes as follows:

class Program
{
static void Main(string[] args)
{
Console.WriteLine(“Attempting connection to database…”);
// Connect to Server
ServerConnection _con = new ServerConnection();

Server server = new Server(“VM0019\\SQLEXPRESS”);

// Get the Database to Transfer
Database db = server.Databases[“gices”];

// Setup transfer
// I want to copy all objects
// both Data and Schema
Transfer t = new Transfer(db);
t.CopyAllObjects = true;
t.DropDestinationObjectsFirst = true;
t.CopySchema = true;
t.CopyData = true;
t.DestinationServer = “VM0019\\SQLEXPRESS”;
t.DestinationDatabase = “testcopy”;
t.Options.IncludeIfNotExists = true;

// Transfer Schema and Data
t.TransferData();

// Kill It
server = null;
Console.WriteLine(“Closing database connection now…Program terminated”);
}
}

You need to first create a Console Application project in Visual Studio and add reference to a couple of libraries before attempting to run the codes.

Microsoft guide to transferring data:

Transfer Schema and Data from One Database to Another

Mass drop of Stored Procedures in SQL Server

Sometimes you need to mass drop stored procedures in a database. Well i needed a convenient way to do delete all stored procs starting with the word ‘asp’ on my local DB. The following codes did the job really well:


select Name + ',' from sys.objects where Type = 'P' and name like '%asp%'

This will return you a list of sprocs separated by a comma which you can copy and paste into the next code, as follows:


drop procedure aspnet_Setup_RestorePermissions,
aspnet_Setup_RemoveAllRoleMembers,
aspnet_WebEvent_LogEvent,
aspnet_Personalization_GetApplicationId,
aspnet_Users_DeleteUser,
aspnet_AnyDataInTables,
aspnet_Applications_CreateApplication,
aspnet_Membership_GetUserByName,
aspnet_Membership_UpdateUserInfo

So instead of dropping each stored procedure manually, you can drop many stored procedures at once using this technique.

Introduction to Windows Application Programming

I’m not too much into Windows Application Programming because i tend to concentrate on building applications for the Web. However the other day i wanted to do a little application for Windows and i got stuck a bit. I usually write a small app with just 1 form but when your app contains more than one Form, then you really need to think how you are going to design it.

First things first – you can have a main Form which loads subsequent forms through an MDIForm interface. This means that you choose one form to be the container (MDIParent) and the other to be the Form you want to load (MDIChild) . I personally do not like the hassle of having to do this.

What do i do then? Well, i tend to have just one Form in my application which contains a panel. Instead of creating many other Forms, i create User Controls which i load on the main panel of my ONLY Form. All you need to make sure is that you clear all the controls of your main panel before loading a User Control on it.

pnlMain.Controls.Clear();

MyUserControl _muc = new MyUserControl();

pnlMain.Controls.Add(_muc);

If you have multiple Forms in your application though and you need to nominate one of them to be the default Form so that when you start your application, this particular Form is loaded, you need to edit your program.cs file as follows:

// we want to make Form2 load when application starts instead of Form1

Application.Run(new Form2());

One other thing i’ve noticed about Windows Programming is that if you wanted to have a separate config section in your application, it is not so easily doable. Say for example, i wanted to have an external config file for my connection strings instead of dumping them into the App.config file. If i store the external config file in a subfolder like Config/connections.config and reference it in my App.config as follows:

<connectionStrings configSource=”Config\connections.config”/>

The connection string will not be picked up by the program because it would not find the external file. When you compile your application, the App.config will be re-created as ProjectName.exe.config in your Debug/Release folder. Suppose my project is called Gices, i will have a Gices.exe.config file in my Debug folder which will reference a path to the external connections.config file and because my actual Config/connections.config is two level up, the program will not find it. Now you can say that if we write the path as ../../Config/Config.connections, it will work out fine. Well that’s not true because all path in the App.config or Web.config needs to be both relative and referencing a path in either the same directory or a sub-directory BUT not going any levels up. Therefore you will need to create the Config/connections.config in your Debug/Release folder for your application for it to work.

When you deploy your application now, you will need to have the config files attached. If you need to attach/include any files or folders for your application, the best thing to do is create a Setup Project for your application and add the Project Output to the setup. Once this is done, you can right click on Application Folder [located under File System on Target Machine] and select Add, followed by either folder/file and add it. This will ensure that your folder/file is included when somebody runs the setup for your application.

Submitting form data to a webpage

There are a number of reasons why you would want to send form data to a webpage but i was just trying to automate the process of logging into a website. The website itself presents a form where you need to enter a valid username and password.

The following links will help you to build a small application in C# to do just that:

Strikefish

Sys-Con

Note that the method to send post data to an ASP.NET is quite different because you need to have a consistent viewstate information. Also, if you are trying to send data to an ASP.NET page, you will have to set the EnableEventValidation attribute on the page you are requesting to false as follows:

<%@ Page Language=”C#” EnableEventValidation=”false” %>

If you don’t do that, you will get an error saying that the postback data or callback is invalid because the EnableEventValidation checks that the request was made by a control on the page and since in this case it will be an external request, it will not validate and fail altogether.

Another thing i’ve noticed is that if you use set objRequest.CookieContainer = new CookieContainer() in your first request (where you are trying to log), your subsequent requests for pages which require authentication will fail. You will have to declare a global CookieContainer as follows:

CookieContainer _cookies = new CookieContainer();

objRequest.CookieContainer = _cookies;

When you get the ResponseStream back, it will automatically populate the _cookies variable for you. Therefore you can use it for subsequent web requests and it will authenticate properly.

The facility to perform such automated tasks is unbelievable and can be used for a lot of useful things – just think about it and you’ll see!

C# coding standards

The following documents show the best practice for coding in ASP.NET and C#. There’s also a Web Deployment document to show how to deploy projects without overwrite the web.config file and deleting test pages and SubVersion files.

dotnetcodingstandards.doc

web-deployment.doc

How to set the default page for a website in the MSI?

After adding a web setup project to the solution, you will need to right click on the setup project and choose ‘File System’ (File System Editor). If you then right click on the ‘Web Application Project’ and view the propertie, you will be able to set the default page for the website and other properties as well. Just enter the name for the default page you want (search.aspx for eg) in the DefaultDocument row and your default page will be set in IIS.

Moved to wordpress

Being a Microsoft guy, i wanted to have a blogging platform on windows. So i initially wrote a small application which would allow me to write simple stuffs. As i started using the app though, i found that i needed more functionality built into it but since i didn’t have any time to do it myself, i deciced to search for a blogging engine that would have the facilities i was looking for and two, be able to run on a windows platform. I thought about WordPress but because it’s written in PHP i decided to ditch it and go for something else.

I searched everywhere on the internet for a free blogging engine in .net to run on windows but there were very few available. I tried one of them and it was really really basic, so i opted for Subtext. Subtext is really good but it lacks many functionalities. Like i was trying to edit the date on one of my posts the other day and i couldn’t do that. The thing is sometimes we want to write something but backdate it so that it reflects the appropriate day the particular event happened. I mean for me blogging is a way to log things happening in my life and stuffs which i want to remember. However you don’t always have the chance to write about something on the same day that it happened! The other thing is that i wanted my permalinks to be extensionless but Subtext appends the “.aspx” extension to all posts which does not make it really portable (if in the future you want to move to another blogging platform)

Subtext is still in its infancy and i’m sure it will do well in the future. At the moment however, i need something more mature so i decided to go for WordPress (yeh, i know, i should have done it before!). With the large number of themes available to you and the different plugins for you to use, you can’t go wrong. And there’s more support for that software too. Therefore i’ve moved everything (i had less than 50 posts btw) to WordPress. I love the functionality it offers and i’m glad with it 🙂