如何用combobox选择sql数据库的表(这些表结构都一样,名字不一样),然后用datagridview显示出来
我用combobox选择数据库中的某一个表,然后显示在datagridview中publicpartialclassLookDate_everyDay:Form{publ...
我用combobox选择数据库中的某一个表,然后显示在datagridview中
public partial class LookDate_everyDay : Form
{
public LookDate_everyDay()
{
InitializeComponent();
}
SqlConnection con = new SqlConnection(@"server='PC-201010011231\SA';database=Water_DB;uid=sa;pwd=");
SqlDataAdapter ap = new SqlDataAdapter();
DataSet ds = new DataSet();
SqlParameter par = new SqlParameter();
DialogResult dr = new DialogResult();
public void LookDate_everyDay_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“water_DBDataSet1.Text”中。您可以根据需要移动或移除它。
this.textTableAdapter.Fill(this.water_DBDataSet1.Text);
string text1 = this.comboBox1.SelectedText;
string sql ="";
switch (this.comboBox1.SelectedText)
{
case "B10-1": sql = "select * from B101"; break;
case "B10-2": sql = "select * from B102"; break;
case "B4": sql = "select * from B4"; break;
case "B8-1": sql = "select * from B81"; break;
case "B8-2": sql = "select * from B82"; break;
default: break;
ap = new SqlDataAdapter(sql, con);
ap.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
这个是绑定,然后comboBox1_SelectedIndexChanged这个里面还要写什么呢? 展开
public partial class LookDate_everyDay : Form
{
public LookDate_everyDay()
{
InitializeComponent();
}
SqlConnection con = new SqlConnection(@"server='PC-201010011231\SA';database=Water_DB;uid=sa;pwd=");
SqlDataAdapter ap = new SqlDataAdapter();
DataSet ds = new DataSet();
SqlParameter par = new SqlParameter();
DialogResult dr = new DialogResult();
public void LookDate_everyDay_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“water_DBDataSet1.Text”中。您可以根据需要移动或移除它。
this.textTableAdapter.Fill(this.water_DBDataSet1.Text);
string text1 = this.comboBox1.SelectedText;
string sql ="";
switch (this.comboBox1.SelectedText)
{
case "B10-1": sql = "select * from B101"; break;
case "B10-2": sql = "select * from B102"; break;
case "B4": sql = "select * from B4"; break;
case "B8-1": sql = "select * from B81"; break;
case "B8-2": sql = "select * from B82"; break;
default: break;
ap = new SqlDataAdapter(sql, con);
ap.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
这个是绑定,然后comboBox1_SelectedIndexChanged这个里面还要写什么呢? 展开
1个回答
展开全部
string str = "Data Source=服务器名称;Initial Catalog=数据库名称;Integrated Security=True";
SqlConnection conn = new SqlConnection(str);
string sql = "select name from sys.sysobjects WHERE type='U'";
SqlDataAdapter ad = new SqlDataAdapter(sql, conn);
DataSet set = new DataSet();
ad.Fill(set);
comboBox1.DataSource = set.Tables[0];
comboBox1.DisplayMember = set.Tables[0].Columns[0].ColumnName;
SqlConnection conn = new SqlConnection(str);
string sql = "select name from sys.sysobjects WHERE type='U'";
SqlDataAdapter ad = new SqlDataAdapter(sql, conn);
DataSet set = new DataSet();
ad.Fill(set);
comboBox1.DataSource = set.Tables[0];
comboBox1.DisplayMember = set.Tables[0].Columns[0].ColumnName;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询