开发者

Cant change database

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;" +
   开发者_StackOverflow社区     "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)!


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?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜