猿创征文|C#基础——DBHelper
前言
本节内容为方便大家复制,所有内容都写在了引用模块。
DBHelper源码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; namespace CShapDBHelper { public class DBHelper { private static string url = "server=.;database=girl1804;uid=sa;pwd=root"; /// <summary> /// 获取查询信息 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static DataTable Query(string sql) { SqlConnection conn = new SqlConnection(url); SqlDataAdapter sdap = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(); sdap.Fill(ds); return ds.Tables[0]; } /// <summary> /// 增删改 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static int ExecuteNonQuery(string sql) { SqlConnection conn = new SqlConnection(url); conn.Open(); SqlCommand cmd = new SqlCommand(sql,conn); int rows = cmd.ExecuteNonQuery(); conn.Close(); return rows; } } }