在平常的工做中,一般一個項目會大量用的數據庫的各類基本操做,所以小編幾個常見的數據庫的操做封裝成了一個dll方便後續的開發使用。SQLserver數據庫是最爲常見的一種數據庫,本文則主要是記錄了C#對SQL的鏈接、增、刪、改、查的基本操做,若有什麼問題還請各位大佬指教。後續也將對其餘幾個經常使用的數據庫進行相應的整理。話很少說,直接開始碼代碼。sql
先聲明一個SqlConnection便於後續使用。數據庫
private SqlConnection sql_con;//聲明一個SqlConnection
/// <summary> /// SQLserver open /// </summary> /// <param name="link">link statement</param> /// <returns>Success:success; Fail:reason</returns> public string Sqlserver_Open(string link) { try {
sql_con = new SqlConnection(link);
sql_con.Open();
return "success";
}
catch (Exception ex)
{
return ex.Message;
}
}
/// <summary> /// SQLserver close /// </summary> /// <returns>Success:success Fail:reason</returns> public string Sqlserver_Close() { try { if (sql_con == null) { return "No database connection"; } if (sql_con.State == ConnectionState.Open || sql_con.State == ConnectionState.Connecting) { sql_con.Close(); sql_con.Dispose(); } else { if (sql_con.State == ConnectionState.Closed) { return "success"; } if (sql_con.State == ConnectionState.Broken) { return "ConnectionState:Broken"; } } return "success"; } catch (Exception ex) { return ex.Message; } }
/// <summary> /// SQLserver insert,delete,update /// </summary> /// <param name="sql">insert,delete,update statement</param> /// <returns>Success:success + Number of affected rows; Fail:reason</returns> public string Sqlserver_Insdelupd(string sql) { try { int num = 0; if (sql_con == null) { return "Please open the database connection first"; } if (sql_con.State == ConnectionState.Open) { SqlCommand sqlCommand = new SqlCommand(sql, sql_con); num = sqlCommand.ExecuteNonQuery(); } else { if (sql_con.State == ConnectionState.Closed) { return "Database connection closed"; } if (sql_con.State == ConnectionState.Broken) { return "Database connection is destroyed"; } if (sql_con.State == ConnectionState.Connecting) { return "The database is in connection"; } } return "success" + num; } catch (Exception ex) { return ex.Message.ToString(); } }
/// <summary> /// SQLserver select /// </summary> /// <param name="sql">select statement</param> /// <param name="record">Success:success; Fail:reason</param> /// <returns>select result</returns> public DataSet Sqlserver_Select(string sql, out string record) { try { DataSet dataSet = new DataSet(); if (sql_con == null) { record = "Please open the database connection first"; return dataSet; } if (sql_con.State == ConnectionState.Open) { SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, sql_con); sqlDataAdapter.Fill(dataSet, "sample"); sqlDataAdapter.Dispose(); record = "success"; return dataSet; } if (sql_con.State == ConnectionState.Closed) { record = "Database connection closed"; return dataSet; } if (sql_con.State == ConnectionState.Broken) { record = "Database connection is destroyed"; return dataSet; } if (sql_con.State == ConnectionState.Connecting) { record = "The database is in connection"; return dataSet; } record = "ERROR"; return dataSet; } catch (Exception ex) { DataSet dataSet = new DataSet(); record = ex.Message.ToString(); return dataSet; } }
小編髮現以上這種封裝方式仍是很麻煩,每次對SQL進行增刪改查的時候還得先打開數據庫,最後還要關閉,實際運用起來比較麻煩。所以對上面兩個增刪改查的方法進行了重載,在每次進行操做時都先打開數據庫,而後關閉數據庫。spa
/// <summary> /// SQLserver insert,delete,update /// </summary> /// <param name="sql">insert,delete,update statement</param> /// <param name="link">link statement</param> /// <returns>Success:success + Number of affected rows; Fail:reason</returns> public string Sqlserver_Insdelupd(string sql, string link) { try { int num = 0; using (SqlConnection con = new SqlConnection(link)) { con.Open(); SqlCommand cmd = new SqlCommand(sql, con); num = cmd.ExecuteNonQuery(); con.Close(); return "success" + num; } } catch (Exception ex) { return ex.Message.ToString(); } }
/// <summary> /// SQLserver select /// </summary> /// <param name="sql">select statement</param> /// <param name="link">link statement</param> /// <param name="record">Success:success; Fail:reason</param> /// <returns>select result</returns> public DataSet Sqlserver_Select(string sql, string link, out string record) { try { DataSet ds = new DataSet(); using (SqlConnection con = new SqlConnection(link)) { con.Open(); SqlDataAdapter sda = new SqlDataAdapter(sql, con); sda.Fill(ds, "sample"); con.Close(); sda.Dispose(); record = "success"; return ds; } } catch (Exception ex) { DataSet dataSet = new DataSet(); record = ex.Message.ToString(); return dataSet; } }