views:

457

answers:

8

I have the following code in my btn_click event:

Sqlconnection con = new Sqlconnection("server=.;database=bss;user id=ab;pwd=ab");
con.open();
SqlCommand cmd = new Sqlcommand("select * from login where username='" 
+ txt4name.Text + "' and pwd='" + txt4pwd.Text + "'", con);

SqlDataReader reader = cmd.execute Reader();

Where login is the table and username and pwd are its fields. After this code all the values are stored in the reader object. I want to store username and pwd in the separate variables.

How can I accomplish this?

+7  A: 

You're running a huge risk of sql injection with that. Use SQL Parameters for values into SqlCommands.

ccook
Can You kindly give me any detail of SQL Parameters???
Arman
Rich and buyutec beat me to it :)
ccook
A: 
string userName =  txt4name.Text;
string password =  txt4pwd.Text;

Is that really what you want? Just to get that data into variables?

BFree
Oh yes. But If I want to retrive it from the reader, then???
Arman
+11  A: 

In general, when accessing your DB, you should be using something similar to this instead to eliminate SQL injection vulnerabilities:

using (SqlCommand myCommand = new SqlCommand("SELECT * FROM USERS WHERE USERNAME=@username AND PASSWORD=HASHBYTES('SHA1', @password)", myConnection))
    {                    
        myCommand.Parameters.AddWithValue("@username", user);
        myCommand.Parameters.AddWithValue("@password", pass);

        myConnection.Open();
        SqlDataReader myReader = myCommand.ExecuteReader())
        ...................
    }

But more realistically to store credentials, you should be using something like the Membership system instead of rolling your own.

Geoffrey Chetwood
The only problem with this is that if he were to go and use this code it would likely not return anything because I have a feeling he is not using any sort of encryption on his passwords. What you are suggesting is definitely best practices, but it might help to add in explanation on the passwords.
TheTXI
@TheTXI: That is fine, I don't want him to use that code. I want him to use the membership providers.
Geoffrey Chetwood
+1 for mentioning using an SHA1 hash. If you do roll your own, you really should be using hashes instead of storing plaintext passwords in the database.
Daniel Pryden
+4  A: 

If you mean c# variables, and if you want to get them from db, just do this:

SqlDataReader reader = cmd.execute Reader();
if (reader.Read())
{
    string username = reader["username"];
    string pwd = reader["password"];
}

While you are at it, parameterize your query and prevent sql injection:

SqlCommand cmd = new Sqlcommand("select * from login where username=@username and pwd=@pwd", con);
cmd.Parameters.AddWithValue("@username", txt4name.Text);
cmd.Parameters.AddWithValue("@pwd", txt4pwd.Text);
Serhat Özgel
You really should not be advocating this kind of credential storage.
Geoffrey Chetwood
Not advocating, just telling. We do not know if this will go into real time code or is asked just for learning purposes.
Serhat Özgel
@buyutec: It is still your responsibility to inform and educate him.
Geoffrey Chetwood
Serhat Özgel
@buyutec: Well then you deserve my downvote. I would feel ashamed of myself if I had your outlook.
Geoffrey Chetwood
+2  A: 

Definitely heed the advice about SQL injection but here is the answer to your question:

String username;
String pwd;

int columnIndex = reader.GetOrdinal("username");

if (!dataReader.IsDBNull(columnIndex))
{
    username = dataReader.GetString(columnIndex);
}

columnIndex = reader.GetOrdinal("pwd");

if (!dataReader.IsDBNull(columnIndex))
{
    pwd = dataReader.GetString(columnIndex);
}
Andrew Hare
A: 

You really need to use parameterized SQL. There's an example here Furthermore, your question doesn't really make sense; you want the username and password in seperate variables? they already are seperate in your example. If you are unable to assign them to strings I suggest following some tutorials.

Kris
A: 

You can usually find basic usage examples on MSDN, like this one for SqlDataReader.

Constantin
GIYF type of answers are really not welcome here. Either help him, or just leave it alone.
Geoffrey Chetwood
@Rich B, how is providing a link to resource containing the answer is not helping? Oh, i see, you've got a nice upvote/downvote ratio there. Good luck in your personal crusade.
Constantin
@Constantin: Just providing a link is looked down upon here. Explain your answer. Do the work.
Geoffrey Chetwood
A: 

Another approach is to load the reader results into a DataTable like so:

DataTable Result = new DataTable();

Result.Load(reader);

If your login table only contains two columns (userName and password) that are unique you end up with Result containing only one row with the information. You can then get the column values from each column:

string userName = Result.Rows[0].Field<string>("userName");
string password = Result.Rows[0].Field<string>("pwd");
Abel