2010-10-17 12 views
0

Salut à tous écrire un projet et un problème rencontré lors de la code:base de données CANT changement

private SqlConnection scon; 
    private SqlCommand scom; 

    string defConnection = 
     "Data Source=" + Environment.MachineName + @"\SQLEXPRESS;" + 
     "Integrated security=true;" + 
     "database=dbTrash"; 

    string altConnection = 
     "Data Source=" + Environment.MachineName + @"\SQLEXPRESS;" + 
     "Integrated security=SSPI;" + 
     "database=master"; 
    /// <summary> 
    /// Constructor, trying connect to DB 
    /// </summary> 
    public DB() 
    { 
     scon = new SqlConnection(defConnection); 

     try 
     { 
      scon.Open(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Не удалось создать подключение к БД\n"+ 
       "Будет созданая новая БД, инструкцию прочтите в справке\nПричина: " +ex.Message, "Сообщение"); 
      try 
      { 
       // dbTrash exist, trying connect to db master, succesfully! 
       SqlConnection myConn = new SqlConnection(altConnection); 

       // create MY db - dbTrash 
       SqlCommand myCommand = new SqlCommand(createDB, myConn); 

       try 
       { 
        myConn.Open(); 
        myCommand.ExecuteNonQuery(); 
       } 
       catch (System.Exception x) 
       { 
        MessageBox.Show(x.Message, "Сообщение"); 
       } 
       finally 
       { 
        if (myConn.State == ConnectionState.Open) 
        { 
         myConn.Close(); 
        } 
       } 
      } 
      catch (Exception e) 
      { 
       MessageBox.Show("Не удалось создать новую БД\nПричина: " + e.Message, "Сообщение"); 
      } 

      // trying connect to dbTrash 
      // AND NOTHING. ERROR - CAN'T CONNECT TO DB, 'CAUSE EXIST dbTrash!!!!!!!!!!!! 
      SqlConnection scon1 = new SqlConnection(defConnection); 
      // create tables in DB but nothin' 
      SqlCommand scom1 = new SqlCommand(createTables, scon1); 
      scon1.Open(); 
     } 

    } 

const string createDB = "create database dbTrash"; 

    /// <summary> 
    /// 
    /// </summary> 
    const string createTables = "create table [dbo].[Types] (" + 
           "id int not null," + 
           "types nvarchar(15) not null," + 
            "primary key (id)" + 
           ") GO " + 
           "create table Files (" + 
           "id int not null," + 
           "nameOfFile nvarchar(50) not null," + 
           "fileSizeInByte int not null," + 
            "fileSize float not null," + 
           "typeId int not null," + 
           "tags nvarchar(200)," + 
           "filePath nvarchar(60) unique not null," + 
           "xml nvarchar(300)," + 
           "primary key (id)," + 
           "foreign key (typeId) references Types(id)" + 
           "); GO"; 

Comment écrire des tables dans dbTrash, si (lire le code)!

+0

de l'Ukraine, désolé) – 4iNo

+1

Pourquoi êtes-vous d'ajouter le code pour ouvrir la connexion de base de données dans les prises() où la connexion n'a pas pu être ouvert en premier lieu ?! – Zabba

+0

Pas besoin d'être désolé; et la question n'est pas «merde» – Zabba

Répondre

0

Le problème peut être l'ordre des commandes

 SqlConnection scon1 = new SqlConnection(defConnection); 
     // create tables in DB but nothin' 
     SqlCommand scom1 = new SqlCommand(createTables, scon1); 
     scon1.Open(); 

Si vous ouvrez pas la connexion avant d'exécuter la commande contre cette connexion?

+0

Shiraz Bhaiji Je veux changer maître-DB à dbtrash-DB (myCon.ChangeDatabase ("dbTrash") n'aide pas) – 4iNo

0

La base de données dbTrash est-elle créée ou non?

De même, assurez-vous que les instructions GO sont chacune sur ses propres lignes.

Appel ExecuteNonQuery() et

..... 
// AND NOTHING. ERROR - CAN'T CONNECT TO DB, 'CAUSE EXIST dbTrash!!!!!!!!!!!! 
SqlConnection scon1 = new SqlConnection(defConnection); 
// create tables in DB but nothin' 
SqlCommand scom1 = new SqlCommand(createTables, scon1); 
scon1.Open(); 
scom1.ExecuteNonQuery(); //call this 
+0

Je vérifie le dbTrash dans SQL Server Managment Express Studio – 4iNo

Questions connexes