码迷,mamicode.com
首页 > 数据库 > 详细

数据库帮助类

时间:2018-07-02 14:48:35      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:AC   lse   sele   select   bsp   sys   add   inf   sed   

 

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

            }

        }


    }

 

数据库帮助类

标签:AC   lse   sele   select   bsp   sys   add   inf   sed   

原文地址:https://www.cnblogs.com/yunquan/p/9253634.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!