2017-06-22 7 views
0

私の質問と同じように、私はmySQLクエリを実行していますが、私はオブジェクトとしてデータベースからデータを取得しようとしている私のjavascriptのコードを繰り返しますが、データベースから取得するデータはすべてnullであり、データベースにもかかわらず0です。自己はnullまたは0の値を持ちません。nullにはできないデータベース値を設定しました。 これは私のデータベースの値です: You can ignore the null at the 3rd row cause it means nothingMySQLクエリデータは、データベース内のデータにもかかわらずすべてnullを与える検索します

これは私が検索したデータです。 {"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}

これは、このコマンドは、単に検索ボックスの値にどんなIキーのように動作しているようだ私の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; 
} 

ある

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#クラスでありますデータベース内のmusicTitleに関連しないもののような検索値は、正しいものは何も私に提供しません。しかし、musicTitleとの関係を持つものは、データベースにデータがあるにもかかわらず、取得された値がnullであり、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と書く必要があります。だからコンストラクタのようですが、実際のコンストラクタを書く必要はありません。

+0

幸運を学ぶ! デバッガはおそらく多くの時間を節約してくれました。開発しているプラ​​ットフォームでどのようにデバッグするかを見つけてください。 – AlexanderMP

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; 
} 
関連する問題