Web.config
1 <connectionStrings> 2 <add name="connStr" connectionString="server=.;database=MyWeb;uid=sa;pwd=123"/> 3 </connectionStrings>
SqlHelper.cs
1 /// <summary> 2 /// SqlHelper(数据库帮助类) 3 /// </summary> 4 public static class SqlHelper 5 { 6 /// <summary> 7 /// SQL连接字符串 8 /// </summary> 9 private static string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; 10 /// <summary> 11 /// 获取数据库列表数据 12 /// </summary> 13 /// <param name="sql">SQL语句</param> 14 /// <param name="ps">SQL参数</param> 15 /// <returns>返回DataTable</returns> 16 public static DataTable GetList(string sql, params SqlParameter[] ps) 17 { 18 //创建Sql连接对象(参数:连接字符串) 19 using (SqlConnection conn = new SqlConnection(connStr)) 20 { 21 //填充DataSet和更新SQL Server数据库(一组数据命令,一个数据库连接) 22 SqlDataAdapter sda = new SqlDataAdapter(sql, conn); 23 //设置存储过程,在数据源中选择记录 24 sda.SelectCommand.Parameters.AddRange(ps); 25 //表示内存中数据的一个表 26 DataTable dt = new DataTable(); 27 //添加或刷新指定范围中的行DataSet,以匹配中使用数据源的那些DataTable名称 28 sda.Fill(dt); 29 //返回表数据 30 return dt; 31 } 32 } 33 /// <summary> 34 /// 执行SQL查询,并返回受影响的行数 35 /// </summary> 36 /// <param name="sql">SQL语句</param> 37 /// <param name="ps">SQL参数</param> 38 /// <returns>返回受影响的行数</returns> 39 public static int ExecuteNonQuery(string sql, params SqlParameter[] ps) 40 { 41 //创建Sql连接对象(参数:连接字符串) 42 using (SqlConnection conn = new SqlConnection(connStr)) 43 { 44 //表示对SQL Server数据库执行Sql语句或存储过程 45 SqlCommand cmd = new SqlCommand(sql, conn); 46 //对命令传入参数 47 cmd.Parameters.AddRange(ps); 48 //打开数据库连接 49 conn.Open(); 50 //执行Sql语句,返回受影响的行数 51 return cmd.ExecuteNonQuery(); 52 } 53 } 54 /// <summary> 55 /// 执行查询,返回结果集中第一行的第一列 56 /// </summary> 57 /// <param name="sql">SQL语句</param> 58 /// <param name="ps">SQL参数</param> 59 /// <returns>返回结果集第一行的第一列</returns> 60 public static object ExecuteScalar(string sql, params SqlParameter[] ps) 61 { 62 //创建Sql连接对象(参数:连接字符串) 63 using (SqlConnection conn = new SqlConnection(connStr)) 64 { 65 //对SQL Server数据库执行Sql语句 66 SqlCommand cmd = new SqlCommand(sql, conn); 67 //传入命令参数 68 cmd.Parameters.AddRange(ps); 69 //打开数据库 70 conn.Open(); 71 //返回结果集第一行的第一列 72 return cmd.ExecuteScalar(); 73 } 74 } 75 }
CantusDal.cs
1 /// <summary> 2 /// Lrc歌词数据层 3 /// </summary> 4 public class CantusDal : ICantusDal 5 { 6 /// <summary> 7 /// 获取数据库列表数据.Dal 8 /// </summary> 9 /// <returns></returns> 10 public List<Cantus> GetList() 11 { 12 //sql查询语句 13 string sql = "select * from cantus"; 14 //创建内存数据库表,接收查询到的数据 15 DataTable dt = SqlHelper.GetList(sql); 16 //创建List<Cantus>泛型集合 17 List<Cantus> list = new List<Cantus>(); 18 //循环遍历(DataTable每一行数据) 19 foreach (DataRow row in dt.Rows) 20 { 21 //list集合添加数据单元 22 list.Add(new Cantus() 23 { 24 Id = Convert.ToInt32(row["Id"]),//Id(id) 25 Serial = Convert.ToByte(row["Serial"]),//Serial(曲目) 26 Song = row["Song"].ToString(),//Song(歌词) 27 Album = row["Album"].ToString(),//Album(专辑) 28 Number = (short)row["Number"],//Number(张数) 29 Date = Convert.ToDateTime(row["Date"]),//Date(年份) 30 Writer = row["Writer"].ToString(),//Writer(作词) 31 Composer = row["Composer"].ToString(),//Composer(作曲) 32 Arranger = row["Arranger"].ToString(),//Arranger(编曲) 33 Singing = row["Singing"].ToString(),//Singing(原唱) 34 Lyric = row["Lyric"].ToString()//Lyric(歌词) 35 }); 36 } 37 return list; 38 } 39 /// <summary> 40 /// 添加数据 41 /// </summary> 42 /// <returns></returns> 43 public bool Add(Cantus ct) 44 { 45 string sql = "insert into cantus values(@Serial,@Song,@Album,@Number,@Date,@Writer,@Composer,@Arranger,@Singing,@Lyric)"; 46 SqlParameter[] ps = { 47 new SqlParameter("@Serial",ct.Serial), 48 new SqlParameter("@Song",ct.Song), 49 new SqlParameter("@Album",ct.Album), 50 new SqlParameter("@Number",ct.Number), 51 new SqlParameter("@Date",ct.Date), 52 new SqlParameter("@Writer",ct.Writer), 53 new SqlParameter("@Composer",ct.Composer), 54 new SqlParameter("@Arranger",ct.Arranger), 55 new SqlParameter("@Singing",ct.Singing), 56 new SqlParameter("@Lyric",ct.Lyric), 57 }; 58 return SqlHelper.ExecuteNonQuery(sql, ps) > 0; 59 } 60 }
CantusBll.cs
1 /// <summary> 2 /// 业务逻辑层.BLL 3 /// </summary> 4 public class CantusBll : ICantusBll 5 { 6 /// <summary> 7 /// 声明Dal操作对象 8 /// </summary> 9 private CantusDal ctDal; 10 /// <summary> 11 /// 构造函数() 12 /// </summary> 13 public CantusBll() 14 { 15 //创建Dal操作对象 16 ctDal = new CantusDal(); 17 } 18 /// <summary> 19 /// 返回数据层获取的数据 20 /// </summary> 21 /// <returns></returns> 22 public List<Cantus> GetList() 23 { 24 return ctDal.GetList(); 25 } 26 /// <summary> 27 /// 添加数据 28 /// </summary> 29 /// <param name="ct"></param> 30 /// <returns></returns> 31 public bool Add(Cantus ct) 32 { 33 return ctDal.Add(ct); 34 } 35 }
BackController.cs
1 public class BackController : Controller 2 { 3 CantusBll ctBll = new CantusBll(); 4 // GET: Back 5 public ActionResult Index() 6 { 7 List<Cantus> ct = ctBll.GetList(); 8 JavaScriptSerializer js = new JavaScriptSerializer(); 9 string result = js.Serialize(ct); 10 Response.Write(result); 11 return View(); 12 } 13 }