C#中comboBox如何绑定数据库
这是窗体,我想让日记分类里的comboBox1和数据表article中的article_item绑定,然后文章列表下的listbox1中可以显示上面分类下的文章,数据是a...
这是窗体,我想让日记分类里的comboBox1和数据表article中的article_item绑定,然后文章列表下的listbox1中可以显示上面分类下的文章,数据是article中的article_name 因为我是初学者,所以很多的都不太懂。。
我的代码附上:
public partial class article: Form
{
string connString = "server=DELL20130905PJ;database=Blog;uid=sa;pwd=w19960413";
SqlDataReader dataReader;
SqlDataAdapter dataAdapter;
SqlConnection connection;
SqlCommand command;
string id;
public article()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
connection = new SqlConnection(connString);
string cid = comboBox1.SelectedValue.ToString();
string sql = string.Format("Select item from article where article_ID=[0]", cid);
try
{
dataAdapter = new SqlDataAdapter(sql,connection);
DataSet da = new DataSet();
dataAdapter.Fill(da);
comboBox1.DataSource = da.Tables[0];
comboBox1.ValueMember = "article_ID";
comboBox1.DisplayMember = "item";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "操作数据库出错!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
connection.Close(); // 关闭数据库连接
}
}
}
拜托拜托~~~ 展开
我的代码附上:
public partial class article: Form
{
string connString = "server=DELL20130905PJ;database=Blog;uid=sa;pwd=w19960413";
SqlDataReader dataReader;
SqlDataAdapter dataAdapter;
SqlConnection connection;
SqlCommand command;
string id;
public article()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
connection = new SqlConnection(connString);
string cid = comboBox1.SelectedValue.ToString();
string sql = string.Format("Select item from article where article_ID=[0]", cid);
try
{
dataAdapter = new SqlDataAdapter(sql,connection);
DataSet da = new DataSet();
dataAdapter.Fill(da);
comboBox1.DataSource = da.Tables[0];
comboBox1.ValueMember = "article_ID";
comboBox1.DisplayMember = "item";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "操作数据库出错!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
connection.Close(); // 关闭数据库连接
}
}
}
拜托拜托~~~ 展开
展开全部
你把你的数据代码放在 comboBox1_SelectedIndexChanged里面是干什么啊,你这个永远都不会执行的!!
把你的
connection = new SqlConnection(connString);
string cid = comboBox1.SelectedValue.ToString();
string sql = string.Format("Select item from article where article_ID=[0]", cid);
try
{
dataAdapter = new SqlDataAdapter(sql,connection);
DataSet da = new DataSet();
dataAdapter.Fill(da);
comboBox1.DataSource = da.Tables[0];
comboBox1.ValueMember = "article_ID";
comboBox1.DisplayMember = "item";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "操作数据库出错!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
connection.Close(); // 关闭数据库连接
}
这段代码放在窗体的load事件里面去
把你的
connection = new SqlConnection(connString);
string cid = comboBox1.SelectedValue.ToString();
string sql = string.Format("Select item from article where article_ID=[0]", cid);
try
{
dataAdapter = new SqlDataAdapter(sql,connection);
DataSet da = new DataSet();
dataAdapter.Fill(da);
comboBox1.DataSource = da.Tables[0];
comboBox1.ValueMember = "article_ID";
comboBox1.DisplayMember = "item";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "操作数据库出错!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
connection.Close(); // 关闭数据库连接
}
这段代码放在窗体的load事件里面去
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |