refresh database automaticaly (not datasource)

Discussion in 'C#' started by achillf, Oct 21, 2011.

  1. achillf

    achillf New Member

    Joined:
    Oct 21, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello i make 2 function which with first Datareader i read from the database and from the another i edit the database's tables. here is the functions:

    Code:
    public string DatabaseReader(string select, string read)
    {
    SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Users\\Achill\\Documents\\Visual Studio 2010\\Projects\\WindowsFormsApplication2\\WindowsFormsApplication2\\bin\\Debug\\basi.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
    SqlCommand command = conn.CreateCommand();
    command.CommandText = select;
    bool syndesh = true;
    try
    {
    conn.Open();
    }
    catch
    {
    showError("Αδύνατη σύνδεση με τη βάση δεδομένων");
    syndesh = false;
    }
    if (syndesh)
    {
    SqlDataReader reader = command.ExecuteReader();
    while (reader.Read()) return reader[read].ToString();
    conn.Close();
    
    }
    return "noData";
    }
    
    
    
    
    
    
    
    public void DatabaseChange(string write)
    {
    SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Users\\Achill\\Documents\\Visual Studio 2010\\Projects\\WindowsFormsApplication2\\WindowsFormsApplication2\\basi.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
    SqlCommand command = conn.CreateCommand();
    command.CommandText = write;
    bool syndesh = true;
    try
    {
    conn.Open();
    }
    catch
    {
    showError("Αδύνατη σύνδεση με τη βάση δεδομένων");
    syndesh = false;
    }
    if (syndesh)
    {
    command.ExecuteNonQuery();
    }
    }


    I want when i debug the program when i edit or add something from any table i want to refresh the database automaticaly and its not nesacary to close the program and open again to read it.

    here is an example to understand what i mean:

    DatabaseChange("insert into table (column1,column2) values ('data1','data2')";
    label1.Text = DatabaseReader("select data1 from table where column2='data2'","data1");

    and in the label1's text goes noData.

    How is possible to refresh and go the new data who i insert?
    i dont want datasource or datagridview if this is possible.
    if is important then tell me the way to fix it. thanks.
     
    Last edited by a moderator: Oct 21, 2011

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice