OleDbConnection cn = new OleDbConnection();
cn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\`Hp\Desktop\PROGRAM1\notificationSystem\notificationSystem\Database.accdb";
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = cn;
//reg();
cn.Open();
cmd.CommandText = "select article from news where npublished = '" + metroDateTime1.Text + "' AND ntime = '" + label4.Text + "'";
//AND ntime = '" + label4.Text + "'
//Scalar executes the fetching of data
var a = cmd.ExecuteScalar();
//this statement gets the process of getting the data from the database to label
if (a != null)
{
label1.Text = "" + a.ToString();
//while in this statement is displaying the data inside the textbox
metroTextBox2.Text = label1.Text;
}
else if (metroTextBox2.Text != null)
{
//SEND SMS PROCESS
}
else if (metroTextBox2.Text == null)
{
//REPEAT FETCH
}
cn.Close();
所以上面的代码可以工作,但是我使用了一个按钮,我每次都要按下按钮来获取信息。有没有人对如何使用datagridview自动从数据库中获取信息并将其显示在文本框上有任何建议?
我想把代码放在表单加载上,所以每次我运行程序时它都会自动获取并完成它的工作。但是我不知道该怎么做而且我尝试过但它不会起作用。
任何帮助都会受到高度赞赏。