SqlConnection con = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand(sqlstring, con);
SqlDataAdapter dr = new SqlDataAdapter(sqlstring, con);
DataSet ds = new DataSet();
dr.Fill(ds, "[Customers]");
4.把操作结果用控件dataGridView来体现出来
dataGridView1.DataSource = ds.Tables["[Customers]"];
整体代码和页面如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace _8._18
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string connstring = "integrated security = SSPI;database=doul; ";
string sqlstring = "select * from Customers";
SqlConnection con = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand(sqlstring, con);
SqlDataAdapter dr = new SqlDataAdapter(sqlstring, con);
DataSet ds = new DataSet();
dr.Fill(ds, "[Customers]");
dataGridView1.DataSource = ds.Tables["[Customers]"];
}