MySQL查询数据检索尽管数据库中的数据仍然为null

时间:2017-06-22 17:12:03

标签: c# mysql

所以就像我的问题所说的那样,我做了一个mySQL查询,但是当我试图从我的数据库中检索数据作为对象时,查询给了我一些头疼,所以我可以在我的javascript代码中重申我检索出来的数据从数据库全部为null和0,尽管我的数据库自己没有值为null或0.我已设置我的数据库值,它不能为null。 所以,这是我的数据库中的值: You can ignore the null at the 3rd row cause it means nothing

这是我检索到的数据。 {"Musicid":0,"Audioid":null,"Description":null,"MusicTitle":null,"AudioPath":null,"ImagePath":null,"PriceType":null,"UploadDate":"\/Date(-62135596800000)\/","Views":0,"Likes":0,"NoOfReports":0,"Type":null}

这是我的C#类

public class music{
public int Musicid { get; set; }
public String Audioid { get; set; }
public String Description { get; set; }
public String MusicTitle { get; set; }
public String AudioPath { get; set; }
public String ImagePath { get; set; }
public String PriceType { get; set; }
public DateTime UploadDate { get; set; }
public int Views { get; set; }
public int Likes { get; set; }
public int NoOfReports { get; set; }
public String Type { get; set; }

public music(int musicid, String audioid, String description, String MusicTitle, String audioPath, String imagePath, String priceType, DateTime uploadDate, int views, int likes, int noOfreports, String Type)
{
    musicid = this.Musicid;
    audioid = this.Audioid;
    description = this.Description;
    MusicTitle = this.MusicTitle;
    audioPath = this.AudioPath;
    imagePath = this.ImagePath;
    priceType = this.PriceType;
    uploadDate = this.UploadDate;
    views = this.Views;
    likes = this.Likes;
    noOfreports = this.NoOfReports;
    Type = this.Type;
}

}

这是我的c#代码

public List<music> Searchdatabase(String searchvalue)
{
    List<music> al = new List<music>();
    ArrayList array = new ArrayList();

    //sql connection, query values to database error need help
    string cs = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
    using (MySqlConnection con = new MySqlConnection(cs))
    {

        con.Open();
        String query = "SELECT music.* FROM music WHERE MusicTitle LIKE @search";
        MySqlCommand command = new MySqlCommand(query, con);
        command.Parameters.AddWithValue("@search", "%" + searchvalue + "%");
        using (MySqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                if (searchvalue == null || searchvalue == "")
                {
                    break;
                }
                else
                {
                    al.Add(new music(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetString(4), reader.GetString(5), reader.GetString(6), reader.GetDateTime(7), reader.GetInt32(8), reader.GetInt32(9) , reader.GetInt32(10), reader.GetString(11)));
                }
            }

            if (reader != null)
                reader.Close();
        }

    }
    return al;
}

该命令似乎就像我对搜索值的搜索值所做的任何操作一样,就像任何与数据库中的musicTitle无关的东西都没有提供任何正确的信息。但是任何与musicTitle有关系的东西都会返回对象数组,只是检索到的值为null,尽管在数据库中有数据,但是为0。

我知道这可能很漫长,希望有人可以帮助我。感谢

2 个答案:

答案 0 :(得分:1)

您的构造函数代码错误。您正在为构造函数参数赋值,而不是相反。

public music(int musicid, String audioid, String description, String MusicTitle, String audioPath, String imagePath, String priceType, DateTime uploadDate, int views, int likes, int noOfreports, String Type)
{
    this.Musicid = musicid;
    ....
    this.NoOfReports = noOfreports;
    this.Type = Type;
}

另外,请查看对象初始值设定项,因此您不必编写这些荒谬的构造函数。

new music { Musicid = musicid, Type, NoOfReports = noOfreports .... };

在这种情况下,您甚至不需要构造函数。如您所见,如果变量名称与属性名称相同,则您不必将其写为X = Y,而只需X。所以它就像一个构造函数,但你不必编写实际的构造函数。

答案 1 :(得分:0)

您的构造函数具有相反的顺序。您正在设置参数而不是属性。将其更改为此并重试。

public music(int musicid, String audioid, String description, String MusicTitle, String audioPath, String imagePath, String priceType, DateTime uploadDate, int views, int likes, int noOfreports, String Type)
{
    this.Musicid =musicid;
    this.Audioid = audioid;
    this.Description = description;
    this.MusicTitle = MusicTitle;
    this.AudioPath = audioPath;
    this.ImagePath = imagePath;
    this.PriceType = priceType;
    this.UploadDate = uploadDate;
    this.Views = views;
    this.Likes = likes;
    this.NoOfReports = noOfreports;
    this.Type = Type;
}