PDA

View Full Version : لود آخرین رکورد ثبت شده در دیتابیس توسط SQLDataReader در معماری سه لایه



bahrammzm
پنج شنبه 26 تیر 1393, 10:45 صبح
سلام دوستانمیخوام اطلاعات آخرین رکورد ثبت شده رو بریزم داخل تکست باکس .اما نمیدونم مشکل از کجاست .کدها رو بصورت کامل میذارم

//DBBASE :
public class DBBase
{
private static SqlCommand _command;
private static SqlDataReader _dataReader;

protected static SqlCommand Command
{
get { return _command; }
set { _command = value; }
}
protected static SqlDataReader DataReader
{
get { return _dataReader; }
set { _dataReader = value; }
}

}

//DBCUSTOMER :
public CustomerSet Select_Last_Customer(Customer CurrentCustomer)
{
Command = new SqlCommand("sp_LastCustomer_Select", Connection);
Command.CommandType = System.Data.CommandType.StoredProcedure;
Customer LastCustomer = new Customer();
Connection.Open();
DataReader = Command.ExecuteReader();
if (DataReader.Read())
{
LastCustomer.id = int.Parse(DataReader["id"].ToString());
LastCustomer.fname = DataReader["fname"].ToString();
}
DataReader.Close();
Connection.Close();
return LastCustomer;
}

//BUSINESS CUSTOMER :
public CustomerSet Select_Last_Customer(Customer TempCustomer)
{
return ((new DBCustomer()).Select_Last_Customer(TempCustomer));
}

//UI:
public void LoadDataTotxtCustomerFullName(Customer LastCustomer)
{
CustomerSet ShowLastCustomer = new CustomerSet();
ShowLastCustomer = (new BusinessCustomer().Select_Last_Customer(LastCustom er));
txtLastCustomerName.Text = ShowLastCustomer.ToString();
}

private void frmPhotoReg_Load(object sender, EventArgs e)
{
Customer ShowLastCustomer = new Customer();
txtLastCustomerName.Text = LoadDataTotxtCustomerFullName(ShowLastCustomer);
}


//STOREPROCEDURE:
ALTER procedure [dbo].[sp_LastCustomer_Select]
(
@Error bit out
)
as
begin
select fname,id from tblCustomers
where id = (select MAX(id) from tblCustomers)
set @Error=@@ERROR;

مهرداد صفا
پنج شنبه 26 تیر 1393, 12:35 عصر
سلام.
لطفا اول بفرمایید کجا مشکل دارید؟
به خطا بر می خورید؟ کدوم قسمت؟
نتیجه مورد نظر رو نمی گیرید؟

ro3vayedel
پنج شنبه 26 تیر 1393, 17:02 عصر
یک ردیف به table اضافه کن از نوع identi که از یک شروع بشه و یکی یکی بره بالا...
بعد برای نمایش آخرین دیتا رو بر اساس شماره ردیف بصورت نزولی (DESC) مرتب کن... حالا datagridview1.rows[0].cell[x].value.tostrig(); v میتونی استفاده کنی...و میتونی از دستور select top 1 در query استفاده کنی که فقط سطر اول رو برگشت بده...