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

时间:2022-09-03 23:15:01

This is an example of one of the buttons. [I am able to retrieve the artist, name and genre from the database but not the price. What should the code be to retrieve the price? I have set the price datatype to varchar(50) in the database then tried the decimal and int and non of them work

这是其中一个按钮的示例。 [我能够从数据库中检索艺术家,名称和流派,但不能从价格中检索。代码应该是什么来检索价格?我已经在数据库中将price数据类型设置为varchar(50),然后尝试使用decimal和int,并且不使用它们

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);
}

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

1 个解决方案

#1


1  

Your code looks fine, you are not using 'price' for anything though. Not exactly sure what you would like to do with 'price, maybe add it to 'listMusic'?

您的代码看起来很好,但您并没有使用“价格”。不完全确定你想用'价格做什么,也许把它添加到'listMusic'?

If 'Music.Price'is a decimal you will need to convert it:

如果'Music.Price'是小数,则需要将其转换为:

Music.Price = Convert.ToDecimal(read["price"]);

Then add it to 'listMusic':

然后将其添加到'listMusic':

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

You should also consider changing your database 'price' column to Decimal. Above code should work either way.

您还应该考虑将数据库“price”列更改为Decimal。以上代码应该以任何方式工作。

To reduce the amount of type conversions required when retrieving column values have a look at this article. DataReader provides a series of methods allowing you to access column values in their native data types.

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

Something like:

        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);
                }
            }
        }

#1


1  

Your code looks fine, you are not using 'price' for anything though. Not exactly sure what you would like to do with 'price, maybe add it to 'listMusic'?

您的代码看起来很好,但您并没有使用“价格”。不完全确定你想用'价格做什么,也许把它添加到'listMusic'?

If 'Music.Price'is a decimal you will need to convert it:

如果'Music.Price'是小数,则需要将其转换为:

Music.Price = Convert.ToDecimal(read["price"]);

Then add it to 'listMusic':

然后将其添加到'listMusic':

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

You should also consider changing your database 'price' column to Decimal. Above code should work either way.

您还应该考虑将数据库“price”列更改为Decimal。以上代码应该以任何方式工作。

To reduce the amount of type conversions required when retrieving column values have a look at this article. DataReader provides a series of methods allowing you to access column values in their native data types.

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

Something like:

        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);
                }
            }
        }