当前位置:Gxlcms > 数据库问题 > 将Sql查询语句获取的数据插入到List列表里面

将Sql查询语句获取的数据插入到List列表里面

时间:2021-07-01 10:21:17 帮助过:128人阅读

我想查询一个用户表的信息,该用户有姓名,密码,信息三列 //1.定义一个用户类型的List数组,userInfo类的代码在下方 List<userInfo> userInfo = new List<userInfo>(); //2.我们要读取查询语句的数据,并且保存了。这里我们将使用IDataReader语句 //数据库类的实例,类的代码在下方 DB db = new DB(); //解析方法 using(IDataReader read=db.read("select * from userInfo")) { while (read.Read()) { userInfo a = new userInfo(); a.user_Name = read[0].ToString(); a.user_Passwd = read[1].ToString(); a.user_region = read[2].ToString(); userInfo.Add(a); } }

 

userInfo类的代码:

    public class userInfo
    {      

        public string user_Name{get;set;}
        public string user_Passwd {get;set;}
        public string user_region{get;set;}
    }

 

DB类的代码:

    public  class DB
    {
   
         //数据库操作
         //1.连接数据库
         public  SqlConnection connect()
         {
        
             string rode = @"Data Source=KTY;Integrated Security=SSPI;Initial Catalog=shuyunquan";

             SqlConnection con = new SqlConnection(rode);
             con.Open();
             return con;
         }
          //执行语句的数据库方法
         public  SqlCommand command(string sql)
         {

             SqlCommand cmd = new SqlCommand(sql, connect());
             return cmd;

         }
          //行数影响的方法
         public  int Execute(string sql)
         {
             return command(sql).ExecuteNonQuery();

         }
          //返回查询结果的方法
         public SqlDataReader read(string sql)
         {
             return command(sql).ExecuteReader();
         }


    }

 

将Sql查询语句获取的数据插入到List列表里面

标签:span   连接数据库   并且   rod   ade   使用   方法   结果   定义   

人气教程排行