按照下面的定义以及添加到Form中去没有问题,本机测试过using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient;namespace TestWinForm { public class MyComboBox : ComboBox { public MyComboBox() { string connectionStr = @"Data Source=XXX\SQLEXPRESS;Initial Catalog=XXX;Integrated Security=True"; object result = null; using (SqlConnection conn = new SqlConnection(connectionStr)) { SqlCommand com = new SqlCommand("SELECT PersonID FROM Person ORDER BY PersonID DESC", conn); com.Connection.Open(); result = com.ExecuteScalar(); } this.Items.Add(result); this.Items.Add(1); this.Items.Add(2); } } }this.Controls.Add(new MyComboBox());
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace TestWinForm
{
public class MyComboBox : ComboBox
{
public MyComboBox()
{
string connectionStr =
@"Data Source=XXX\SQLEXPRESS;Initial Catalog=XXX;Integrated Security=True";
object result = null;
using (SqlConnection conn = new SqlConnection(connectionStr))
{
SqlCommand com = new SqlCommand("SELECT PersonID FROM Person ORDER BY PersonID DESC", conn);
com.Connection.Open();
result = com.ExecuteScalar();
}
this.Items.Add(result);
this.Items.Add(1);
this.Items.Add(2);
}
}
}this.Controls.Add(new MyComboBox());