就像我的问题说的那样,我正在执行mySQL查询,但由于我试图从数据库中检索数据作为对象而使我有些头疼,因此我可以在javascript代码中重申,但是我从数据库中检索到的数据都是null和0,尽管我的数据库本身不具有null或0的值。我已将数据库值设置为不能为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}
这是我的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;
}
该命令似乎在起作用,就像我键入searchbox值(对于searchvalue)一样,就像与数据库中与musicTitle不相关的任何内容一样,都无法为我提供任何正确的信息。但是任何与musicTitle相关的东西都会返回对象数组,只是尽管数据库中有数据,但检索到的值为null和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.NoOfReports = noOfreports;
this.Type = Type;
}
另外,请改用对象初始化程序,这样您就不必编写这些荒谬的构造函数。
new music { Musicid = musicid, Type, NoOfReports = noOfreports .... };
在这种情况下,您甚至不需要构造函数。如您所见,如果变量名与属性名相同,则不必将其写为
X = Y
,而只需写为X
。因此,它就像一个构造函数,但是您不必编写实际的构造函数。