如何使用选择查询在Datagrid视图中添加检索多个数据库表? [英] How I Can Add Retrieve Multiple Database Tables In Datagrid View With Select Query?

查看:43
本文介绍了如何使用选择查询在Datagrid视图中添加检索多个数据库表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'timtableDataSet.sewagram' table. You can move, or remove it, as needed.
            this.sewagramTableAdapter.Fill(this.timtableDataSet.sewagram);
            con = new SqlConnection("server=ABHINAV-PC;database=timtable;trusted_connection=true");
            con.Open();
            ds = new DataSet();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            if (textBox3.Text == "Hutatma express")
            {
                SqlCommand cm2 = new SqlCommand("Select * from hutatma", con);
                dataGridView1.DataSource = cm2;
                cmd = new SqlCommand("select * from traintime", con);
                cmd1 = new SqlCommand("select * from traintime", con);
                trs();
            }
            else if (textBox3.Text == "Sewagram express")
            {
                SqlCommand cm1 = new SqlCommand("select * from sewagram", con);
                dataGridView1.DataSource = cm1;
                Image image1 = Image.FromFile(@"D:\asp.net practice\mindicator\mindicator\image\sevagram exp12.jpg");
                label3.Image = image1;
            }

推荐答案

您无法将命令对象分配给datagridview源。



检查这个是否有更正。



You cannot assign a command object to a datagridview source.

Check this for your correction.

private void button2_Click_1(object sender, EventArgs e)
        {
            if (textBox3.Text == "Hutatma express")
            {
                SqlCommand cm2 = new SqlCommand("Select * from hutatma", con);
                DataTable dth = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cm2);
                da.Fill(dth);
                dataGridView1.DataSource = dth;
                cmd = new SqlCommand("select * from traintime", con);
                cmd1 = new SqlCommand("select * from traintime", con);
                trs();
            }
            else if (textBox3.Text == "Sewagram express")
            {
                SqlCommand cm1 = new SqlCommand("select * from sewagram", con);
                DataTable dts = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cm2);
                da.Fill(dts);
                dataGridView1.DataSource = dts;
                Image image1 = Image.FromFile(@"D:\asp.net practice\mindicator\mindicator\image\sevagram exp12.jpg");
                label3.Image = image1;


这篇关于如何使用选择查询在Datagrid视图中添加检索多个数据库表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆