古詩詞大全網 - 成語用法 - C#將執行後的SQL查詢結果輸出到dataGridView控件裏

C#將執行後的SQL查詢結果輸出到dataGridView控件裏

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace Systems

{

public partial class Form6 : Form

{

public Form6()

{

InitializeComponent();

}

private void Form6_Load(object sender, EventArgs e)

{

SqlConnection con = new SqlConnection("server=.;database=student;trusted_connection=sspi");

SqlCommand cm = new SqlCommand();

cm.Connection = con;

cm.CommandText = "select * from studentInfo";

con.Open();

SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = cm;

DataSet ds = new DataSet();

da.Fill(ds, "student");

dataGridView1.DataSource = ds.Tables["student"];

}

}

}