不使用链接字符串链接数据库,何解?
V0lD 2009-12-25 11:04:46 如何不使用链接字符串链接数据库:(这里是登陆界面里的相关代码)
如下代码:
namespace friendship
{
#region 数据库连接类
class Fconnection
{
public SqlConnection SQlconnection()//数据库连接
{
SqlConnection con = new SqlConnection();
Properties.Settings s = Properties.Settings.Default;
con.ConnectionString = s.sqlconnection;
return con;
}
}
#endregion
}
namespace friendship
{
class use
{
private SqlConnection cn = null;
public SqlDataReader dr = null;
#region//登录信息
public int load(userload user)
{
int intfal = 0;
try
{
Fconnection fcn = new Fconnection();
cn = fcn.SQlconnection();
cn.Open();
//str = "select userName,userPassword from user where userName='" + user.User + "'and userpasswoed'" + user.Pwd + "'";
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.Parameters.Add("@userName", SqlDbType.Char);
cmd.Parameters["@userName"].Value = user.User;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "P_load";
dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
intfal = 1;
}
return intfal;
}
catch (Exception ex)
{
return 0;
}
#endregion
}
}
}
在登陆界面的"登陆"按钮中调用这两个类,通过判断use类返回的值,来确定是否链接到数据库,从而进入登陆界面后的操作的.
请问,为何?即没用到链接字符串就能链接数据库了.