数据库帮助类
Posted yunquan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库帮助类相关的知识,希望对你有一定的参考价值。
新建一个类库,专门来处理数据。我觉得这个和EF的思想是差不多的,等有时间我好好研究一下EF,以后就不再用自己写sql了
public class SqlHelper { string connstr = ConfigurationManager.ConnectionStrings["CONNECTIONS"].ConnectionString; //返回Table public DataTable SqlConnectionInformation(string sql) { DataTable dt = new DataTable(); using (SqlConnection conn = new SqlConnection(connstr)) { if (conn.State == ConnectionState.Closed) { conn.Open(); } //string sql = "select * from table"; SqlCommand com = new SqlCommand(sql, conn); SqlDataAdapter adapter = new SqlDataAdapter(com); adapter.Fill(dt); } return dt; } //判断登录信息的 public string SqlQuery(string id, string pword) { string sql = "select* from sysUser where AccountNumber [email protected] and Password = @pword"; using (SqlConnection conn = new SqlConnection(connstr)) { if (conn.State == ConnectionState.Closed) { conn.Open(); } SqlCommand com = new SqlCommand(sql, conn); SqlParameter[] parameters = { new SqlParameter("@id",SqlDbType.VarChar,12), new SqlParameter("@pword",SqlDbType.VarChar,12) }; parameters[0].Value = id; parameters[1].Value = pword; com.Parameters.AddRange(parameters); if (com.ExecuteScalar()!=null) { string user_Name = com.ExecuteScalar().ToString(); if (user_Name == id) { return "yes"; } else { return "no"; } } else { return "no"; } } } //增删改 public int Excute(string sql) { using (SqlConnection conn = new SqlConnection(connstr)) { if (conn.State == ConnectionState.Closed) { conn.Open(); } //string sql = "select * from table"; SqlCommand com = new SqlCommand(sql, conn); int result = com.ExecuteNonQuery(); return result; } } }
以上是关于数据库帮助类的主要内容,如果未能解决你的问题,请参考以下文章
调用模板化成员函数:帮助我理解另一个 *** 帖子中的代码片段