Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi.I'm trying to create database using c# asp.net,but I can't.Can any one help me here.

What I have tried:

Here is my c# asp.net program it's execute well and data store in database table but database is not creating.Kindly tell me stored procedure that how can i create database using stored procedure so that i pass value through program and database will be created in database using stored procedure.here i try,it is not through stored procedure,kindly tell me stored procedure or correct this one.
C#
 public void create(PropertyService p)
    {
        cnn.Open();
        cmd2 = new SqlCommand("SELECT User_ID FROM tbl_user WHERE User_Name='" + p.userDB + "'", cnn);
        string id = cmd2.ExecuteScalar().ToString();
        int ID = Int32.Parse(id);
        cmd = new SqlCommand("createDB", cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@DbName", p.dbname);
        string str;
        
            str = "CREATE DATABASE '" + p.dbname + "' ON PRIMARY"
    + "(Name= '" + p.dbname + "', filename = 'C:\\mysql\\ '" + p.dbname + "'.mdf', size=3,"
    + "maxsize=5, filegrowth=10%)log on"
    + "(name= '" + p.dbname + "', filename='C:\\mysql\\ '" + p.dbname + "'.ldf',size=3,"
    + "maxsize=20,filegrowth=1)";
            SqlCommand myCommand = new SqlCommand(str, cnn);
            cmd.Parameters.AddWithValue("@Description", p.description);
            cmd.Parameters.AddWithValue("@Date", p.dbdate);
            cmd.Parameters.AddWithValue("@user", ID);
            cmd.ExecuteNonQuery();
            cmd2.ExecuteNonQuery();
            myCommand.ExecuteNonQuery();
            cnn.Close();
        
        
      

    }
}

connection string etc define above,every thing is working fine but issue is database.thanks
Posted
Updated 31-Mar-16 20:45pm
Comments
Sinisa Hajnal 1-Apr-16 2:21am    
Before you execute this query in code do the following:
1) wrap everything in try..catch block, see if there are any errors
2) set a breakpoint at str = "CREATE..." and step through.
3) before continuing with myCommand, execute str value in database query console...check if database is created.

1 solution

Hi, refer these articles, which helps you to solve your issue,
1. Create an SQL Server Database Using C#[^]
2. https://support.microsoft.com/en-in/kb/307283[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900