创建数据库连接类Dbconn
namespace datagridview
{
class Dbconn
{
public static SqlConnection sqlconn()
{
string sqlconstr = "Data Source=WWW-HP\\ME;Initial Catalog=VV;Integrated Security=True";
SqlConnection sqlconn = new SqlConnection(sqlconstr);
return sqlconn;
}
}
}
实现数据读取并显示。数据库可以不用打开,SqlDataAdapter能够自动打开。
namespace datagridview
{
public partial class Form1 : Form
{
SqlConnection conn = ();
SqlDataAdapter sda;
SqlCommand scmd;
DataSet ds;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlCommand scmd = new SqlCommand("select * from [table]", conn);
SqlDataAdapter sda = new SqlDataAdapter(scmd);
ds = new DataSet();
(ds,"Table");
= ["Table"];
();
}
}