快速简单地返回一个DataTable

void Form1_Load(object sender, EventArgs e)
{
    Bind();
}
private void Bind()
{
            

    dataGridList.DataSource = DataToTable("select * from newsInfo");
}


private  DataTable DataToTable(string sql)
{
    DataTable dt = new DataTable();
    try
    {
        using (SqlConnection Sconn = new SqlConnection(@"server=.;database=MyNewsTest;uid=sa;pwd=123"))
        {
            using (SqlDataAdapter adapter = new SqlDataAdapter(sql, Sconn))
            {
                adapter.Fill(dt);
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    return dt;
}

posted on 2022-08-25 22:50  manber  阅读(70)  评论(0)    收藏  举报

导航