简体   繁体   English

如何在特定DataTable行的其余列中添加值?

[英]How to add values in remaining columns of specific DataTable row?

I have created Restful API (JSON based API) for android application by using asp.net and SQL server. 我已经使用asp.net和SQL Server为Android应用程序创建了Restful API(基于JSON的API)。 I am succeed in performing the CURD operations from android app to sql server database via Restful web service by following this link: 通过以下链接,我成功通过Restful Web服务执行了从android应用到sql服务器数据库的CURD操作:

http://www.tutecentral.com/restful-api-for-android-part-1/ http://www.tutecentral.com/restful-api-for-android-part-1/

Now what I want is to create single web method for 2 or more related sql select queries and add the results in single datatable ie add the values in remaining columns of datatable rows on executing the second query. 现在,我想要为2个或更多相关的sql select查询创建单个web方法,并将结果添加到单个datatable中,即在执行第二个查询时在datatable行的其余列中添加值。 but my application crashes when I'm trying to do so: 但是当我尝试这样做时,我的应用程序崩溃了:

Web Service method for the above scenario is: 上述方案的Web Service方法是:

public DataTable GetStaffProfile(string userid)
{
    String faculty_id="";
    DataTable staffProfile = new DataTable();

   //Adding data to these columns on executing 1st sql query
    staffProfile.Columns.Add(new DataColumn("eid", typeof(String)));
    staffProfile.Columns.Add(new DataColumn("empid", typeof(String)));
    staffProfile.Columns.Add(new DataColumn("userid", typeof(String)));
    staffProfile.Columns.Add(new DataColumn("fname", typeof(String)));
    staffProfile.Columns.Add(new DataColumn("lname", typeof(String)));
    staffProfile.Columns.Add(new DataColumn("fathername", typeof(String)));
    staffProfile.Columns.Add(new DataColumn("dob", typeof(String)));
    staffProfile.Columns.Add(new DataColumn("nationality", typeof(String)));
    staffProfile.Columns.Add(new DataColumn("religion", typeof(String)));
    staffProfile.Columns.Add(new DataColumn("cnic", typeof(String)));
    staffProfile.Columns.Add(new DataColumn("gender", typeof(String)));
    staffProfile.Columns.Add(new DataColumn("domicile", typeof(String)));
    staffProfile.Columns.Add(new DataColumn("designame", typeof(String)));
    staffProfile.Columns.Add(new DataColumn("dname", typeof(String)));
    staffProfile.Columns.Add(new DataColumn("employmentdate", typeof(String)));

//Adding data to these columns on executing 2nd sql query
    staffProfile.Columns.Add(new DataColumn("qualification", typeof(String)));
    staffProfile.Columns.Add(new DataColumn("university", typeof(String)));
    staffProfile.Columns.Add(new DataColumn("majors", typeof(String)));
    staffProfile.Columns.Add(new DataColumn("year", typeof(String)));
    staffProfile.Columns.Add(new DataColumn("city", typeof(String)));
    staffProfile.Columns.Add(new DataColumn("country", typeof(String)));

    if (dbConnection.State.ToString() == "Closed")
    {
        dbConnection.Open();
    }
    string query = "SELECT eid, empid, userid, fname,lname, fathername, dob, nationality, religion, cnic, gender, domicile,(select title from uw_designation where desigid=uw_employee.desigid) as designame,(select dname from uw_department where "
        +"deptid=uw_employee.deptid) as dname, employmentdate FROM uw_employee where userid='"+userid+"'";
    SqlCommand command = new SqlCommand(query, dbConnection);
    SqlDataReader reader = command.ExecuteReader();
    if (reader.HasRows)
    {
        while (reader.Read())
        {
            faculty_id = reader["eid"] as string;
            staffProfile.Rows.Add(reader["eid"], reader["empid"], reader["userid"], reader["fname"], reader["lname"], reader["fathername"], reader["dob"], reader["nationality"],
                reader["religion"], reader["cnic"], reader["gender"], reader["domicile"], reader["designame"], reader["dname"],
                reader["employmentdate"]);
        }
    }
    reader.Close();


    //getting staff qualification


    string query2 = "SELECT TOP(1) eid, qualification, university, majors, year,city,country " +
                    "FROM uw_employee_education where eid='"+faculty_id+"'";
    SqlCommand command2 = new SqlCommand(query2, dbConnection);
    SqlDataReader reader1 = command2.ExecuteReader();
    if (reader1.HasRows)
    {
        while (reader1.Read())
        {
            staffProfile.Rows[0]["qualification"]=reader1["qualification"] as string;
            staffProfile.Rows[0]["university"] = reader1["university"]as string;
            staffProfile.Rows[0]["majors"] = reader1["majors"] as string;
            staffProfile.Rows[0]["year"] = reader1["year"] as string;
            staffProfile.Rows[0]["city"] = reader1["city"] as string;
            staffProfile.Rows[0]["country"] = reader1["country"] as string;
        }
    }
    reader1.Close();

    dbConnection.Close();
    return staffProfile;
}

both queries return only one result. 这两个查询仅返回一个结果。 Every kind of help is appreciated. 各种帮助都值得赞赏。 Thanks in advance. 提前致谢。

Would you try to change existing SqlCommand for second query. 您是否尝试将现有的SqlCommand更改为第二个查询。

string query2 = "SELECT TOP(1) eid, qualification, university, majors, year,city,country " +
                "FROM uw_employee_education where eid='" + faculty_id + "'";
command.CommandText = query2;
SqlDataReader reader1 = command.ExecuteReader(); 

Also, please consider the SQL injection while creating the SQL queries. 另外,在创建SQL查询时,请考虑使用SQL注入。 I suggest you to use SqlParameter . 我建议您使用SqlParameter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM