• 数据库帮助类


      新建一个类库,专门来处理数据。我觉得这个和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;
    
                }
    
            }
    
    
        }
  • 相关阅读:
    [我的阿里云服务器] —— FTP配置
    [我的阿里云服务器] —— WorkPress
    现阶段状态,三年的门槛!!!
    dubbo初体验
    SpringBoot简易搭建
    javascript数组特性
    linux修改root账户的用户名所得的教训
    记一次虚拟机无法访问主机的坑
    javascript真假(true/false)值
    Java集合基本概念及元素添加
  • 原文地址:https://www.cnblogs.com/yunquan/p/9253634.html
Copyright © 2020-2023  润新知