Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9290809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:30:17+00:00 2026-06-18T20:30:17+00:00

I am trying to create a database programmatically and create a table in it.

  • 0

I am trying to create a database programmatically and create a table in it.
This is my code :

 string connectionstr = string.Format(@"Data Source=.\sqlexpress;Initial Catalog={0};Integrated Security=True", "books");
        SqlConnection myConn = new SqlConnection(connectionstr);

        DirectoryInfo dir = new DirectoryInfo(@"D:\Data");
        dir.Create();

        string str3 = @"CREATE DATABASE [books] ON  PRIMARY 
        ( NAME = N'books', FILENAME = N'D:\DATA\books.mdf' , SIZE = 3072KB , FILEGROWTH = 1024KB )   
         LOG ON 
        ( NAME = N'books_log', FILENAME = N'D:\DATA\books_log.ldf' , SIZE = 1024KB , FILEGROWTH = 10%)
        IF NOT EXISTS (SELECT name FROM sys.filegroups WHERE is_default=1 AND name = N'PRIMARY')     
        ALTER DATABASE [books] MODIFY FILEGROUP [PRIMARY] DEFAULT";

        SqlCommand myCommand = new SqlCommand(str3, myConn); 
        myConn.Open();
        myCommand.ExecuteNonQuery();
        myConn.Close();

but i am getting the error:

Cannot open database “books” requested by the login. The login failed.
Login failed for user ‘Master-PC\Master’.

what can i do to solve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T20:30:18+00:00Added an answer on June 18, 2026 at 8:30 pm

    Well, reading your query it is clear that the books database doesn’t exist at the connection time. You try to create it with your query.
    Change your connection string and connect to ‘master’ database

    string connectionstr = @"Data Source=.\sqlexpress;Initial Catalog=master;"
                           @"Integrated Security=True";
    

    then execute your query, (this will create the database ‘books’), close the connection and reopen it with the original “InitialCatalog=books” string

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a table in a SQL CE database programmatically. Currently,
I am trying to create a database table using PostgreSQL but I am unable
I am trying to create a database with the name of a string stored
I'm trying to create a hypothetical videostore database and this error message comes up
I am trying to create a database table when the page loads. I tried
I'm trying to create a billing database with Entity Framework 4.3 using Code First
I am trying to create two tables within the same sqlite database. Now, this
I'm trying to create a database model with Code First, without creating the database
I am trying to create a new database programmatically using SMO (c#). I am
I am trying to create a database using code-first approach. When I am running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.