C#自动获取信息 - oledb访问 [英] C# automatically fetch information - oledb access
本文介绍了C#自动获取信息 - oledb访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
OleDbConnection cn = new OleDbConnection();
cn.ConnectionString = @ Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\\ \\Users\`Hp\Desktop\PROGRAM1\\\
otificationSystem\\\
otificationSystem\Database.accdb跨度>;
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = cn;
// reg();
cn.Open();
cmd.CommandText = 从新闻中选择文章npublished =' + metroDateTime1.Text + 'AND ntime =' + label4.Text + ';
// AND ntime ='+ label4.Text +'
// 标量执行数据提取
var a = cmd.ExecuteScalar();
// 此语句获取从数据库获取数据到标签的过程
if (a!= null )
{
label1.Text = + a.ToString();
// 在此语句中显示文本框内的数据
metroTextBox2.Text = label1.Text;
}
else if (metroTextBox2.Text!= < span class =code-keyword> null )
{
// 发送短信流程
}
其他 如果 (metroTextBox2.Text == null )
{
// REPEAT FETCH
}
cn.Close();
所以上面的代码有效,但我使用了一个按钮,我必须每次都按下按钮以获取信息。有没有人对我应该做什么以从数据库有任何建议-keyword>使用 datagridview并将其显示在文本框中。
我想把代码放在表单加载上,所以每次我运行程序时它都会自动获取并且 它的工作。但我不' 知道该怎么做,我试过但它赢了工作。
任何帮助 非常感谢。
我尝试了什么:
尝试使用计时器,所以每1秒就会触发一次动作。
解决方案
请按照以下说明操作:演练:连接到Access数据库中的数据( Windows窗体) [ ^ ]。然后阅读:在Windows应用程序中显示表单数据 [ ^ ]
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();
So the code above works but i use a button, and i have to press the button everytime to fetch information. Does anyone have any advice on what should i do to automatically fetch information from database using datagridview and display it on textbox.
I want to put the code on form load, so everytime i will run the program it will automatically fetch and do its job. But i don't know how to do it and i tried but it won't work.
Any help is gradly appreciated.
What I have tried:
Tried using a timer so every 1 sec it will fire an action.
解决方案
Follow these instructions: Walkthrough: Connecting to Data in an Access Database (Windows Forms)[^]. Then read this: Displaying Data on Forms in Windows Applications[^]
这篇关于C#自动获取信息 - oledb访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文