C#将数据库数据取出后存放在ArrayList数组里的方法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
string M_str_sqlstr = "SQL查询语句";

            SqlConnection sqlcon = this.getcon();
            SqlDataAdapter sqlda = new SqlDataAdapter(M_str_sqlstr,sqlcon);
            DataSet myds = new DataSet();
            sqlda.Fill(myds,M_str_sqlstr);
            DataTable dt = myds.Tables[0];
            foreach (DataRow row in dt.Rows)
            {
                Client client = new Client();
                client.ClientID = (int)row["ClientID"];
                client.CName = (string)row["CName"];
                clientlist.Add(client);
            }

//数据库连接
private SqlConnection getcon()
        {
            string connString = "Data Source=.;Initial Catalog=Csharp;Integrated Security=True";
            SqlConnection connection = new SqlConnection(connString);
            return connection;
        }

Client类:

1
2
3
4
5
6
7
public class Client
    {
        private int clientID;
        public int ClientID { get => clientID; set => clientID = value; }
		private string cName;
		public string CName { get => cName; set => cName = value; }
}
Licensed under CC BY-NC-SA 4.0