I am working on c# I need a help on Command obect. I am creating one database ,adding a table adding a record and I am trying to delete the database .Its always telling that connection is opened. I can give you the code pls tell me any wrong. I dont know how to destroy Command object completely. I have used dispose,close . Heres the code
if(connectionString == "") { connectionString = "Provider=SQLOLEDB;Data Source=Ravi;Initial Catalog=Master;" + "Integrated Security=SSPI;"; } String StrCreateDb = "Create Database ["+textBox1.Text+"]"; OleDbConnection connection = new OleDbConnection (connectionString); OleDbCommand cmd = new OleDbCommand (StrCreateDb,connection); //cmd.CommandText = StrCreateDb; cmd.CommandTimeout = 0; cmd.Connection = connection; connection.Open (); cmd.ExecuteNonQuery (); cmd.ResetCommandTimeout (); cmd.Dispose(); connection.Close (); if(connection != null) { connection.Close (); connection.Dispose (); } connection.Dispose (); connection = null; CreateTable(); private void CreateTable() { String connectionString = ""; if(connectionString == "") { String strTempDatabase = textBox1.Text; connectionString = "Provider=SQLOLEDB;Data Source=Ravi;Initial Catalog="+strTempDatabase+";" + "Integrated Security=SSPI;"; } String strCreateTable = "Create Table AAA (Column1 Varchar(250))"; OleDbConnection connection1 = new OleDbConnection (connectionString); OleDbCommand cmd1 = new OleDbCommand (strCreateTable,connection1); //cmd.CommandText = StrCreateDb; //cmd1.CommandTimeout = 0; connection1.Open (); cmd1.Connection = connection1; cmd1.ExecuteNonQuery (); cmd1.ResetCommandTimeout (); //Delete tabl //cmd1.Cancel(); cmd1.Dispose(); //cmd1.Connection.Close (); connection1.Close (); connection1.Dispose (); if(connection1 != null) { connection1.Close (); connection1.Dispose (); } connection1 = null; /////////////////////////////////////////////////// } //Delete database String connectionString; for(int x=0; x<=200000;x++) { } connectionString = ""; if(connectionString == "") { String strTemp= "Master"; connectionString = "Provider=SQLOLEDB;Data Source=Ravi;Initial Catalog="+strTemp+";" + "Integrated Security=SSPI;"; } String errorMessage = ""; try { String strDropdb = "Drop database ["+textBox1.Text+"]" ; OleDbConnection connection2 = new OleDbConnection(connectionString); OleDbCommand cmd2 = new OleDbCommand (strDropdb,connection2); //cmd.CommandText = StrCreateDb; cmd2.CommandTimeout = 0; cmd2.Connection = connection2; connection2.Open (); cmd2.ExecuteNonQuery (); cmd2.Connection.Close (); connection2.Close (); connection2.Dispose (); connection2=null; } catch(Exception ee) { errorMessage=ee.ToString(); MessageBox.Show(errorMessage); //if(ErrorOccured!=null) ErrorOccured (this,new EventArgs()); } --------------------------------- Do you Yahoo!? SBC Yahoo! DSL - Now only $29.95 per month!