.cs
后缀,数据库文件可以是 .mdf
(SQL Server)或 .db
(SQLite)等。
在C#中操作文件和数据库文件是常见的任务,以下是关于这方面的详细内容:
1、文件操作
创建和写入文件
使用StreamWriter类:这是最常用的写入文本文件的方式,以下代码创建了一个名为“example.txt”的文件,并向其中写入一些文本:
using (StreamWriter sw = new StreamWriter("example.txt")) { sw.WriteLine("Hello, World!"); sw.WriteLine("This is a test file."); }
使用File.WriteAllText方法:该方法可以直接将字符串写入指定的文件中,如果文件不存在则创建它,示例如下:
File.WriteAllText("example2.txt", "This is some text.");
读取文件
使用StreamReader类:用于从文本文件中逐行读取文本。
using (StreamReader sr = new StreamReader("example.txt")) { string line; while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } }
使用File.ReadAllText方法:可以一次性读取整个文件的内容到一个字符串中,示例如下:
string content = File.ReadAllText("example2.txt"); Console.WriteLine(content);
文件的复制、移动和删除
复制文件:可以使用File.Copy方法来复制文件,将“example.txt”复制为“copy_of_example.txt”:
File.Copy("example.txt", "copy_of_example.txt");
移动文件:使用File.Move方法可以将文件从一个位置移动到另一个位置,将“example.txt”移动到“new_folder/example.txt”:
File.Move("example.txt", "new_folder/example.txt");
删除文件:使用File.Delete方法可以删除指定的文件,删除“example.txt”:
File.Delete("example.txt");
2、数据库文件操作(以SQLite为例)
引入SQLite库:首先需要在项目中添加对SQLite的支持,可以通过NuGet包管理器安装System.Data.SQLite
包。
连接到数据库:创建一个SQLite连接对象,并指定数据库文件的路径,连接到一个名为“mydatabase.db”的数据库文件:
using (var connection = new SQLiteConnection("Data Source=mydatabase.db;Version=3;")) { connection.Open(); // 在这里执行数据库操作 }
创建表:使用SQL语句创建表,创建一个名为“Users”的表,包含“Id”、“Name”和“Age”三个字段:
using (var connection = new SQLiteConnection("Data Source=mydatabase.db;Version=3;")) { connection.Open(); using (var command = new SQLiteCommand("CREATE TABLE IF NOT EXISTS Users (Id INTEGER PRIMARY KEY, Name TEXT, Age INTEGER)", connection)) { command.ExecuteNonQuery(); } }
插入数据:向表中插入数据,向“Users”表中插入一条记录:
using (var connection = new SQLiteConnection("Data Source=mydatabase.db;Version=3;")) { connection.Open(); using (var command = new SQLiteCommand("INSERT INTO Users (Name, Age) VALUES (@name, @age)", connection)) { command.Parameters.AddWithValue("@name", "John Doe"); command.Parameters.AddWithValue("@age", 30); command.ExecuteNonQuery(); } }
查询数据:从表中查询数据,查询“Users”表中的所有记录:
using (var connection = new SQLiteConnection("Data Source=mydatabase.db;Version=3;")) { connection.Open(); using (var command = new SQLiteCommand("SELECT * FROM Users", connection)) { using (var reader = command.ExecuteReader()) { while (reader.Read()) { int id = reader.GetInt32(0); string name = reader.GetString(1); int age = reader.GetInt32(2); Console.WriteLine($"Id: {id}, Name: {name}, Age: {age}"); } } } }
更新数据:更新表中的数据,将“Users”表中Id为1的用户的年龄修改为35:
using (var connection = new SQLiteConnection("Data Source=mydatabase.db;Version=3;")) { connection.Open(); using (var command = new SQLiteCommand("UPDATE Users SET Age = @age WHERE Id = @id", connection)) { command.Parameters.AddWithValue("@age", 35); command.Parameters.AddWithValue("@id", 1); command.ExecuteNonQuery(); } }
删除数据:删除表中的数据,删除“Users”表中Id为1的用户:
using (var connection = new SQLiteConnection("Data Source=mydatabase.db;Version=3;")) { connection.Open(); using (var command = new SQLiteCommand("DELETE FROM Users WHERE Id = @id", connection)) { command.Parameters.AddWithValue("@id", 1); command.ExecuteNonQuery(); } }
C#提供了丰富的类和方法来操作文件和数据库文件,使得开发人员能够方便地进行数据的存储、读取和管理,无论是处理本地文件还是与数据库进行交互,都可以通过简单的代码实现各种功能。