在C#中建立本地数据库可以通过多种方式实现,以下是一些常见的方法:
1、安装SQL Server LocalDB:首先需要从微软官方网站下载并安装SQL Server LocalDB。
2、创建数据库连接字符串:在C#代码中,使用SqlConnection
类来连接数据库。
string connectionString = @"Data Source=(localdb)MSSQLLocalDB;Initial Catalog=MyDatabase;Integrated Security=True"; using (SqlConnection connection = new SqlConnection(connectionString)) { // 在这里执行数据库操作 }
3、创建和操作数据库:使用SqlCommand
类执行SQL语句来创建表、插入数据、查询数据等操作。
using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string createTableQuery = "CREATE TABLE MyTable (Id INT PRIMARY KEY, Name NVARCHAR(50))"; using (SqlCommand command = new SqlCommand(createTableQuery, connection)) { command.ExecuteNonQuery(); } // 插入数据 string insertQuery = "INSERT INTO MyTable (Name) VALUES (@Name)"; using (SqlCommand command = new SqlCommand(insertQuery, connection)) { command.Parameters.AddWithValue("@Name", "John Doe"); command.ExecuteNonQuery(); } // 查询数据 string selectQuery = "SELECT * FROM MyTable"; using (SqlCommand command = new SqlCommand(selectQuery, connection)) { using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { Console.WriteLine($"Id: {reader["Id"]}, Name: {reader["Name"]}"); } } } }
1、安装SQLite NuGet包:在Visual Studio中,通过NuGet包管理器安装System.Data.SQLite
或Microsoft.Data.SQLite
包。
2、创建数据库连接字符串:
string connectionString = "Data Source=MyDatabase.db;Version=3;"; using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { // 在这里执行数据库操作 }
3、创建和操作数据库:与使用SQL Server LocalDB类似,使用SQLiteCommand
类执行SQL语句来创建表、插入数据、查询数据等操作。
using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { connection.Open(); string createTableQuery = "CREATE TABLE IF NOT EXISTS MyTable (Id INTEGER PRIMARY KEY, Name TEXT)"; using (SQLiteCommand command = new SQLiteCommand(createTableQuery, connection)) { command.ExecuteNonQuery(); } // 插入数据 string insertQuery = "INSERT INTO MyTable (Name) VALUES (@Name)"; using (SQLiteCommand command = new SQLiteCommand(insertQuery, connection)) { command.Parameters.AddWithValue("@Name", "John Doe"); command.ExecuteNonQuery(); } // 查询数据 string selectQuery = "SELECT * FROM MyTable"; using (SQLiteCommand command = new SQLiteCommand(selectQuery, connection)) { using (SQLiteDataReader reader = command.ExecuteReader()) { while (reader.Read()) { Console.WriteLine($"Id: {reader["Id"]}, Name: {reader["Name"]}"); } } } }
使用Entity Framework Core In-Memory Database
1、安装Entity Framework Core NuGet包:在Visual Studio中,通过NuGet包管理器安装Microsoft.EntityFrameworkCore
包。
2、配置DbContext:创建一个继承自DbContext
的类,并在其中定义数据库集合。
public class MyDbContext : DbContext { public DbSet<MyEntity> MyEntities { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseInMemoryDatabase(); } } public class MyEntity { public int Id { get; set; } public string Name { get; set; } }
3、添加和操作数据:使用DbContext
实例来添加和操作数据。
using (var context = new MyDbContext()) { // 添加数据 context.MyEntities.Add(new MyEntity { Name = "John Doe" }); context.SaveChanges(); // 查询数据 var entities = context.MyEntities.ToList(); foreach (var entity in entities) { Console.WriteLine($"Id: {entity.Id}, Name: {entity.Name}"); } }
三种方法各有优缺点,开发者可以根据具体需求选择合适的方法来建立本地数据库。