使用SQL的自定义控件 [英] Custom Control with SQL
本文介绍了使用SQL的自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个从SQL检索数据的控件.
我试过了
I want to create a control which retrieve data from SQL.
I tried
public partial class ComboSession : ComboBox
{
private ComboBox m_com = new ComboBox();
public ComboBox Combo1
{
get { return m_com; }
set { m_com = value; Invalidate(); }
}
public ComboSession()
{
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
try
{
string connectionString = "Data Source=";
SqlConnection database = new SqlConnection(connectionString);
database.Open();
string queryString = "select pt_session from pt_session order by pt_session";
SqlCommand cmd = new SqlCommand(queryString, database);
SqlDataReader dr = cmd.ExecuteReader();
m_com.Items.Clear();
while (dr.Read())
{
m_com.Items.Add(dr.GetValue(0).ToString());
}
database.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
这没有用.问题是什么?
This didnt work. What is the Problem???
推荐答案
除了在Paint事件处理程序中执行与绘画无关的事情外,还有什么?
为什么干嘛要那样做?
这里有很多问题:
1)您不可以在绘画事件中读取数据库-它们需要快速而清晰,否则UI会受到严重影响.
2)您需要查看连接字符串,这是一堆垃圾.
3)您甚至都不显示组合框,这是一个主要优点,因为它可能会导致您的UI在永久的绘画事件中被锁定...
What, apart from you doing non-paint related things in the Paint event handler?
Why the heck would you do something like that?
There are a number of problems here:
1) You don''t do database reading in paint events - they need to be quick and sharp or the UI is affected badly.
2) You need to look at connections strings, your is a pile of rubbish.
3) You don''t even display your combo box, which is a major advantage as it would probably cause your UI to lock up in a permanent paint event...
这篇关于使用SQL的自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文