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;

现在看出原因了吧!!

多思考,多创新,才是正道!
原文地址:https://www.cnblogs.com/shuang121/p/1969897.html