我试图得到一个SQL进入C#,然后设置项值一个十进制 - 价格。试图获得SQL进入C#
我在写我的SQL项的结果控制台,然后尝试再次读取它,但它只会让我读它作为INT。
{
public partial class Purchase : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void purchaseButton_Click(object sender, EventArgs e)
{
SqlConnection sqlConnection1 = new SqlConnection("Data Source=DELETED);
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
string url = HttpContext.Current.Request.Url.AbsoluteUri;
char ID = url[url.Length - 1];
cmd.CommandText = "SELECT Price FROM customerData where ProductId =" + ID;
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
ReadSingleRow((IDataRecord)reader);
}
int Price = Console.Read();
PriceTest.Text = Price.ToString();
reader.Close();
sqlConnection1.Close();
}
private static void ReadSingleRow(IDataRecord record)
{
Console.WriteLine(String.Format("{0}, {1}", record[0], record[1]));
}
我有意删除了我的数据源,因为很明显的原因。
我试图有一些代码是alongs线:
Decimal Price = Console.Read();
我不知道,如果这个方法在检索从SQL数据,因为我没有测试它的工作方式。如果任何人都可以点亮一下如何做到这一点,非常感谢,对于英语不好,也不好意思,这不是我的第一语言。通过将数据转储到控制台,然后回读谢谢
'Console'无关,与你的SQL操作的。如果你想要一行中的单个列,然后查找'ExecuteScalar'。 – Crowcoder
请仔细研究SQL注入攻击,因为您的代码当前很脆弱......我希望页面名称为“Purchase”,这是用于学习的目的,而不是用于处理付款的真实应用程序? – Milney
不,这只是为了学习的目的,但感谢您将此引起我的注意 –