C# 我能够从数据库中检索艺术家、姓名和流派,但不能检索价格。检索价格的代码应该是什么?

C# 我能够从数据库中检索艺术家、姓名和流派,但不能检索价格。检索价格的代码应该是什么?,c#,sql,database,C#,Sql,Database,这是其中一个按钮的示例。[我可以从数据库中检索艺术家、姓名和流派,但不能检索价格。检索价格的代码应该是什么?我已在数据库中将价格数据类型设置为varchar(50),然后尝试使用十进制和整数,但不适用于这些数据类型 protected void btnSong6_Click(object sender, EventArgs e) { string Name = "In the end"; Product Music = new Product(); string cons

这是其中一个按钮的示例。[我可以从数据库中检索艺术家、姓名和流派,但不能检索价格。检索价格的代码应该是什么?我已在数据库中将价格数据类型设置为
varchar(50)
,然后尝试使用十进制和整数,但不适用于这些数据类型

protected void btnSong6_Click(object sender, EventArgs e)
{
    string Name = "In the end";
    Product Music = new Product();
    string constr = ConfigurationManager.ConnectionStrings["RegisterConnectionString"].ConnectionString;

    SqlConnection con = new SqlConnection(constr);
    con.Open();
    string SelectCommand = "select Genre,Name,Artist, Price from Music WHERE name = '" + Name + "' ";
    SqlCommand cmd = new SqlCommand(SelectCommand, con);
    SqlDataReader read = cmd.ExecuteReader();
    read.Read();

    Music.Artist = read["artist"].ToString();
    Music.Name = read["name"].ToString();
    Music.Genre = read["genre"].ToString();
    Music.Price= read["price"].ToString();
    //ADD PRICE!!
    listMusic.Items.Add(Music.Genre + " : " + Music.Artist + " - " + Music.Name);
}

您的代码看起来很好,但您没有使用“价格”来表示任何内容。不确定您希望如何使用“价格”,或者将其添加到“listMusic”中

如果“Music.Price”是十进制,则需要将其转换为:

Music.Price = Convert.ToDecimal(read["price"]);
然后将其添加到“listMusic”:

 listMusic.Items.Add(Music.Genre + " : " + Music.Artist + " - " + Music.Name  + " $" + Music.Price);

您还应该考虑将数据库的“价格”列更改为十进制。上面的代码应该以任何方式工作。

要减少检索列值时所需的类型转换量,请参阅本文。提供了一系列方法,允许您访问其本机数据类型中的列值

比如:

        using (SqlConnection con = new SqlConnection(constr))
        {
            string SelectCommand = "select Genre,Name,Artist, Price from Music WHERE Name = '" + Name + "' ";
            SqlCommand cmd = new SqlCommand(SelectCommand, con);
            con.Open();

            SqlDataReader read = cmd.ExecuteReader();

            if (read.HasRows)
            {

                while (read.Read())
                {
                    Music.Genre = read.GetString(0);
                    Music.Name = read.GetString(1);
                    Music.Artist = read.GetString(2);
                    Music.Price = read.GetDecimal(3);

                    listMusic.Items.Add(Music.Genre + " : " + Music.Artist + " - " + Music.Name + " $" + Music.Price);
                }
            }
        }

很抱歉,我应该在上面的代码中提到这一点。我已将其添加到listMusic中,但错误在于:Music.Price=read[“Price”].ToString();错误:无法将类型字符串隐式转换为十进制。这解决了错误,但无论是否使用ToString(),它都不会显示价格.Yes在listMusic中,我已经在我的listMusic.Items中添加了它。添加(Music.Genre+“:“+Music.Artist+”-“+Music.Name+”+Music.Price);您的意思是在数据库中将其更改为十进制,然后将其添加到listMusic?当我尝试在listbox中显示它们时,会出现一个错误错误:mscorlib.dll中出现“System.FormatException”类型的异常,但您更改的代码中的用户代码未处理该异常:string constr=ConfigurationManager.ConnectionString[“RegisterConnectionString”].ConnectionString;进入:string constr=Settings.Default.connection;如果我这样做,它将在设置中显示一个错误,即:error:name设置在当前上下文中不存在检查
Product
对象,查看
Price
的定义是否为十进制。我建议您设置yo的类型将数据库中的ur
Price
字段转换为等效类型(即十进制)。