C#實現對資料庫增刪改查小案例
阿新 • • 發佈:2022-03-08
資料庫參照之前文章中的學生資料庫表格
本次實現一個小案例,實現C#對資料庫的增刪改查的綜合運用,並在控制檯進行測試。
1.資料庫資訊新增部分程式碼:
public static void InsertSql() { using(SqlConnection connection = new SqlConnection(strConn)) { connection.Open(); string sql = "insert into studentinfo" + "(studentno,name,mobile,gender,classname,age) values" + "('016','Karrolet','18912345678','男','二班','22')"; using (SqlCommand sqlCommand = connection.CreateCommand()) { sqlCommand.CommandText = sql; int resultCount = sqlCommand.ExecuteNonQuery(); if (resultCount > 0) { Console.WriteLine("資料庫資訊插入成功"); } else { Console.WriteLine("資料庫資訊插入失敗"); } } connection.Close(); } }
2.資料庫資訊刪除部分程式碼
public static void DeleteSql() { using (SqlConnection connection = new SqlConnection(strConn)) { connection.Open(); string sql = "delete from studentinfo where studentno = '016'"; using (SqlCommand sqlCommand = connection.CreateCommand()) { sqlCommand.CommandText = sql; int resultCount = sqlCommand.ExecuteNonQuery(); if (resultCount > 0) { Console.WriteLine("資料庫資訊刪除成功"); } else { Console.WriteLine("資料庫資訊刪除失敗"); } } connection.Close(); } }
3.資料庫資訊更新部分程式碼:
public static void UpdateSql() { using (SqlConnection connection = new SqlConnection(strConn)) { connection.Open(); string sql = "update studentinfo set name = '李逸龍' where studentno = '016'"; using (SqlCommand sqlCommand = connection.CreateCommand()) { sqlCommand.CommandText = sql; int resultCount = sqlCommand.ExecuteNonQuery(); if (resultCount > 0) { Console.WriteLine("資料庫資訊更新成功"); } else { Console.WriteLine("資料庫資訊更新失敗"); } } connection.Close(); } }
4.資料庫資訊查詢部分程式碼:
public static void QuerySql() { using (SqlConnection connection = new SqlConnection(strConn)) { connection.Open(); string sql = "select " + "studentid,studentno,name,mobile,gender,classname,age,createdon " + "from studentinfo order by studentno desc"; SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, connection); DataTable dt = new DataTable(); sqlDataAdapter.Fill(dt); foreach (DataRow dr in dt.Rows) { Console.WriteLine("studentid={0},studentno={1},name={2},mobile={3},gender={4},classname={5},age={6},createdon={7}", dr[0], dr[1], dr[2], dr[3], dr[4], dr[5], dr[6], dr[7]); } connection.Close(); } }
注:以上程式碼全部寫死,思考如何改進。
案例完整程式碼:
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SqlControl { class Program { static string strConn = "Data Source=.;Initial Catalog=Student;User Id=sa;Password=123456"; static void Main(string[] args) { int m; Console.WriteLine("請輸入選項以進行資料庫修改"); Console.WriteLine("1.資料庫資訊新增"); Console.WriteLine("2.資料庫資訊刪除"); Console.WriteLine("3.資料庫資訊修改"); Console.WriteLine("4.資料庫資訊查詢"); Console.WriteLine("5.退出"); while (true) { try { int n = int.Parse(Console.ReadLine()); switch (n) { case 1: InsertSql(); break; case 2: DeleteSql(); break; case 3: UpdateSql(); break; case 4: QuerySql(); break; case 5: Console.WriteLine("操作結束"); Console.ReadLine(); return; default: Console.WriteLine("輸入錯誤請重新輸入!"); break; } } catch { Console.WriteLine("輸入錯誤"); } //int n = int.Parse(Console.ReadLine()); } Console.ReadLine(); } /// <summary> /// 資料庫資訊增加 /// </summary> public static void InsertSql() { using(SqlConnection connection = new SqlConnection(strConn)) { connection.Open(); string sql = "insert into studentinfo" + "(studentno,name,mobile,gender,classname,age) values" + "('016','Karrolet','18912345678','男','二班','22')"; using (SqlCommand sqlCommand = connection.CreateCommand()) { sqlCommand.CommandText = sql; int resultCount = sqlCommand.ExecuteNonQuery(); if (resultCount > 0) { Console.WriteLine("資料庫資訊插入成功"); } else { Console.WriteLine("資料庫資訊插入失敗"); } } connection.Close(); } } /// <summary> /// 資料庫資訊刪除 /// </summary> public static void DeleteSql() { using (SqlConnection connection = new SqlConnection(strConn)) { connection.Open(); string sql = "delete from studentinfo where studentno = '016'"; using (SqlCommand sqlCommand = connection.CreateCommand()) { sqlCommand.CommandText = sql; int resultCount = sqlCommand.ExecuteNonQuery(); if (resultCount > 0) { Console.WriteLine("資料庫資訊刪除成功"); } else { Console.WriteLine("資料庫資訊刪除失敗"); } } connection.Close(); } } /// <summary> /// 資料庫資訊更新 /// </summary> public static void UpdateSql() { using (SqlConnection connection = new SqlConnection(strConn)) { connection.Open(); string sql = "update studentinfo set name = '李逸龍' where studentno = '016'"; using (SqlCommand sqlCommand = connection.CreateCommand()) { sqlCommand.CommandText = sql; int resultCount = sqlCommand.ExecuteNonQuery(); if (resultCount > 0) { Console.WriteLine("資料庫資訊更新成功"); } else { Console.WriteLine("資料庫資訊更新失敗"); } } connection.Close(); } } /// <summary> /// 資料庫資訊查詢 /// </summary> public static void QuerySql() { using (SqlConnection connection = new SqlConnection(strConn)) { connection.Open(); string sql = "select " + "studentid,studentno,name,mobile,gender,classname,age,createdon " + "from studentinfo order by studentno desc"; SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, connection); DataTable dt = new DataTable(); sqlDataAdapter.Fill(dt); foreach (DataRow dr in dt.Rows) { Console.WriteLine("studentid={0},studentno={1},name={2},mobile={3},gender={4},classname={5},age={6},createdon={7}", dr[0], dr[1], dr[2], dr[3], dr[4], dr[5], dr[6], dr[7]); } connection.Close(); } } } }
注意獲取輸入時的異常問題。