tags:

views:

37

answers:

2

hi all write one project and encountered with one problem code:

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";

How to write tables in dbTrash, if (read the code)!

A: 

The problem may be the order of the commands

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

Should you not open the connection before running the command against that connection?

Shiraz Bhaiji
Shiraz BhaijiI want change master-DB to dbtrash-DB(myCon.ChangeDatabase("dbTrash") dont help)
4iNo
A: 

Is the dbTrash database being created or not?

Also, ensure that the GO statements are each on it's own lines.

Call ExecuteNonQuery() as well

.....
// 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
Zabba
I check the dbTrash in SQL Server Managment express studio
4iNo