如何不使用链接字符串链接数据库:(这里是登陆界面里的相关代码)
如下代码:
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类返回的值,来确定是否链接到数据库,从而进入登陆界面后的操作的.请问,为何?即没用到链接字符串就能链接数据库了.

解决方案 »

  1.   

    SqlConnection con = new SqlConnection(); 
                Properties.Settings s = Properties.Settings.Default; 
                con.ConnectionString = s.sqlconnection; 
      

  2.   

    那是因为你的字符串在config 里面写好了
      

  3.   

    你打印出s.sqlconnection就知道是啥了
      

  4.   

    谁说没有用呢
    Properties.Settings s = Properties.Settings.Default; 
    s.sqlconnection这个就是
      

  5.   

     public SqlConnection SQlconnection()//数据库连接 
            { 
                SqlConnection con = new SqlConnection(); 
                Properties.Settings s = Properties.Settings.Default; //这个啊
                con.ConnectionString = s.sqlconnection; //.........
                return con; 
            } ............