在购买物品时,需要对客户账户上的余额与物品的实际价格进行比较,因此需要把sql中的money读取出来,并进行转化。
找了好久。
结果如下
stringst1=ConfigurationManager.ConnectionStrings["yikawangluoConnectionString"].ConnectionString;

SqlConnectionConn1=newSqlConnection(st1);
Conn1.Open();
SqlCommandmycomm1=newSqlCommand("selectaccountfromuserinfowherename='"+name2+"'",Conn1);
try

...{
SqlDataReaderr=mycomm1.ExecuteReader();
r.Read();
//TextBox1.Text=((DbType)r[0]).ToString();
decimalmoney=Convert.ToDecimal(r[0]);
TextBox1.Text=money.ToString();
}
catch(SqlExceptioneeee)

...{
//Response.Redirect("error1.aspx");
}
在检索的过程中,有如下答案,采用了第一种。
数据库中的money类型对应C#的decimal类型,要解决这个问题起码有两种办法转换数据类型:
1:
使用Convert类提供的静态方法ToDecimal
decimal money = Convert.ToDecimal(this.lable1.Text.Trim());
2:
使用decimal类提供的静态方法Parse
decimal money = decimal.Parse(this.lable1.Text.Trim());
找了好久。
结果如下
stringst1=ConfigurationManager.ConnectionStrings["yikawangluoConnectionString"].ConnectionString;
SqlConnectionConn1=newSqlConnection(st1);
Conn1.Open();
SqlCommandmycomm1=newSqlCommand("selectaccountfromuserinfowherename='"+name2+"'",Conn1);
try
...{
SqlDataReaderr=mycomm1.ExecuteReader();
r.Read();
//TextBox1.Text=((DbType)r[0]).ToString();
decimalmoney=Convert.ToDecimal(r[0]);
TextBox1.Text=money.ToString();
}
catch(SqlExceptioneeee)
...{
//Response.Redirect("error1.aspx");
}
在检索的过程中,有如下答案,采用了第一种。
数据库中的money类型对应C#的decimal类型,要解决这个问题起码有两种办法转换数据类型:
1:
使用Convert类提供的静态方法ToDecimal
decimal money = Convert.ToDecimal(this.lable1.Text.Trim());
2:
使用decimal类提供的静态方法Parse
decimal money = decimal.Parse(this.lable1.Text.Trim());
本文介绍了一种从SQL数据库中读取货币值并将其转换为C# decimal类型的方法。通过使用SqlConnection和SqlCommand执行SQL查询来获取账户余额,并采用Convert.ToDecimal或decimal.Parse方法完成类型转换。
7357

被折叠的 条评论
为什么被折叠?



