未定义类型'System.Data.SqlClient.SqlDataReader',请检查代码

时间:2018-07-19 04:19:15

标签: c#

  

'System.Data.SqlClient.SqlDataReader'没有定义的构造函数,   请检查代码   我无法解决这个问题

     

当我运行这段代码时,我在SqlDataReader中遇到了这个问题dr =   新的SqlDataReader();   类型'System.Data.SqlClient.SqlDataReader'没有构造函数   定义,请检查代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace PhotoGellaryy.Models
{
    public class PhotoGellary
    {

        public string strcn = 
ConfigurationManager.ConnectionStrings["UploadImagesEntities"].ToString();

        public IList<UploadImagesViewModel>GetImages()
        {
            SqlConnection con = new SqlConnection(strcn);
            List<UploadImagesViewModel> photogellary = new 
List<UploadImagesViewModel>();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select ImageID,ImageName from 
ImageUploadTbl";
                con.Open();
                SqlDataReader dr = new SqlDataReader();
                while(dr.Read())
                {
                    UploadImagesViewModel objuploadimagesviewmodel = new 
UploadImagesViewModel();
                     //objuploadimagesviewmodel.ImageID = 
dr["ImageID"].ToString();
                     objuploadimagesviewmodel.ImageName = 
dr["ImageName"].ToString();
                    photogellary.Add(objuploadimagesviewmodel);
                }
                if(dr!= null)
                {


                    dr.Dispose();
                    dr.Close();
                }
                con.Close();
                return photogellary.ToList();
            }

        }


    }
}

1 个答案:

答案 0 :(得分:0)

将行替换为:

SqlDataReader dr = cmd.ExecuteReader();

以及为什么我们不允许创建SqlDataReader的对象,所以请看一下Answer