请教参数传递封装方法
请教一个封装参数的问题,谢谢了。原始代码如下:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace DataRule
{
public class DataAcc
{
string conn = ConfigurationSettings.AppSettings["DBCONN"];
/// <summary>
/// 验证用户名和密码
/// </summary>
/// <param name="users">用户</param>
/// <returns>dataset</returns>
public DataSet UserLogin(CommRule.Comm users)
{
string sql = "SELECT * FROM USERNAME WHERE USERID = @USERID AND USERPASSWORD = @USERPASS";
SqlConnection myconn = new SqlConnection(conn);//实例化连接对象
SqlDataAdapter sda = new SqlDataAdapter();//实例化适配器
sda.SelectCommand = new SqlCommand(sql,myconn);
sda.SelectCommand.Parameters.Add("@USERID",SqlDbType.NVarChar , 20,users.Username);
sda.SelectCommand.Parameters["@USERID"].Value = users.Username;
sda.SelectCommand.Parameters.Add(new SqlParameter("@USERPASS" ,SqlDbType.NVarChar , 20));
sda.SelectCommand.Parameters["@USERID"].Value = users.Username;
sda.SelectCommand.Parameters["@USERPASS"].Value = users.Password;
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
}
}
我的想法是:将
sda.SelectCommand.Parameters.Add("@USERID",SqlDbType.NVarChar , 20,users.Username);
sda.SelectCommand.Parameters["@USERID"].Value = users.Username;
sda.SelectCommand.Parameters.Add(new SqlParameter("@USERPASS" ,SqlDbType.NVarChar , 20));
sda.SelectCommand.Parameters["@USERID"].Value = users.Username;
sda.SelectCommand.Parameters["@USERPASS"].Value = users.Password;
封装成方法,直接调用 ,这样会好很多。 希望高手能够指点一下,应该怎么写,谢谢了。 --------------------编程问答--------------------
public static DataTable GetDataSet(string sql, params SqlParameter[] values)--------------------编程问答-------------------- 使用sqlhelper
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms)
{
if ((conn.State == ConnectionState.Broken) || (conn.State == ConnectionState.Closed))
{
conn.Open();
}
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = CommandType.Text;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
{
if ((parm.Direction == ParameterDirection.InputOutput || parm.Direction == ParameterDirection.Input) && (parm.Value == null))
{
parm.Value = DBNull.Value;
}
cmd.Parameters.Add(parm);
}
}
}
--------------------编程问答-------------------- 对于adp.net,能力所限,已经很好了,那个玩意也就那个用法。
如果想封装为面向对象的 DAL方法,BLL中这样调用才清晰:
--------------------编程问答-------------------- 对于adp.net,能力所限 --> 对于ado.net,能力所限
public List<User> CheckLoginUsers(CommRule.Comm users)
{
using(var trans= DALFactory.CreateInstance())
{
return (from User u in trans
where u.name==users.Username && u.password==users.Password
select u
).ToList();
}
}
如果你想使用能力比较强的面向对象方法,使用Linq to SQL或者其它基于Linq的provider.
补充:.NET技术 , ASP.NET