日期:2014-05-18  浏览次数:21023 次

如何提取查询出来的值
sql = "select ParentId from Tab_System_Area where AreaId=" + Aid +"";

各位大侠 如果 我要用到 ParentId 应该怎样提取出来 .. 麻烦给出详细的代码!
谢谢!

------解决方案--------------------
你返回的是啥啊?
如果是一个DataTable

dt.Rows[0]["ParentId"].ToString()
------解决方案--------------------
C# code
public string connectionString = "server=10.66.66.16;database=db;uid=sa;pwd=aa";
public static DataSet Query(string SQLString)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
                    command.Fill(ds, "ds");
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                return ds;
            }
        }
string sql = "select ParentId from Tab_System_Area where AreaId=" + Aid +""; 
dataset ds=Query(sql);
string id= ds.datatables[0].rows[0]["ParentId"].tostring();

------解决方案--------------------
这不查出来了吗~
SqlConnection con = new Sqlconnection(connectionstring);
SqlCommand com = new SqlCommand(con,sql);
SqlReader reader = com.ExecuteReader();
reader里面就读出来了

------解决方案--------------------
try
{
sql = "select ParentId from Tab_System_Area where AreaId=" + Aid +""; 

SqlCommand cmd=new SqlCommand(sql,conn);
cmd.Connecction.Open();
SqlDataReader reader = cmd.ExecuteReader();

if (reader.Read())
{
int id = (int)reader["ParentId "];

}
reader.Close();

}
catch (Exception ex)
{
Logger.LogException(LogComponentConstants.SQLServer, ex.Message);
}
finally
{
try
{
conn.Close();
}
catch { }
}