3. ทำไมมันเปิดโปรแกรมโคตรช้าเลยครับ พอมีวิธีทำให้มันเร็วไหม ตอนนี้ผมแก้ที่ sql : select top 30 แทน ข้อมูลที่จะนำมาแสดงใน list ค้นหา มี 3-4 พันบรรทัดครับ พอจะมีวิธีไหมครับที่ทำให้ไวขึ้นกว่านี้
ขอบคุณครับได้แค่นี้น่าจะทำให้ผมไปต่อได้เยอะเลย ..
Code (C#)
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.SqlClient;
namespace MultiAutocompleteSQLC
{
public partial class Form1 : Form
{
//initialize all classes
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
//declaring variables
string query;
//int result;
public Form1()
{
InitializeComponent();
}
private void AutoSuggest()
{
try
{
//set a query for retrieving data in the database
query = "Select top 30 code,name1,name2 from bcitem";
//initialize new Sql commands
cmd = new SqlCommand();
//hold the data to be executed.
cmd.Connection = conn;
cmd.CommandText = query;
//initialize new Sql data adapter
da = new SqlDataAdapter();
//fetching query in the database.
da.SelectCommand = cmd;
//initialize new datatable
dt = new DataTable();
//refreshes the rows in specified range in the datasource.
da.Fill(dt);
int max = dt.Columns.Count;
txtMultiSuggest.AutoCompleteCustomSource.Clear();
foreach (DataRow r in dt.Rows)
{
for (int i = 0; i < max; i++)
{
//getting all rows in the specific field|Column
string rw = r.Field<string>("code").ToString()
+ " " + r.Field<string>("name1").ToString()
+ " " + r.Field<string>("name2").ToString();
//MessageBox.Show(rw);
//Set the properties of a textbox to make it auto suggest.
txtMultiSuggest.AutoCompleteMode = AutoCompleteMode.Suggest;
txtMultiSuggest.AutoCompleteSource = AutoCompleteSource.CustomSource;
//adding all rows into the textbox
txtMultiSuggest.AutoCompleteCustomSource.Add(rw);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.ActiveControl = txtMultiSuggest;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
//set a connection between SQL server and Visual C#
conn.ConnectionString = "data source=XXXXXX;initial catalog=XXXXXX;user id=sa;password=XXXXXX;";
AutoSuggest();
}
private void txtMultiSuggest_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
SqlConnection con = new SqlConnection("data source=XXXXXX;initial catalog=XXXXXX;user id=sa;password=XXXXXX;");
SqlDataAdapter oda = new SqlDataAdapter("select code,name1,name2 from bcitem where code = '" + txtMultiSuggest.Text "'", con);
con.Open();
DataTable data = new DataTable();
oda.Fill(data);
if (data.Rows.Count > 0)
{
dataGridView1.DataSource = data;
}
else
{
MessageBox.Show("ไม่พบสินค้า " + txtMultiSuggest.Text);
}
con.Close();
txtMultiSuggest.Clear();
}
}
}
}