Write a thread:
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. xml;
Using system. Threading;
Using system. Data. sqlclient;
Namespace testform
{
Public partial class form1: Form
{
// Define a variable to close the thread
Private bool flag;
// Define a thread
Private thread;
// Define a delegate
Public Delegate void myinvoke (Dataset DS );
Public form1 ()
{
Initializecomponent ();
Flag = true;
Thread = new thread (New threadstart (threadproc ));
Thread. Start ();
}
// Thread callback function
Private void threadproc ()
{
While (FLAG)
{
// Once every three seconds
Databind ();
Thread. Sleep (3000 );
}
}
// Operate the datagridview
Private void binddatagridview (Dataset DS)
{
This. Maid = Ds. Tables ["student"];
}
// Retrieve data from the database and bind it
Private void databind ()
{
Sqlconnection con = new sqlconnection ("Server =.; database = student; uid = sa; Pwd = 0421 ");
Try
{
Con. open ();
Sqldataadapter SDA = new sqldataadapter ("select * From studentinfor", con );
Dataset DS = new dataset ();
SDA. Fill (DS, "student ");
// This. Maid = Ds. Tables ["student"];
// Bind data to the datagridview
Myinvoke MI = new myinvoke (binddatagridview );
This. begininvoke (MI, new object [] {DS });
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
Finally
{
Con. Close ();
}
}
Private void button#click (Object sender, eventargs E)
{
// Test and add a piece of data to check whether the data can be retrieved after a period of time ..
Sqlconnection con = new sqlconnection ("Server =.; database = student; uid = sa; Pwd = 0421 ");
Try
{
Con. open ();
Sqlcommand cmd = new sqlcommand ("insert into studentinforvalues ('2013', 'test')", con );
Cmd. executenonquery ();
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
Finally
{
Con. Close ();
}
}
Private void form=formclosing (Object sender, formclosingeventargs E)
{
// Close the thread before closing
Flag = false;
Thread. Join ();
}
}
}
// ================================================
Use timer to write one:
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. Data. sqlclient;
Using system. Threading;
Namespace Test
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
// Set the interval to start Timer
This. timer1.interval = 3000;
This. timer1.enabled = true;
}
// Retrieve data from the database and bind it
Private void databind ()
{
Sqlconnection con = new sqlconnection ("Server =.; database = student; uid = sa; Pwd = 0421 ");
Try
{
Con. open ();
Sqldataadapter SDA = new sqldataadapter ("select * From studentinfor", con );
Dataset DS = new dataset ();
SDA. Fill (DS, "student ");
This. Maid = Ds. Tables ["student"];
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
Finally
{
Con. Close ();
}
}
Private void button#click (Object sender, eventargs E)
{
// Test and add a piece of data to check whether the data can be retrieved after a period of time ..
Sqlconnection con = new sqlconnection ("Server =.; database = student; uid = sa; Pwd = 0421 ");
Try
{
Con. open ();
Sqlcommand cmd = new sqlcommand ("insert into studentinfor values ('20140901', 'test')", con );
Cmd. executenonquery ();
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
Finally
{
Con. Close ();
}
}
Private void timereffectick (Object sender, eventargs E)
{
// Respond every three seconds
Databind ();
}
}
}