使用ADO.NET访问数据库

来源:百度知道 编辑:UC知道 时间:2024/05/13 02:47:46
使用ADO.NET访问数据库,某程序员编写了如下几行代码,你觉得错误的应该是第()行. (选择一项)
OleDbCommand oleCmd=new OleDbCommand(); //1
OleDbConnection oleCon=new
OleDbConnection(“Server=SQLDB;DataBase=pubs;uid=sa;pwd=pwd”);
oleCmd.Connection=oleCon; //2
oleCmd.Connection.Open();
oleCmd.CommandText="select * from authors";
oleCmd.CommandType=CommandType.StoredProcedure; //3
oleCmd.ExecuteScalar(); //4
oleCmd.Connection.Close();
A) 1
B) 2
C) 3
D) 4

C) 3
StoredProcedure是调用存储过程的

3,应该是CommandType.Text

2行,先open,再附给连接对象。

oleCmd.ExecuteScalar(); //这个命令自动关闭Connection

oleCmd.Connection.Close(); //这里会报错

第三行