Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i having task to create database dynamically, run time i give servr name ,database,user name,password to the text box and click to button, through script the database to create.
Posted

 
Share this answer
 
Comments
Аslam Iqbal 4-Apr-11 14:01pm    
good answer.
Monjurul Habib 4-Apr-11 17:07pm    
thank you.
Аslam Iqbal 16-Apr-11 1:03am    
you are welcome:)
Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A quick search using "create database dynamically" pasted from your question subject gave 637,000,000 hits. The first page alone is full of explanations - just pick the one that fits your circumstances.
http://www.google.co.uk/search?sourceid=chrome&ie=UTF-8&q=create+database+dynamically[^]

Try to use Google - it can save you lots of time, as opposed to posting a question here...
 
Share this answer
 
Comments
prahalad.gaggar 29-Nov-12 4:31am    
why can't u provide us with the code!!!!
one should always avoid links while replying answer to a Question!!!
OriginalGriff 29-Nov-12 4:47am    
No, one should give links when people are too lazy to do at least basic research themselves.
prahalad.gaggar 30-Nov-12 4:14am    
One must have research it from his side and after getting nothing from useful from his research, then and then only one should have asked the question. This is what one must have done (From my point of view).

you are saying about lazy, i don't think that lazy people can survive in this field.

What i requested from you is that one must not give random links.

As in your case u said the first link from from Google search.
I want to say that link from google search keep on changing (i think you know that), so i thought one must not give such links!!!!

Hope that goes well with you!!!
To add to the answers already given, re-consider creating the database dynamically.

Usually there's no reason to create new databases dynamically. This is mainly a question of design. In many cases the need of multiple databases can be resolved with a single (or several) well-modeled permanent databases.

Since you didn't specify the reason to create the database dynamically, there's no way of saying which approach is better in your situation but if you think about the situation again you may end up to different solution.
 
Share this answer
 
From your question i conclude that
1.You want to write a query in your code(C#) that can work as if you are writing and executing query on your (SQL server).
Accordingly i am writing a simple code that creates a table(base on the data-table created by your code) in SQL server and inserts records in it from a data-table.

C#
SqlConnection con = new SqlConnection("Data Source=.;uid=sa;pwd=sa123;database=Example1");
            SqlCommand sqlCom = new SqlCommand("SELECT count(*) FROM sysobjects WHERE xtype = 'U' and name = 'abcd'", con);
            int i = 0;
            con.Open();
            i = int.Parse(sqlCom.ExecuteScalar().ToString());
            if (i > 0)
            {
                string drop = "drop table abcd";
                SqlCommand cmd1 = new SqlCommand(drop, con);
                cmd1.ExecuteNonQuery();
                //Exists
            }
            string sql = "Create Table abcd (";
            foreach (DataColumn column in dt.Columns)
            {
                sql += "[" + column.ColumnName + "] " + "nvarchar(50)" + ",";
            }
            sql = sql.TrimEnd(new char[] { ',' }) + ")";
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            cmd.ExecuteNonQuery();
            using (var adapter = new SqlDataAdapter("SELECT * FROM abcd", con)) 
            using(var builder = new SqlCommandBuilder(adapter))
            {
                adapter.InsertCommand = builder.GetInsertCommand();
                adapter.Update(dt);
            }
            con.Close();


Please make the appropriate changes in your connection string.
Please vote my answer if it helped you in getting your solution!!!
 
Share this answer
 
Comments
Member 10462036 14-Dec-13 3:27am    
Sir i want create new database through asp.net how it is possible.

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