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

StackOverflow https://stackoverflow.com/questions/8912858

  •  17-04-2021
  •  | 
  •  

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.

有帮助吗?

解决方案

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.

其他提示

In your case you're not specifying the database with iBlast on the second line, you're specifying the schema of the current database. Should be something like [database].[schema].[table]

you need to do iblast.dbo.tblBoreHoles or iBlast..tblBoreHoles. In SQL's naming convention, the part right before the table name is always the schema, which it doesn't appear you're trying to reference with "iBlast"

Try this:

    else
    {
        sqlConn.ConnectionString = "Data Source=VEEMER11;Initial Catalog=" + InitialCatalog + ";Integrated Security=SSPI";
    } 

Changing to SSPI may give you the permission you need. Check in your server that your windows user has full DBO privileges.

also, change your insert to this:

InsertTable("Create Table tblBoreHoles (HoleID uniqueIdentifier, HoleName nvarchar(40), JobID uniqueidentifier, CreateDate datetime, Longitude float, Latitude float, Altitude float, HoleDia real, ExpectedDepth float)", "iBlast");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top