2013-09-16 122 views
3

我正在尝试为Windows窗体应用程序项目制作登录工具。我使用Visual Studio 2010和MS SQL Server 2008的从Sql Server 2008获取数据与C#

我引用这篇文章: http://www.codeproject.com/Articles/4416/Beginners-guide-to-accessing-SQL-Server-through-C

,这里是我的数据库表命名用户: enter image description here

TextBox1用户名TextBox2用于用户密码Button1用于开始登录过程。这里是我的Button1_Click方法的代码:

private void button1_Click(object sender, EventArgs e) 
{ 
    string kullaniciAdi; // user name 
    string sifre; // password 

    SqlConnection myConn = new SqlConnection(); 
    myConn.ConnectionString = "Data Source=localhost; database=EKS; uid=sa; pwd=123; connection lifetime=20; connection timeout=25; packet size=1024;"; 
    myConn.Open(); 
    try 
    { 
     SqlDataReader myReader; 
     string myQuery = ("select u_password from user where u_name='" + textBox1.Text + "';"); 
     SqlCommand myCommand = new SqlCommand(myQuery,myConn); 
     myReader = myCommand.ExecuteReader(); 
     while (myReader.Read()) 
     { 
      sifre = myReader["u_password"].ToString(); 
     } 
    } 
    catch (Exception x) 
    { 
     MessageBox.Show(x.ToString()); 
    } 
    myConn.Close(); 
} 

我没有与C#太多的经验,但我想我失去了一些东西小做是正确的。下面我分享我抓住的异常信息。你能告诉我我失踪了吗? (33行是myReader = myCommand.ExecuteReader();

enter image description here

Considerin给出答案,我更新了我的try块在下面,但它仍然无法正常工作。

try 
{ 
    SqlDataReader myReader; 
    string myQuery = ("select u_password from [user] where [email protected]"); 
    SqlCommand myCommand = new SqlCommand(myQuery, myConn); 
    myCommand.Parameters.AddWithValue("@user", textBox1.Text); 
    myReader = myCommand.ExecuteReader(); 
    while (myReader.Read()) 
    { 
     sifre = myReader["u_password"].ToString(); 
    } 

    if (textBox2.Text.Equals(sifre)) 
    { 
     Form2 admnPnl = new Form2(); 
     admnPnl.Show(); 
    } 
} 

按正弦的建议改变如下整个代码后,截图也低于: ,我想,不知何故,我不能在数据库字符串sifre分配密码。

代码:

string sifre = ""; 
var builder = new SqlConnectionStringBuilder(); 
builder.DataSource = "localhost"; 
builder.InitialCatalog = "EKS"; 
builder.UserID = "sa"; 
builder.Password = "123"; 

using (var conn = new SqlConnection(builder.ToString())) 
{ 
    using (var cmd = new SqlCommand()) 
    { 
     cmd.Connection = conn; 
     cmd.CommandText = "select u_password from [user] where u_name = @u_name"; 
     cmd.Parameters.AddWithValue("@u_name", textBox1.Text); 
     conn.Open(); 

     using (var reader = cmd.ExecuteReader()) 
     { 
      while (reader.Read()) 
      { 
       var tmp = reader["u_password"]; 
       if (tmp != DBNull.Value) 
       { 
        sifre = reader["u_password"].ToString(); 
       } 
      } 

      if (textBox2.Text.Equals(sifre)) 
      { 
       try 
       { 
        AdminPanel admnPnl = new AdminPanel(); 
        admnPnl.Show(); 
       } 
       catch (Exception y) 
       { 
        MessageBox.Show(y.ToString()); 
       } 
      } 
      else 
      { 
       MessageBox.Show("incorrect password!"); 
      } 
     } 
    } 
} 

enter image description here

+0

嗨,我附加它作为图像。在我的问题结尾 –

+0

尝试将'FROM user'更改为'FROM EKS.dbo.user' – makciook

+3

您应该使用Parameters for Queries !!! – makim

回答

2

试图把用户变成[],因为它是一个reseved关键字在T-SQL和使用参数,您的代码是开放的SQL注入!

private void button1_Click(object sender, EventArgs e) 
{ 
    var builder = new SqlConnectionStringBuilder(); 
    builder.DataSource = "servername"; 
    builder.InitialCatalog = "databasename"; 
    builder.UserID = "username"; 
    builder.Password = "yourpassword"; 

    using(var conn = new SqlConnection(builder.ToString())) 
    { 
     using(var cmd = new SqlCommand()) 
     { 
      cmd.Connection = conn; 
      cmd.CommandText = "select u_password from [user] where u_name = @u_name"; 
      cmd.Parameters.AddWithValue("@u_name", textBox1.Text); 
      conn.Open(); 

      using(var reader = cmd.ExecuteReader()) 
      { 
       while (reader.Read()) 
       { 
        var tmp = reader["u_password"]; 
        if(tmp != DBNull.Value) 
        { 
         sifre = reader["u_password"].ToString(); 
        } 
       } 
      } 
     } 
    } 
} 
+0

我根据您的建议修改了我的代码,但它不起作用。你能检查我的尝试块的最新状态吗?它在我的文章结尾。我找不到我想要的东西 –

+0

你仍然得到相同的异常或不同的异常吗? – makim

+1

虽然我不知道是什么导致问题,我已经更新了我的答案尝试此代码,如果它不工作,更新您的问题与异常(如果它不像以前一样) – makim

1

User是SQL保留关键字,你需要这样做:

select u_password from [user] where [email protected] 

与以往一样,基本的SQL问题,您应该始终使用使用参数化查询来防止人们通过te在数据库上运行任何旧命令xtbox。

SqlCommand myCommand = new SqlCommand(myQuery,myConn); 
myCommand.Parameters.AddWithValue("@user", textBox1.Text); 
1

用户在T-SQL

尝试把[]周围保留字保留字。

string myQuery = ("select u_password from [user] where u_name='" + textBox1.Text + "';"); 
4

User是在T-SQL中reserved keyword。您应该将它与方括号[User]一起使用。

而你应该使用parameterized sql来代替。这种字符串连接对于SQL Injection攻击是开放的。

string myQuery = "select u_password from [user] where [email protected]"; 
SqlCommand myCommand = new SqlCommand(myQuery,myConn); 
myCommand.Parameters.AddWithValue("@user", textBox1.Text); 

作为一般recomendation,不使用保留关键字为你的标识符和数据库中的对象的名称。

相关问题