我已經在數據訪問類中創建了一個方法,用參數從數據庫中選擇數據。我只想使用參數化查詢。如何在參數化查詢的數據訪問類中創建方法?
方法是:
public DataTable executeSelectQuery(String _query, SqlParameter[] sqlParameter)
{
SqlCommand myCommand = new SqlCommand();
DataTable dataTable = new DataTable();
dataTable = null;
DataSet ds = new DataSet();
try
{
myCommand.Connection = openConnection();
myCommand.CommandText = _query;
myCommand.Parameters.AddRange(sqlParameter);
myCommand.ExecuteNonQuery();
myAdapter.SelectCommand = myCommand;
myAdapter.Fill(ds);
dataTable = ds.Tables[0];
}
catch (SqlException e)
{
return null;
}
finally
{
myCommand.Connection = CloseConnection();
}
return dataTable;
}
,但我不明白如何使用這種方法來獲取數據,以及如何傳遞參數?
我的查詢可能是"select password from tblUsers where [email protected]"
如何在業務層傳遞@email?
如何使數據訪問類中的方法獲得標量值?
public string getpasswrd(string unm)
{
con.Open();
string cpaswrd;
cmd1 = new SqlCommand("select password from tbl_login where username='" + unm + "'", con);
cpaswrd = (String)cmd1.ExecuteScalar();
con.Close();
return cpaswrd;
}