이전 섹션, 우리는 데이터베이스 파일에 대한 준비가되어, 지금하자가, 포장 마이크로 소프트 엔티티 프레임 워크의 다양한 작업을 수행
이 방법들은 데이터베이스 CRUD에 따라 사용.
준비 :
새 콘솔을 엔지니어링, 프로젝트 디렉토리에 데이터베이스 복사본에 대한 부분은, 참으로 지역 설정을 복사
디렉토리 구조는 실질적으로 긴 방법입니다 :
그런 측면 뒤에 제공 # C 다양한 방법을 사용하여 nuget 패키지를 추가합니다 :
실질적으로 일반적인 작업은 검사 데이터는 클래스, 클래스 CRUD입니다
데이터를 검색하는 방법을 봐 :
//查询数据 사용 (VAR 연결 = 새로운 SQLiteConnection ( "데이터 소스 Student.db =")) { connection.Open (); VAR 명령 = 새로운 SQLiteCommand ( "StudentInformation에서 선택 *", 연결); VAR 어댑터는 새로운 SQLiteDataAdapter (명령) =; VAR 데이터 셋은 새로운 데이터 집합 () =; adapter.Fill (와 DataSet); VAR 테이블 dataSet.Tables = [0]; }
결과 보여
剩下的增删改,原理都一样,都是写sql语句,然后使用command上面的ExecuteNonQuery方法执行
增加数据
// 增加数据 using (var connection = new SQLiteConnection("data source=Student.db")) { connection.Open(); var command = new SQLiteCommand("insert into StudentInformation " + "values(\"王五\",22,\"陕西省西安市长安区\",\"看书,听音乐\",3)", connection); var result = command.ExecuteNonQuery(); }
效果:
执行前:
执行后:
删除数据
// 删除数据 using (var connection = new SQLiteConnection("data source=Student.db")) { connection.Open(); var command = new SQLiteCommand("delete from StudentInformation where Id = 2", connection); var result = command.ExecuteNonQuery(); }
效果:
执行前:
执行后:
修改数据
// 修改数据 using (var connection = new SQLiteConnection("data source=Student.db")) { connection.Open(); var command = new SQLiteCommand("update StudentInformation set Name = '张三New' where Id = 2", connection); var result = command.ExecuteNonQuery(); }
效果:
执行前:
执行后:
到此为止,我们已经能通过c#提供的方法 使用sql语句,对数据库文件进行增删改查了。