当前位置:编程学习 > C#/ASP.NET >>

ASP.NET数据库查询代码问题

select * from dbo.Nop_Budget as b,dbo.Nop_Project as p,dbo.Nop_ProjectOrder as pr where b.ProjectId=p.ProjectId and b.ProjectId=pr.ProductID    这是SQL数据库查询代码,我需要取里面的列名的数据放到GirdView里面,请问怎么写代码?
答案://创建一个你需要提取所有列的一个 类 Patient 如下:

public class Patient
{
 
    string Name;

    public string Name1
    {
        get { return Name; }
        set { Name = value; }
    }
    string Sex;

    public string Sex1
    {
        get { return Sex; }
        set { Sex = value; }
    }
    string Office;

    public string Office1
    {
        get { return Office; }
        set { Office = value; }
    }
    string DoctorName;

    public string DoctorName1
    {
        get { return DoctorName; }
        set { DoctorName = value; }
    }
    string Hospital;

    public string Hospital1
    {
        get { return Hospital; }
        set { Hospital = value; }
    }

//再创建一个类 类里面写个返回类型Patient类集合的方法

 

 

  public List<Patient> GetInfo(string a, string b)
    {
        List<Patient> ls = new List<Patient>();
        string con = "Data Source=.;Initial Catalog=PatientsDb;Integrated Security=True";
        SqlConnection connection = new SqlConnection(con);
        connection.Open();//访问数据库
        string sqlcon = "select a.PName,a.Sex , a.Office, b.DoctorName ,b.Hospital from patientsinfo as a JOIN sickinfo  as b ON a.PName =b.PatentName where  " + a + " like '%" + b + "%'";//需要查询的语句
        SqlCommand com = new SqlCommand(sqlcon, connection);
        SqlDataReader reader = com.ExecuteReader();//执行
        while (reader.Read())//读取数据
        {
            Patient pa = new Patient();//创建Patient 对象
            pa.Name1 = (string)reader[0];//对象属性一一对应赋值
            pa.Sex1 = (string)reader[1];
            pa.Office1 = (string)reader[2];
            pa.DoctorName1 = (string)reader[3];
            pa.Hospital1 = (string)reader[4];
            ls.Add(pa);//添加到集合
        }
        connection.Close();
        return ls;返回集合
    }

 

生成

 

然后在

 GirdView也没拖个Sqldatasource控件进行第二类的方法绑定·

最后将GirdView绑定到Sqldatasource上就好了

你的代码也类似一样的

我是自己代码复杂过来加点注释的··自己研究吧··

好麻烦··多给我点分······

 

上一个:asp.net有哪些传值方式?
下一个:asp.net和java的书

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,