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类:
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; }
}