A day with .Net

My day to day experince in .net

Attaching .mdf file to sql server 2000 using SQLDMO.DLL

Posted by vivekcek on July 24, 2009

Ya in the application for my shop that i described in the previous post,actually my client was my uncle in oman,the application was for his elctronic shop there,I helped him to install SQL SERVER 2000 via chatting on his PC,But he dont know attching database restoring operations.So i need to attch the databse associated with my application when it first time load after installtion.Ya there are some methods to install and attch database using the setup program and sql expree,i will explained it later.So now come to topic.

You can download SQLDMO.DLL freely by some google serch then add a refrence to this dll
Then the UI and code is explained below.
Be free to ask ny doubts related it

I n this after attching the database to server i create an XML file and stores the SQLSERVER connection information for future use

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SQLDMO;
using System.Xml;

namespace eStock
    public partial class frmAttachDatabase : Form
        private xmlop XmlObj;
        // Create an SQLDMO application 
        SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
        // Create an Server, which resembles to your actual server
        SQLDMO.SQLServer srv = new SQLDMO.SQLServerClass(); 
        public frmAttachDatabase()

        private void frmAttachDatabase_Load(object sender, EventArgs e)

        private void cmdAttach_Click(object sender, EventArgs e)
            if (txtDatabase.Text != string.Empty && txtServer.Text != string.Empty && txtUid.Text != string.Empty)
                    srv.Connect(this.txtServer.Text, this.txtUid.Text, this.txtPwd.Text); 
                    srv.AttachDB(txtDatabase.Text.Trim(), "[" +System.Windows.Forms.Application.StartupPath + @"\eStock.mdf]");
                    XmlObj = new xmlop();
                    ConString.cons = XmlObj.GetConString();

                catch(Exception ex)

                MessageBox.Show("Server Name,User Name,Database Name are required");

        private void CreateConnectionString()

            XmlTextWriter xmlwritr = new XmlTextWriter("config.xml", Encoding.ASCII);
            xmlwritr.Formatting = Formatting.Indented;
            xmlwritr.WriteComment("Making any cahnges to this file cause application error");
            xmlwritr.WriteElementString("server", txtServer.Text);
            xmlwritr.WriteElementString("uid", txtUid.Text);
            xmlwritr.WriteElementString("pwd", txtPwd.Text);
            xmlwritr.WriteElementString("database", txtDatabase.Text);


        private void cmdClose_Click(object sender, EventArgs e)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s