C#断开式连接

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ado.net
{
public partial class 断开式连接 : Form
{
public 断开式连接()
{
InitializeComponent();
}

private void btnSelect_Click(object sender, EventArgs e)
{
//创建连接
string connString = "server=.;database=StudentMISDB;uid=sa;pwd=123456";
//创建连接对象
SqlConnection conn = new SqlConnection(connString);
//创建SQL语句和命令
string sql = "select* from Course";
SqlCommand cmd = new SqlCommand(sql,conn);
//创建适配对象
SqlDataAdapter da = new SqlDataAdapter(cmd);
//打开连接
conn.Open();
//创建数据集对象,用于存储查询的数据
DataSet ds = new DataSet();
//填充数据集,把查询的数据填充到ds中
da.Fill(ds);
//8.关闭
conn.Close();
//数据展示
foreach (DataRow item in ds.Tables[0].Rows)
{
listBox1.Items.Add(item[0].ToString() + " "+
item[1].ToString()+" "+
item[2].ToString());
}

}
}
}

原文地址:https://www.cnblogs.com/yaodengfeng/p/7448775.html