1

I have a SQL Server Express database with tables

I use this code to store into a table

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Company Management System\DBConn.mdf;Integrated Security=True;User Instance=True;");
con.Open();

SqlCommand cmd = new SqlCommand("INSERT INTO [GATE Activity] (activity, date)VALUES ('" +"Closed" + "','" + dateTimePicker1.Value.ToString("dd/MM/yyyy hh:mm:ss tt") + "')", con);

cmd.ExecuteNonQuery();

con.Close();

but now I need to do the exact opposite. I have a table called PASSWORDS with the first column called password

I want to copy the value from the last row into a string in C# called pass from example

3
  • 2
    Based on the C# and square braces, I removed the MySQL tag. Commented Sep 28, 2014 at 17:01
  • 1
    Please tell me you're not storing passwords in clear text in your database!! Commented Sep 28, 2014 at 18:17
  • No im not ^^ FEELING EMPTY NEED MORE KNOWLEDGE Commented Sep 28, 2014 at 19:05

1 Answer 1

3

You can do this. Here is my solution, provided you have a primary key in Password table with int or identity or any field to recognize the order of record, identity is handy one, or some other column like createddate.

  SqlCommand cmd = new SqlCommand(" select top 1 password from [PASSWORDS] order by <ID> desc", con);
  SqlDataReader sdr = cmd.ExecuteReader();
  string last_pass = sdr["password"].ToString();
  if (!sdr.IsClosed) sdr.Close();

Here is the field that i was referring earlier.

Sign up to request clarification or add additional context in comments.

15 Comments

is this the way to use it coz im getting alot of errors here SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Company Management System\DBConn.mdf;Integrated Security=True;User Instance=True;"); con.Open(); SqlCommand cmd = new SqlCommand(" select top 1 password from [PASSWORDS] order by <ID> dec", con); SqlDataReader sdr = cmd.ExecuteReader(); string last_pass = sdr["password"].ToString(); if (!sdr.IsClosed) sdr.Close(); con.Close();
What are errors you are getting?, Please replace <ID> with your primary key first, Right way, is to put Connection string in Config. And then write stored proc to execute DB calls.Then put all these codes DB access layer.
Error 5 Invalid token '(' in class, struct, or interface member declaration E:\Company Management System\Form2.cs 26 33 Company Management System
sorry i forgot to ask what is <ID> key
I had told you that it should be primary key of PASSWORDS table.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.