122-c-sharp-sql-update-command

Easier way to connect to database is

In this tutorial c sharp SQL retrieve data is explained, i.e. to retrieve data with SQL query from C#. We continue from initial database tutorial.

private void clear_all_fields()
{

txtName.Clear();
txtSurname.Clear();
txtPhone.Clear();
txtEmail.Clear();

}

private void ltbxPersons_Click(object sender, EventArgs e)
{

try
{

clear_all_fields();
int int_selected_index = ltbxPersons.SelectedIndex;
string str_sql_retrieve = “select * from PERSON where ID_PERSON = ” + alConnection[int_selected_index].ToString();
sqlCmd.Connection = sqlCn;
sqlCn.Open();
sqlCmd.CommandText = str_sql_retrieve;
SqlDataReader sqlReader = sqlCmd.ExecuteReader();
while (sqlReader.Read())
{

txtName.Text = sqlReader[“NAME”].ToString();
txtSurname.Text = sqlReader[“SURNAME”].ToString();
txtPhone.Text = sqlReader[“PHONE”].ToString();
txtEmail.Text = sqlReader[“EMAIL”].ToString();

}

sqlCn.Close();

}
catch
{
}

}

External links:

C Sharp SQL Retrieve Data on Akadia
C Sharp SQL Retrieve Data on Microsoft

Posted in c-sharp-code-examples

Leave a Reply

Your email address will not be published. Required fields are marked *

*