sqlDataApter与sqlcommand的关系

使用SqlDataAdapter 的时候不知道有没有想过这样的问题,为什么数据库不用打开直接就可以连接了呢?在这里我就来告诉大家原因:

  其实 数据库的连接是隐式的被打开的

SqlDataAdapter  sqlDateAdapter = new SqlDataAdapter(sql语句,connection);

 

其实它是隐式的替代了下面的代码:

conn.Open();

                SqlCommand cmd = new SqlCommand();

                cmd.CommandText = "select * from googs";

                cmd.CommandType = CommandType.Text;

                cmd.Connection = conn;

                SqlDataAdapter   sqlDateAdapter = new SqlDataAdapter();

                sqlDateAdapter.SelectCommand = cmd;

现在看出原因了吧!!

posted @ 2011-03-03 14:28  双魂人生  阅读(460)  评论(0编辑  收藏  举报