ASP.Net MySqlDatabase空值

时间:2015-10-15 04:23:45

标签: c# mysql asp.net webforms

我的ASP.Net Web窗体页面C#,

中有以下代码
 protected void Page_Load(object sender, EventArgs e)
        {
            conn.Open();
            string selectTruckQuery = "SELECT * from truck where truck_id='" + truckID + "'";
            MySqlCommand cmd = new MySqlCommand(selectTruckQuery, conn);
            foreach (DbDataRecord rowData in cmd.ExecuteReader())
            {

                if (!rowData.IsDBNull(rowData.GetOrdinal("truck_id")))
                    IdNo.Text = rowData.GetString(rowData.GetOrdinal("truck_id"));

                if (!rowData.IsDBNull(rowData.GetOrdinal("registration_no")))
                    RegistrationNo.Text = rowData.GetString(rowData.GetOrdinal("registration_no"));

                if (!rowData.IsDBNull(rowData.GetOrdinal("make")))
                    TMake.Text = rowData.GetString(rowData.GetOrdinal("make"));

                if (!rowData.IsDBNull(rowData.GetOrdinal("model")))
                    TMod.Text = rowData.GetString(rowData.GetOrdinal("model"));

                if (!rowData.IsDBNull(rowData.GetOrdinal("engine_no")))
                    TEng.Text = rowData.GetString(rowData.GetOrdinal("engine_no"));


                if (!rowData.IsDBNull(rowData.GetOrdinal("chassis_no")))
                    TChass.Text = rowData.GetString(rowData.GetOrdinal("chassis_no"));


                if (!rowData.IsDBNull(rowData.GetOrdinal("driver_name")))
                    TDriver.Text = rowData.GetString(rowData.GetOrdinal("driver_name"));

                if (!rowData.IsDBNull(rowData.GetOrdinal("driver_contact_no")))
                    TNo.Text = rowData.GetString(rowData.GetOrdinal("driver_contact_no"));

                if (!rowData.IsDBNull(rowData.GetOrdinal("status")))
                    TStat.Text = rowData.GetString(rowData.GetOrdinal("status"));

            }
            conn.Close();
        }

我想从MySqlDatabase获取值,我从MySqlDatabase获取值null,我很确定代码是正确的。

所有输出都是空的,应该是什么问题?

请帮助我!

1 个答案:

答案 0 :(得分:0)

您可以使用参数化查询。

请在下面尝试。

conn.Open();
string selectTruckQuery = "SELECT * from truck where truck_id=@truckId";

MySqlCommand cmd = new MySqlCommand(selectTruckQuery, conn);
cmd.Parameter.AddWithValue("@truckId", truckId);

根据您的字段的数据类型转换truckId。