在Visual Studio中创建新数据库而无需安装SQL Server
问题描述:
嗨
是否可以在VisualStudio中创建新数据库而无需安装SQlServer ???
怎么样通过代码???
Hi
IS it possible to make a new database in VisualStudio without installing SQlServer???
what about by code???
答
通过使用SqlCeEngine类和其中一种方法,可以在硬盘中创建数据库! :)
By using SqlCeEngine class and one of the methods, it is possible to create database in your hard drive! :)
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();
}
}
}
使用msaccess这样做...
使用此视频作为参考连接到Microsoft Visual Studio 2012中的Access数据库 [ ^ ]
use msaccess to do so...
use this video for reference Connecting to an Access Database in Microsoft Visual Studio 2012[^]