数据库帮助类

 

  新建一个类库,专门来处理数据。我觉得这个和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 =@id  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;

            }

        }


    }

 

posted @ 2018-07-02 14:27  蜀云泉  阅读(230)  评论(0编辑  收藏  举报