2017-01-01 187 views
0

所以它在表格中,我在网上搜索,但没有任何帮助。 这是代码。我不明白为什么它现在给我虫子,这不是以前。无效的对象名称tabl_login

我使用Visual Studio 2015 btw。

public partial class loginpage : Form 
{ 
    public loginpage() 
    { 
      InitializeComponent(); 
    } 

    // Connection String 
    string cs = @"Data Source=MS-LAPTOP\SQLEXPRESS;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"; 
    //btn_Submit Click event 

    public sealed class SecurePasswordHasher 
    { 
     /// <summary> 
     /// Size of salt 
     /// </summary> 
     private const int SaltSize = 16; 

     /// <summary> 
     /// Size of hash 
     /// </summary> 
     private const int HashSize = 20; 

     /// <summary> 
     /// Creates a hash from a password 
     /// </summary> 
     /// <param name="password">the password</param> 
     /// <param name="iterations">number of iterations</param> 
     /// <returns>the hash</returns> 
     public static string Hash(string password, int iterations) 
     { 
       //create salt 
       byte[] salt; 
       new RNGCryptoServiceProvider().GetBytes(salt = new byte[SaltSize]); 

       //create hash 
       var pbkdf2 = new Rfc2898DeriveBytes(password, salt, iterations); 
       var hash = pbkdf2.GetBytes(HashSize); 

       //combine salt and hash 
       var hashBytes = new byte[SaltSize + HashSize]; 
       Array.Copy(salt, 0, hashBytes, 0, SaltSize); 
       Array.Copy(hash, 0, hashBytes, SaltSize, HashSize); 

       //convert to base64 
       var base64Hash = Convert.ToBase64String(hashBytes); 

       //format hash with extra information 
       return string.Format("$MYHASH$V1${0}${1}", iterations, base64Hash); 
     } 

     /// <summary> 
     /// Creates a hash from a password with 10000 iterations 
     /// </summary> 
     /// <param name="password">the password</param> 
     /// <returns>the hash</returns> 
     public static string Hash(string password) 
     { 
       return Hash(password, 10000); 
     } 

     /// <summary> 
     /// Check if hash is supported 
     /// </summary> 
     /// <param name="hashString">the hash</param> 
     /// <returns>is supported?</returns> 
     public static bool IsHashSupported(string hashString) 
     { 
       return hashString.Contains("$MYHASH$V1$"); 
     } 

     /// <summary> 
     /// verify a password against a hash 
     /// </summary> 
     /// <param name="password">the password</param> 
     /// <param name="hashedPassword">the hash</param> 
     /// <returns>could be verified?</returns> 
     public static bool Verify(string password, string hashedPassword) 
     { 
       //check hash 
       if (!IsHashSupported(hashedPassword)) 
       { 
        throw new NotSupportedException("The hashtype is not supported"); 
       } 

       //extract iteration and Base64 string 
       var splittedHashString = hashedPassword.Replace("$MYHASH$V1$", "").Split('$'); 
       var iterations = int.Parse(splittedHashString[0]); 
       var base64Hash = splittedHashString[1]; 

       //get hashbytes 
       var hashBytes = Convert.FromBase64String(base64Hash); 

       //get salt 
       var salt = new byte[SaltSize]; 
       Array.Copy(hashBytes, 0, salt, 0, SaltSize); 

       //create hash with given salt 
       var pbkdf2 = new Rfc2898DeriveBytes(password, salt, iterations); 
       byte[] hash = pbkdf2.GetBytes(HashSize); 

       //get result 
       for (var i = 0; i < HashSize; i++) 
       { 
        if (hashBytes[i + SaltSize] != hash[i]) 
        { 
         return false; 
        } 
       } 
       return true; 
      } 
} 

private void button2_Click(object sender, EventArgs e) 
{ 
    //Hash 
    var hash = SecurePasswordHasher.Hash("password"); 

    //Verify 
    var result = SecurePasswordHasher.Verify("password", hash); 

    if (txtUsername.Text == "" || txt_Password.Text == "") 
    { 
     MessageBox.Show("Please provide a Username and Password"); 
     return; 
    } 

    try 
    { 
     //Create SqlConnection 
     SqlConnection con = new SqlConnection(cs); 

     SqlCommand cmd = new SqlCommand("Select * from tabl_login where [email protected] and [email protected]", con); 
     cmd.Parameters.AddWithValue("@username", txtUsername.Text); 
     cmd.Parameters.AddWithValue("@password", txt_Password.Text); 

     con.Open(); 

     SqlDataAdapter adapt = new SqlDataAdapter(cmd); 
     DataSet ds = new DataSet(); 
     adapt.Fill(ds); 

     con.Close(); 

     int count = ds.Tables[0].Rows.Count; 

     //If count is equal to 1, than show frmMain form 
     if (count == 1) 
     { 
      MessageBox.Show("Login Successful!"); 

      Form1 objFrmMain = new Form1(); 
      this.Hide(); 
      objFrmMain.ShowDialog(); 
      this.Close(); 
     } 
     else 
     { 
      MessageBox.Show("Login Failed!"); 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 

而且还有这里是截图

Visual Studios 2015

回答

0

您的连接srtring不正确。它错过了数据库名称。

// Connection String 
    string cs = @"Data Source=MS-LAPTOP\SQLEXPRESS;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"; 

将其更改为


// Connection String 
    string cs = @"Data Source=MS-LAPTOP\SQLEXPRESS;Integrated Security=True;Initial Catalog=DataBaseName;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"; 
+0

其实,这可能工作,但它给了我错误26,当我在本地执行它,而另一个没有。 – RockyBoa

+0

虽然我找到了答案。 – RockyBoa

0

你希望它是

[数据库名称]。[方案]。[表名]的

代替 tabl_login