用txt做数据源和winfrom textbox输入智能提示
1.用txt做数据源连接字符串
//text作为数据源的链接字符串
string strConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c://test;
Extended Properties=""text;HDR=Yes;FMT=Delimited"";";
注意:c://test是个文件夹,里面有相应的txt文档做数据源。
2.winfrom textbox输入智能提示
核心代码:
//下面3行用设置智能提示
this.textBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
this.textBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
this.textBox1.AutoCompleteCustomSource = ac;//设置数据源
其中 AutoCompleteStringCollection ac = new AutoCompleteStringCollection();
全部代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace TestAsSource
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
AutoCompleteStringCollection ac = new AutoCompleteStringCollection();
private void Form1_Load(object sender, EventArgs e)
{
//下面3行用设置智能提示
this.textBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
this.textBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
this.textBox1.AutoCompleteCustomSource = ac;//设置数据源
//text作为数据源的链接字符串
string strConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c://test;
Extended Properties=""text;HDR=Yes;FMT=Delimited"";";
using (OleDbConnection objConnection = new OleDbConnection(strConnection))
{
objConnection.Open();
//b.txt是c://test下面的一个文件,如果是a.txt ,只要把下面3处b.txt 换成a.txt就可以了
OleDbDataAdapter myCommandd = new OleDbDataAdapter("select * from b.txt", objConnection);
DataSet ds = new DataSet();
myCommandd.Fill(ds, "b_txt");
DataTable dt = ds.Tables["b_txt"];
for (int i = 0; i < dt.Rows.Count; i++)
{
ac.Add(dt.Rows[i][0].ToString());
}
objConnection.Close();
}
}
}
}