public string Login(string userName, string password) { string customerID; // params to stored proc Database data = new Database(); SqlParameter[] prams = { data.MakeInParam("@username", SqlDbType.VarChar, 25, password), data.MakeInParam("@password", SqlDbType.VarChar, 25, userName), data.MakeOutParam("@CustomerID", SqlDbType.VarChar, 25) }; // create data object and params data.RunProc("upAccountLogin", prams); // run the stored procedure customerID = (string) prams[2].Value; // get the output param value // if the customer id is an empty string, then the login failed if (customerID == string.Empty) return null; else return customerID; }
public int RunProc(string procName) { SqlCommand cmd = CreateCommand(procName, null); cmd.ExecuteNonQuery(); this.Close(); return (int)cmd.Parameters["ReturnValue"].Value; } private SqlCommand CreateCommand(string procName, SqlParameter[] prams) { // make sure connection is open Open(); //command = new SqlCommand( sprocName, new SqlConnection( ConfigManager.DALConnectionString ) ); SqlCommand cmd = new SqlCommand(procName, con); cmd.CommandType = CommandType.StoredProcedure; // add proc parameters if (prams != null) { foreach (SqlParameter parameter in prams) cmd.Parameters.Add(parameter); }
string customerID; // params to stored proc
Database data = new Database();
SqlParameter[] prams = {
data.MakeInParam("@username", SqlDbType.VarChar, 25, password),
data.MakeInParam("@password", SqlDbType.VarChar, 25, userName),
data.MakeOutParam("@CustomerID", SqlDbType.VarChar, 25)
}; // create data object and params
data.RunProc("upAccountLogin", prams); // run the stored procedure
customerID = (string) prams[2].Value; // get the output param value // if the customer id is an empty string, then the login failed
if (customerID == string.Empty)
return null;
else
return customerID;
}
SqlCommand cmd = CreateCommand(procName, null);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters["ReturnValue"].Value;
}
private SqlCommand CreateCommand(string procName, SqlParameter[] prams) {
// make sure connection is open
Open(); //command = new SqlCommand( sprocName, new SqlConnection( ConfigManager.DALConnectionString ) );
SqlCommand cmd = new SqlCommand(procName, con);
cmd.CommandType = CommandType.StoredProcedure; // add proc parameters
if (prams != null) {
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
// return param
cmd.Parameters.Add(
new SqlParameter("ReturnValue", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, false, 0, 0,
string.Empty, DataRowVersion.Default, null)); return cmd;
}
你的工程名.你的类库名.类里面的方法名(参数)除了把LOGIN写到类里,把登录框做成用户控件也挺方便,方便每个页面调用.