Click here to Skip to main content
15,887,467 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
IS it possible to make a new database in VisualStudio without installing SQlServer???

what about by code???
Posted

By using SqlCeEngine class and one of the methods, it is possible to create database in your hard drive! :)
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
using System.Data.SqlClient;
using System.Data.Sql;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlCeConnection Connection = new SqlCeConnection(@"Data Source=E:\Behnoud.sdf;");//string connStr = "Data Source = Test.sdf; Password = <password>";

            SqlCeEngine en = new SqlCeEngine(@"Data Source=E:\Behnoud.sdf;");

            en.CreateDatabase();

            try
            {
                Connection.Open();

                SqlCeCommand Command = Connection.CreateCommand();

                Command.CommandText = "CREATE TABLE Test(SerialNumber bigint)";

                Command.ExecuteNonQuery();

                
            }
            catch (SqlCeException ex)
            {
                Console.WriteLine(ex.Message); ;
            }

            finally
            {
                if (Connection.State == System.Data.ConnectionState.Open)
                {
                    Connection.Close();
                }
            }

            Console.ReadKey();
        }
    }
}
 
Share this answer
 
use msaccess to do so...

use this video for reference Connecting to an Access Database in Microsoft Visual Studio 2012[^]
 
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