Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 连接SQL Server中的多个列_C#_Asp.net_Sql Server - Fatal编程技术网

C# 连接SQL Server中的多个列

C# 连接SQL Server中的多个列,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我有一个查询,其中我必须添加多个列 查询结果存储在DataTable中,并将其绑定到Repeater。但问题是DataTable显示的是列名,而不是我在查询中指定的名称 HTML Persons.aspx: 逻辑类: public class PersonsLogic { public DataTable GetPersons() { string Connection = ConfigurationManager.ConnectionSt

我有一个查询,其中我必须添加多个列

查询结果存储在DataTable中,并将其绑定到Repeater。但问题是DataTable显示的是列名,而不是我在查询中指定的名称

HTML Persons.aspx:

逻辑类:

public class PersonsLogic
{
        public DataTable GetPersons()
        {
            string Connection = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using(SqlConnection con = new SqlConnection(Connection))
            {
                SqlCommand cmd = new SqlCommand("spGetPersons", con);
                cmd.CommandType = CommandType.StoredProcedure;

                con.Open();
                cmd.ExecuteNonQuery();

                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);

                return dt;
            }
        }
    }
Persons.aspx.cs:


如果您使用concatatite,那么最好将每个非varchar字段强制转换为varchar,比如convertvarchar、myIntField,当然不要忘记isnull…所以我必须用IsNul编写convert?假设您有整数字段field1,而不是isnull convert varchar、field1,绑定中的名称现在与查询中的名称匹配,问题还存在吗?
select 
    FirstName as PersonName, 
    ISNULL(Designation,'') + ',' + ISNULL(Organization,'') as Post, 
    ISNULL(tblPersons.City,'') + ',' + ISNULL(tblPersons.State,'') as Location, 
    GETDATE() - tblPersons.dob + '( DoJ :' + '' + ISNULL(tblPersons.doj,'') as Age, 
    tblPersons.photo_name as Pic
from 
    tblpersons
public class PersonsLogic
{
        public DataTable GetPersons()
        {
            string Connection = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using(SqlConnection con = new SqlConnection(Connection))
            {
                SqlCommand cmd = new SqlCommand("spGetPersons", con);
                cmd.CommandType = CommandType.StoredProcedure;

                con.Open();
                cmd.ExecuteNonQuery();

                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);

                return dt;
            }
        }
    }
protected void Page_Load(object sender, EventArgs e)
{
            Persons objPerson = new Persons();
            PersonsLogic objLogic = new PersonsLogic();
            DataTable dt = objLogic.GetPersons();

            if (dt.Rows.Count > 0)
            {
                rptPersons.DataSource = dt;
                rptPersons.DataBind();
            }
            else
            {
            }
}