c# - 在 C# 中向 SQL Server 添加十进制数
问题描述
我正在开发一个订购程序。订购时无法将产品价格添加到数据库中。价格是十进制的,如 24.99。
我的代码;
int table = Convert.ToInt32(lblTableNo.Text), product = Convert.ToInt32(formLogin.readData("select productID from tbProducts where productName='" + lblProduct.Text + "'")), piece = Convert.ToInt32(lblPiece.Text);
float price = piece * float.Parse(formLogin.readData("select productPrice from tbProducts where productID=" + product));
string time = System.DateTime.Now.ToShortTimeString();
formLogin.runSql("insert into tbOrders (orderTable, orderUser, orderTime, orderProduct, orderPiece, orderPrice) values (" + table+ "," + formLogin.signer+ ",'" + time + "'," + product + "," + piece + "," + price + ")");
价格变量的值是“25.5”,而函数是“25,5”。这就是sql查询失败的原因。
我试图将价格变量转换为十进制。但结果并没有改变。
运行Sql代码;
public static bool runSql(string sql)
{
SqlConnection cnn= connect();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
cmd.Connection.Close();
cnn.Close();
cmd.Dispose();
return true;
}
读取数据代码;
public static string readData(string sql)
{
SqlConnection cnn= connect();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
cmd.CommandText = sql;
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
return dr[0].ToString();
dr.Close();
cnn.Close();
}
解决方案
推荐阅读
- sql-server - 如何使用 T-SQL 在一个查询结果中获得多选
- python - 仅在数据框中提取最小值
- c# - 如何使用相同的过滤器同时过滤集合及其嵌套集合?
- c# - Application Insight 中的范围日志记录
- python - C++ DLL 中的 Python 对象
- google-api - Google 登录同意页面未针对我的应用程序请求的所有范围请求许可
- c++ - Windows 控制台调整大小被忽略 - 屏幕缓冲区搞砸了
- python - for循环中某些值的条件(Python pandas)
- java - 发布密钥无法与已部署的应用程序一起使用
- javascript - 访问 localstorage 中的 `access_token` 属性