Click here to Skip to main content
15,909,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Experts,

I'm creating a database application. I create a simple dialog with a button. On button click event i'm trying to create a db. But while I'm doing "myConn1.Open();" in below code, I'm getting exception like "Cannot open database. The login failed." But with same user name and password i'm able to open another connection (conn.Open()). Please let me know what is the wrong in the code.
I'm newbie and small help is greatly appreciated.


C#
try
            {
                SqlConnection myConn = new SqlConnection("Server=SI-MyUID\\SQLEXPRESSSUD;User ID=sa; Password=password; database=master");
                SqlConnection myConn1 = new SqlConnection("Server=SI-SI-MyUID\\SQLEXPRESSSUD;User ID=sa; Password=password; database=MyDatabaseData");

                
                String str = "CREATE DATABASE MyDatabase ON PRIMARY " +
                    "(NAME = MyDatabase_Data, " +
                    "FILENAME = 'C:\\MyDatabaseData.mdf', " +
                    "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
                    "LOG ON (NAME = MyDatabase_Log, " +
                    "FILENAME = 'C:\\MyDatabaseLog.ldf', " +
                    "SIZE = 1MB, " +
                    "MAXSIZE = 5MB, " +
                    "FILEGROWTH = 10%)";

                    

                SqlCommand myCommand = new SqlCommand(str, myConn);
                    

                String str1 = "CREATE TABLE myTable" +
                                "(myId INTEGER CONSTRAINT PKeyMyId PRIMARY KEY," +
                                "myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)";

                SqlCommand myCommand1 = new SqlCommand(str1, myConn1);

                try 
                {
                    myConn.Open();
	                myCommand.ExecuteNonQuery();
                    myConn.Close();

                    myConn1.Open(); // Exception Occurring Here
                    myCommand1.ExecuteNonQuery();
                    MessageBox.Show("DataBase is Created Successfully", 
                                    "MyProgram", 
                                    MessageBoxButtons.OK, 
                                    MessageBoxIcon.Information);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString(), 
                                    "MyProgram", 
                                    MessageBoxButtons.OK, 
                                    MessageBoxIcon.Information);
                }
                finally
                {
                    if (myConn.State == ConnectionState.Open)
                    {
	                    myConn.Close();
                    }
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show("Error\n"+exp);
                throw;
            }
        }
Posted
Comments
Richard MacCutchan 25-Apr-12 5:07am    
Are your server names supposed to be different?

I think in myConn1, the server name is incorrect:
C#
SqlConnection myConn1 = new SqlConnection("Server=SI-SI-MyUID\\SQLEXPRESSSUD;User ID=sa; Password=password; database=MyDatabaseData");


It must be:
C#
SqlConnection myConn1 = new SqlConnection("Server=SI-MyUID\\SQLEXPRESSSUD;User ID=sa; Password=password; database=MyDatabaseData");


Edited:

Also use this ("NAME = MyDatabaseData"):
C#
String str = "CREATE DATABASE MyDatabaseData ON PRIMARY " +
      "(NAME = MyDatabaseData, " +
      "FILENAME = 'C:\\MyDatabaseData.mdf', " +
      "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
      "LOG ON (NAME = MyDatabaseData_log, " +
      "FILENAME = 'C:\\MyDatabaseData_log.ldf', " +
      "SIZE = 1MB, " +
      "MAXSIZE = 5MB, " +
      "FILEGROWTH = 10%)";

I ran the code. It worked perfect.

Hope it helps.
 
Share this answer
 
v2
Comments
vidiking 25-Apr-12 5:20am    
Changed to SqlConnection myConn1 = new SqlConnection("Server=SI-MyUID\\SQLEXPRESSSUD;User ID=sa; Password=password; database=MyDatabaseData");.

Still facing same problem. :(
Nikfazan 25-Apr-12 8:04am    
I edited the solution.
Hope it helps.
Does "MyDatabaseData" database exists which you using in 2nd connection string?
 
Share this answer
 
Comments
vidiking 25-Apr-12 5:20am    
Yes. After executing SqlCommand myCommand = new SqlCommand(str, myConn);, i checked in SQL server management tool. Its existing
nagendrathecoder 25-Apr-12 5:41am    
Your database name is different in 2nd connection string.
Database name was different.

C#
SqlConnection myConn1 = new SqlConnection("Server=SI-KSUDHANSU\\SQLEXPRESSSUD;User ID=sa; Password=lgsoft123; database=MyDatabase");

<pre lang="vb">str = "CREATE DATABASE MyDatabase ON PRIMARY " +
                "(NAME = MyDatabase_Data, " +
                "FILENAME = 'C:\\MyDatabaseData.mdf', " +
                "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
                "LOG ON (NAME = MyDatabase_Log, " +
                "FILENAME = 'C:\\MyDatabaseLog.ldf', " +
                "SIZE = 1MB, " +
                "MAXSIZE = 5MB, " +
                "FILEGROWTH = 10%)"
 
Share this answer
 
What i can figure is you want o create database MyDatabase and use it on following commands

C#
SqlConnection myConn1 = new SqlConnection("Server=SI-SI-MyUID\\SQLEXPRESSSUD;User ID=sa; Password=password; database=MyDatabaseData"); //change to MyDatabase you just created and not MyDatabaseData so login fails





I would suggest some changes as


C#
try
            {
                SqlConnection myConn = new SqlConnection("Server=SI-MyUID\\SQLEXPRESSSUD;User ID=sa; Password=password; database=master");
                SqlConnection myConn1 = new SqlConnection("Server=SI-SI-MyUID\\SQLEXPRESSSUD;User ID=sa; Password=password; database=MyDatabase");
 
                
                String str = "CREATE DATABASE MyDatabase ON PRIMARY " +
                    "(NAME = MyDatabase_Data, " +
                    "FILENAME = 'C:\\MyDatabaseData.mdf', " +
                    "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
                    "LOG ON (NAME = MyDatabase_Log, " +
                    "FILENAME = 'C:\\MyDatabaseLog.ldf', " +
                    "SIZE = 1MB, " +
                    "MAXSIZE = 5MB, " +
                    "FILEGROWTH = 10%)";
 
                    
 
                SqlCommand myCommand = new SqlCommand(str, myConn);
                    
 
                String str1 = "CREATE TABLE myTable" +
                                "(myId INTEGER CONSTRAINT PKeyMyId PRIMARY KEY," +
                                "myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)";
 
                SqlCommand myCommand1 = new SqlCommand(str1, myConn1);
 
                try 
                {
                    myConn.Open();
	              myCommand.ExecuteNonQuery();

<pre lang="vb">MessageBox.Show("DataBase is Created Successfully",
                                    "MyProgram",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);

                    myConn.Close();
 
                    myConn1.Open(); // Exception Occurring Here
                    myCommand1.ExecuteNonQuery();
                    
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString(), 
                                    "MyProgram", 
                                    MessageBoxButtons.OK, 
                                    MessageBoxIcon.Information);
                }
                finally
                {
                    if (myConn.State == ConnectionState.Open)
                    {
	                    myConn.Close();
                    }
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show("Error\n"+exp);
                throw;
            }
        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900