C# MySQL查询数据检索,尽管数据库中有数据,但所有内容都为空
正如我的问题所说,我在做一个mySQL查询,但是这个查询让我有些头疼,因为我试图从数据库中检索数据作为一个对象,这样我可以在javascript代码中重申,但是我从数据库中检索的数据都是null和0,尽管我的数据库本身没有null或0的值。我已将数据库值设置为不能为空。 这是我数据库中的值: 这是我检索到的数据。 {Musicid:0,Audioid:null,Description:null,MusicTitle:null,AudioPath:null,ImagePath:null,PriceType:null,UploadDate:\/Date-62135596800000\/,视图:0,Likes:0,NoOfReports:0,Type:null} 这是我的C班C# MySQL查询数据检索,尽管数据库中有数据,但所有内容都为空,c#,mysql,C#,Mysql,正如我的问题所说,我在做一个mySQL查询,但是这个查询让我有些头疼,因为我试图从数据库中检索数据作为一个对象,这样我可以在javascript代码中重申,但是我从数据库中检索的数据都是null和0,尽管我的数据库本身没有null或0的值。我已将数据库值设置为不能为空。 这是我数据库中的值: 这是我检索到的数据。 {Musicid:0,Audioid:null,Description:null,MusicTitle:null,AudioPath:null,ImagePath:null,Pric
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相关的内容都会返回对象数组,只要检索到的值为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。因此,它就像一个构造函数,但不必编写实际的构造函数。您的构造函数将所有内容按相反的顺序编写。您正在设置参数而不是属性。将其更改为此,然后重试
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;
}
祝你学习顺利!调试器可能会为您节省很多时间。了解如何在正在开发的平台上进行调试。