locked
How can i bind variables to database in sql server? RRS feed

  • Question

  • User-884967499 posted

     I have designed a page using inline programming that is using strbuild.append.

    I have declared strings which i want to be fetched from the database.

    Eg. String address ="";

    strbuild.append("address is"+address);

    i want address from the database.

    Please tell me about it including establishing the connection as well

    Thursday, May 30, 2013 5:44 AM

Answers

  • User-646145796 posted

    hi,

    you can try such as the following code

                    SqlConnection conn = new SqlConnection(GetConnectionString());
                    conn.Open();
                    string qury = "select Emp_id,Password from loginuser where (name=@Emp_id) and (Password=@Password)";
                    SqlCommand cmd = new SqlCommand(qury, conn);
                    cmd.Parameters.AddWithValue("@Emp_id", name);
                    cmd.Parameters.AddWithValue("@Password", password);
                    DataTable dt = new DataTable();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);
                    if (dt.Rows.Count >= 0)
                    { 
                        string address = dt.Rows[0]["address"].ToString(); 
                    }

    you may attention following points.

    1).GetConnectionString() that return your database connection;

    2).qury  is your sqlcommand and you can user yours.

    Hope it will help you

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 2, 2013 10:53 PM

All replies

  • User-646145796 posted

    hi,

    you need to connect with sql,then get the variables  you want.

    take a look at the following link.

    http://www.codeproject.com/Articles/4416/Beginners-guide-to-accessing-SQL-Server-through-C

    Hope it can help you

    Friday, May 31, 2013 1:24 AM
  • User-884967499 posted

    Thank you , it helped me a lot.

    But can you give me an example based on the what i mentioned in my thread.

    If i select address from database then how can i bind it to the address string.

    This will  clear my query.

    Friday, May 31, 2013 1:46 AM
  • User-646145796 posted

    hi,

    you can try such as the following code

                    SqlConnection conn = new SqlConnection(GetConnectionString());
                    conn.Open();
                    string qury = "select Emp_id,Password from loginuser where (name=@Emp_id) and (Password=@Password)";
                    SqlCommand cmd = new SqlCommand(qury, conn);
                    cmd.Parameters.AddWithValue("@Emp_id", name);
                    cmd.Parameters.AddWithValue("@Password", password);
                    DataTable dt = new DataTable();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);
                    if (dt.Rows.Count >= 0)
                    { 
                        string address = dt.Rows[0]["address"].ToString(); 
                    }

    you may attention following points.

    1).GetConnectionString() that return your database connection;

    2).qury  is your sqlcommand and you can user yours.

    Hope it will help you

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 2, 2013 10:53 PM