我的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,我很确定代码是正确的。

所有输出均为Null,这应该是什么问题?

请帮我!

最佳答案

您可以使用参数化查询。

请尝试以下。

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

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


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

关于c# - ASP.Net MySqlDatabase空值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33139868/

10-13 06:28