ado.net查询数据库返回datatable 电脑版发表于:2020/10/21 16:22 代码很简单,包含可以传参数 ``` public DataTable GetDataTable(string sql, SqlParameter sqlparameter = null) { using (SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123456;database=oa")) { conn.Open(); using (SqlCommand comm = new SqlCommand(sql, conn)) { //链接式查询,断开时查询 //com.ExecuteReader(); //添加条件 if (sqlparameter != null) { comm.Parameters.Add(sqlparameter); } DataTable dataTable = new DataTable(); SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(comm); sqlDataAdapter.Fill(dataTable); return dataTable; } } } ```