using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// Summary description for Transaction
/// </summary>
public class Transaction
{
private string connStr = ConfigurationManager.ConnectionStrings["airmazin"].ConnectionString;
private string _TransactionID;
private string _OfferId;
private string _UserName;
private string _Image;
private string _ProductName;
private decimal _ProductPrice;
public Transaction()
{
this.TransactionID = null;
this.OfferId = null;
this.UserName = null;
this.Image = null;
this.ProductName = null;
this.ProductPrice = 0;
//
// TODO: Add constructor logic here
//
}
public Transaction(string p_TransactionID, string p_OfferId, string p_UserName, string p_Image, string p_ProductName, decimal p_ProductPrice)
{
this.TransactionID = p_TransactionID;
this.OfferId = p_OfferId;
this.UserName = p_UserName;
this.Image = p_Image;
this.ProductName = p_ProductName;
this.ProductPrice = p_ProductPrice;
}
private string TransactionID
{
get { return _TransactionID; }
set { _TransactionID = value; }
}
private string OfferId
{
get { return _OfferId; }
set { _OfferId = value; }
}
private string UserName
{
get { return _UserName; }
set { _UserName = value; }
}
private string Image
{
get { return _Image; }
set { _Image = value; }
}
private string ProductName
{
get { return _TransactionID; }
set { _ProductName = value; }
}
private decimal ProductPrice
{
get { return _ProductPrice; }
set { _ProductPrice = value; }
} //getter and setter
public Transaction getProductTransaction(string p_UserName)
{
Transaction prodDetail = null;
string tran_ID, offer_ID, user, Prod_Image, prod_Name;
decimal prod_Price;
SqlConnection conn = new SqlConnection(connStr);
string queryStr = "SELECT * FROM Transaction WHERE UserName = @user";
// think the naming convention is wrong
SqlCommand cmd = new SqlCommand(queryStr, conn);
cmd.Parameters.AddWithValue("@user", p_UserName);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
tran_ID = dr["TransactionID"].ToString();
offer_ID = dr["OfferId"].ToString();
user = dr["UserName"].ToString();
Prod_Image = dr["Image"].ToString();
prod_Name = dr["ProductName"].ToString();
prod_Price = decimal.Parse(dr["ProductPrice"].ToString());
prodDetail = new Transaction(tran_ID, offer_ID, user, Prod_Image, prod_Name, prod_Price);
}
else
{
prodDetail = null;
}
conn.Close();
dr.Close();
dr.Dispose();
return prodDetail;
}
对于sql不确定。刚开始编程,目前是一名学生。遇到此错误,提示附近关键字“事务”。命名惯例可能是错误的,可能需要一些帮助。想要在aspx页面上显示交易记录,所以我正在执行getProductTransaction(){}方法,但是似乎有问题
编辑(通过评论)
现在我希望它显示交易列表,而不是单个交易
答案 0 :(得分:3)
由于表名称“ Transaction”是关键字,可能会出现问题。 您可以尝试将其作为查询:
string queryStr = "SELECT * FROM [Transaction] WHERE UserName = @user";
答案 1 :(得分:1)
评论和解释是内联的。 using
块可确保IDisposable
对象被关闭并正确处理,即使有错误也是如此。
public class Transaction
{
private string connStr = ConfigurationManager.ConnectionStrings["airmazin"].ConnectionString;
public Transaction() { }
//Auto-Implemented Properties in C# 3 and later
//The standard get and set are written for you by the compiler
//Also the compiler writes the private fields where the data is stored
public string TransactionID { get; set; }
public string OfferId { get; set; }
public string UserName { get; set; }
public string Image { get; set; }
public string ProductName { get; set; }
public decimal ProductPrice { get; set; }
//changed the function to return a list
public List<Transaction> getProductTransaction(string p_UserName)
{
List<Transaction> lst = new List<Transaction>();
using (SqlConnection conn = new SqlConnection(connStr))
{
string queryStr = "SELECT * FROM [Transaction] WHERE UserName = @user";
using (SqlCommand cmd = new SqlCommand(queryStr, conn))
{
cmd.Parameters.AddWithValue("@user", p_UserName);
conn.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.HasRows)
while (dr.Read())
//This loop keeps adding records to the list as long as there are records to read
{
Transaction prodDetail = new Transaction();
prodDetail.TransactionID= dr["TransactionID"].ToString();
prodDetail.OfferId = dr["OfferId"].ToString();
prodDetail.UserName = dr["UserName"].ToString();
prodDetail.Image = dr["Image"].ToString();
prodDetail.ProductName = dr["ProductName"].ToString();
prodDetail.ProductPrice = decimal.Parse(dr["ProductPrice"].ToString());
lst.Add(prodDetail);
}
else
{
lst = null;
}
}
}
}
return lst;
}
}