Search code examples
c#sqlmsde

How do I create a new Database and new tables in SQL MSDE 2000 programmatically with C#.net?


I am writing a program that will create a new datatbase, then add tables to that datatbase. Here is my code....

InsertTable("Create Database iBlast", "Null");
InsertTable("Create Table iBlast.tblBoreHoles (HoleID uniqueIdentifier, HoleName nvarchar(40), JobID uniqueidentifier, CreateDate datetime, Longitude float, Latitude float, Altitude float, HoleDia real, ExpectedDepth float)", "iBlast");


static void InsertTable(String sqlQuery, string InitialCatalog)
{
    SqlConnection sqlConn = new SqlConnection();
    //sqlConn.ConnectionString = "Data Source=VIRTUAL2KB;Initial Catalog=PCS6000SQL;User ID=sa;Password=password;Integrated Security=False";
    if (InitialCatalog == "Null")
    {
        sqlConn.ConnectionString = "Data Source=VEEMER11;Integrated Security=True";
    }
    else
    {
        sqlConn.ConnectionString = "Data Source=VEEMER11;Initial Catalog=" + InitialCatalog + ";Integrated Security=True";
    }   
    sqlConn.Open();
    SqlCommand sqlCommand = new SqlCommand(sqlQuery, sqlConn);
    sqlCommand.ExecuteNonQuery();
}

The database creation works fine but I get an error when the code trys to create the table.

Error = "The specified schema name "iBlast" either does not exist or you do not have permission to use it."

Any help would be appreciated.


Solution

  • You are specifying "iBlast" as the initial catalog, so you don't need to specify it in the query:

     InsertTable("Create Table tblBoreHoles (...)", "iBlast");
    

    If you did need to specify it, the syntax would be iBlast..tblBoreHoles or iBlast.dbo.tblBoreHoles.