How to create Database and table in mssql server database using codes?

Hi! I want to learn how to make database. I want to make the following using VS2010-ultimate
Database name: Information.mdf with tables name ID int, Name varchar(250), and Cellno varchar(250) How to write the code and where it should be to put in vs2010 to run and create the Database? Please help ... thanks
Whing Dela CruzAsked:
Who is Participating?
 
AmmarRCommented:
hi ERWIIN

best way to get your job done is in sql server using sql management studio.
create the database and the tables and every thing you need.

then right click on the database ==> and generate script.

put this script in a .sql file, and now in your C# code, connect to your sql server and execute this file.


check discussion
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/43e8bc3a-1132-453b-b950-09427e970f31
using System.Data.SqlClient;
using System.IO;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string sqlConnectionString = "Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True";
            FileInfo file = new FileInfo("C:\\myscript.sql");
            string script = file.OpenText().ReadToEnd();
            SqlConnection conn = new SqlConnection(sqlConnectionString);
            Server server = new Server(new ServerConnection(conn));
            server.ConnectionContext.ExecuteNonQuery(script);
        }
    }
}

Open in new window

generate-DB.png
0
 
TempDBACommented:
0
 
Whing Dela CruzAuthor Commented:
Thanks a lot... more power to you all
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.