sunco2008

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

企业级开发中普遍性用到数据库连接,以下为连接示例,调用sql脚本,结果集用数据窗口显示:

连接查询过程可以放在窗口的load事件中,也可以放在查询按钮中:

c# 连接访问sql server 数据库 

SqlConnection Inconn;

ConnStr = "Data Source=.\sql2008;Initial Catalog=test;User ID=sa;pwd=system";

Inconn = new SqlConnection(ConnStr);
Inconn.Open();

SqlCommand com = new SqlCommand("select * from table01",Inconn);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = com;
DataSet ds1 = new DataSet();
sda.Fill(ds1, "table01");

dataGridView1.DataSource = ds1.Tables["table01"];

 

以上;

 

posted on 2017-09-13 08:41  sunco2008  阅读(398)  评论(0编辑  收藏  举报