我的sql语句是这样写的
select b.BLName,l.BLName from bscategory as b inner join BLCategory as l on b.BLID=l.BLID
题目要求我把表里面b.BLName,l.BLName值都要拿出来,我该怎么赋值勒?我在实体类中加了一列了,我赋值就出错了!!

解决方案 »

  1.   

    Ado.net?
    可以用DataAdapter取到Dataset里, 在从DataSet里取值
    string queryString = 
      "SELECT CustomerID, CompanyName FROM dbo.Customers";
    SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);DataSet customers = new DataSet();
    adapter.Fill(customers, "Customers");以上代码来自msdn:
    http://msdn.microsoft.com/en-us/library/bh8kx08z.aspx再取值
    DataTable table = customers["Customers"];
    foreach(DataRow row in table.Rows)
    {
        string customerid = row["CustomerID"];
        string companyName = row["CompanyName"];
        .....
    }以上代码手写的。
      

  2.   

    或者用SqlDataReader:private static void ReadOrderData(string connectionString)
    {
        string queryString =
            "SELECT OrderID, CustomerID FROM dbo.Orders;";    using (SqlConnection connection =
                   new SqlConnection(connectionString))
        {
            SqlCommand command =
                new SqlCommand(queryString, connection);
            connection.Open();        SqlDataReader reader = command.ExecuteReader();        // Call Read before accessing data.
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}, {1}",
                    reader[0], reader[1]));
            }        // Call Close when done reading.
            reader.Close();
        }
    }以上代码来自msdn:
    http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqldatareader(v=vs.80).aspx
      

  3.   

     public class Base
        {
            public string ID { get; set; }        public string Name { get; set; }
        }    public class BScategory : Base
        {
            public BScategory()
            {
                this.entity = new BLCategory();
            }
            public BLCategory entity { get; set; }
        }    public class BLCategory : Base
        {
            public BLCategory()
            {
                this.entity = new BScategory();
            }        public BScategory entity { get; set; }
        }
     public class BScategoryDAO
        {
            public IList<BScategory> RetrieveAll(IDataAccess dataAccess, string connstr)
            {
                IList<BScategory> lists = new List<BScategory>();
                using (IDbConnection conn = Helper.OpenConnection(dataAccess, connstr))
                {
                    string sql = string.Format("select b.BLName,l.BLName from bscategory as b inner join BLCategory as l on b.BLID=l.BLID");
                    IDataReader reader = dataAccess.ExecuteReader(conn, CommandType.Text, sql, null);
                    while (reader.Read())
                    {
                        lists.Add(Builder(reader));
                    }
                }
                return lists;
            }        private BScategory Builder(IDataReader reader)
            {
                BScategory entity = new BScategory();
                entity.entity.Name = Helper.GetString(reader, "BLName");
                entity.Name = Helper.GetString(reader, "BLName");
                return entity;
            }
      

  4.   

    你那个IDataAccess是什么意思啊
      

  5.   

    b.BLName,l.BLName列名重复查询出来的DataTable会重新命名列名,你可以改成下面代码:select BLName1=b.BLName,BLName2=l.BLName from bscategory as b inner join BLCategory as l on b.BLID=l.BLID
      

  6.   

    Util.DataAccessInterface 下面一个接口