C#语句从MySQL中简单的读取数据库信息

 protected void Page_Load(object sender, EventArgs e)
    {
        //数据库+Json
        MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
        using (MySqlConnection myconn = dbhelper.Instance().getMySqlCon())
        {
            MySqlCommand mySqlCommand = dbhelper.Instance().getSqlCommand("select * from gangshan", myconn);
            myconn.Open();
            MySqlDataReader reader = mySqlCommand.ExecuteReader();
            json.asDict()["nameList"] = new MyJson.JsonNode_Array();
            while (reader.Read())
            {
                if (reader.HasRows)
                {
                    MyJson.JsonNode_Object rowsJson = new MyJson.JsonNode_Object();
                    rowsJson.asDict()["name"] = new MyJson.JsonNode_ValueString(reader["name"].ToString());
                    rowsJson.asDict()["id"] = new MyJson.JsonNode_ValueString(reader["id"].ToString());
                    rowsJson.asDict()["sex"] = new MyJson.JsonNode_ValueString(reader["sex"].ToString());
                    rowsJson.asDict()["age"] = new MyJson.JsonNode_ValueString(reader["age"].ToString());
                    json.asDict()["nameList"].AsList().Add(rowsJson);
                }
            }
            reader.Close();
            myconn.Close();
        }
        json.asDict()["state"] = new MyJson.JsonNode_ValueNumber(1);
        Response.Write(json.ToString());

       
    }

 

posted @ 2017-12-22 09:46  U3DEngineer  阅读(747)  评论(0编辑  收藏  举报