PDA

View Full Version : سوال: ارسال پارامتر به sql server ,stored procedure



CodeforLife
جمعه 12 خرداد 1396, 13:42 عصر
سلام دوستان عزیز من در برنامم برای اولین بار از معماری سه لایه استفاده کردم , و دچار خطایی میشم که هنوز نتونستم پیداش کنم ؟
موضوع از این قراره که من یه stored procedure در سمت sql دارم و باید دو تا پارامتر رو بگیره و بعد هم یه سری داده رو توی data table بریزه . ولی در زمان اینکه من داده ها رو به متد اصلی برای واکشی میدم .خطا دارم . کد ها رو میگذارم اگر متوجه اشتباه من شدین , ممنون میشم راهنماییم بفرمایید .



BL


namespace BL
{
public class Admin : User
{
//ctor
public Admin()
: base()
{
isFull = false;
}
//prop
public bool isFull { get; set; }


///
///Authenticate
///
public override void Authenticate()
{
try
{
//connect to database
base.Connect();

//Parameters
SqlParameter[] sqlParameters = new SqlParameter[2];


//username
sqlParameters[0] = new SqlParameter("@UserName", SqlDbType.NVarChar, 15);
sqlParameters[0].Value = userName;


//password
sqlParameters[1] = new SqlParameter("@Password", SqlDbType.NVarChar,6);
sqlParameters[1].Value = password;


//method to fetch with this soted procedure
base.FetchData("UserLogin", sqlParameters);


//if get value
if (dataTable.Rows.Count != 0)
{
isFull = true;//is full
}
else
{
isFull = false;//is empty / invalid data
}


}
catch (Exception ex)
{
throw ex;
}


//close
base.Disconnect();


}
}
}


DA

namespace DA
{
public class DataAccessLayer
{
private SqlConnection connection;
private SqlCommand _command;
private SqlDataAdapter _adapter;
private DataTable _dataTable;

/// <summary>
/// property
/// </summary>
public DataTable dataTable
{
get { return _dataTable; }
private set { _dataTable = value; }
}


///
///ctor() to initialize
///
public DataAccessLayer()
{
_adapter = new SqlDataAdapter();
}


///
///method to connect database
///
public void Connect()
{
connection = new SqlConnection("Server=.;Database=ContestDataBase;Trusted_Connecti on=Yes;");
if (connection.State == ConnectionState.Closed || connection.State == ConnectionState.Broken)
{
connection.Open();//open database
}
}


///
///method to close database
///
public void Disconnect()
{
connection.Close();
}


///
///method to fetch data from database
///
public DataTable FetchData(string storedProcedureName, SqlParameter[] parameters)
{
_command = new SqlCommand(storedProcedureName, connection);
dataTable = new DataTable();
_command.Parameters.AddRange(parameters);
_command.ExecuteNonQuery();
_adapter.SelectCommand = _command;
_adapter.Fill(dataTable);


return dataTable;
}




کدهای Stored Procedure
145387



خطا نیز بصورت زییر است :


145386