如何在同一数据库中的两个表之间切换,以便在C#中的Gridview中显示数据? [英] How Can I Switch Between Two Tables In Same Database For Displaying The Data In A Gridview In C# ?

查看:70
本文介绍了如何在同一数据库中的两个表之间切换,以便在C#中的Gridview中显示数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有两张桌子:VehicleMaintenance_log和safety_log。我想一次只显示一个表数据。我有一个有两个选项的组合框:维护日志和safety_report,当我选择oncombobox ex上的一个项目时:对于维护日志,gridview应该只根据注册号显示维护日志值。我怎么能做到这一点?我知道一次只能从一张桌子中选择。我这是第一次这样做。我正在使用visual studio 2013和数据库mysql.My数据驻留在同一模式中的两个表(maintenance_log和safety_report)。这是我尝试使用ifloops.please帮助。

  private   void  button1_Click( object  sender,EventArgs e)
{
string constring = datasource = localhost; port = 3306; username = root; password = Ammoos123;
MySqlConnection conDatabase = new MySqlConnection(constring);
if (( this .cmb2.SelectedItem.ToString())== SafetyInspection_Report
{
MySqlCommand cmdDatabase = new MySqlCommand( select * from vehicle_automation.safetyinspection_report其中vehRegNo = @ regno和@ date1之间的inspection_date和@ date2,conDatabase);
cmdDatabase.Parameters.AddWithValue( @ regno .cmb1.SelectedItem);
cmdDatabase.Parameters.AddWithValue( @ date1 .dt1.Text);
cmdDatabase.Parameters.AddWithValue( @ date2 .dt2.Text);
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDatabase;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bsource = new BindingSource();
bsource.DataSource = dbdataset;
dtgv1.DataSource = bsource;
sda.Update(dbdataset);

}
catch (例外情况)
{
MessageBox.Show(ex.Message);
}
}
其他 如果(( this .cmb2.SelectedItem.ToString())== Maintenance_log
{
MySqlCommand cmdDatabase = new MySqlCommand( select * from vehicle_automation.maintenance_log where vehRegNo = @ regno和inspection_date @ date1和@date2,conDatabase);
cmdDatabase.Parameters.AddWithValue( @ regno .cmb1.SelectedItem);
cmdDatabase.Parameters.AddWithValue( @ date1 .dt1.Text);
cmdDatabase.Parameters.AddWithValue( @ date2 .dt2.Text);
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDatabase;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bsource = new BindingSource();
bsource.DataSource = dbdataset;
dtgv1.DataSource = bsource;
sda.Update(dbdataset);

}
catch (例外情况)
{
MessageBox.Show(ex.Message);
}
}





从下面的评论中复制的其他信息

Hai实际上我不知道这是否是正确的方法。它正在为安全检查工作。如果表中没有数据,gridview正在给那里的空表我需要显示有没有找到数据。

解决方案

这是解决方案。

private void button1_Click(object sender,EventArgs e)

{

string constring =datasource = localhost; port = 3306; username = root; password = Ammoos123;

MySqlConnection conDatabase = new MySqlConnection(constring);

if((this.cmb2.SelectedItem.ToString())==SafetyInspection_Report)

{

dtgv4.Visible = false;

dtgv1.Visible = true;

MySqlCommand cmdDatabase = new MySqlComma nd(select * from vehicle_automation.safetyinspection_report其中vehRegNo = @ regno和@ date1和@ date2之间的inspection_date,conDatabase);

cmdDatabase.Parameters.AddWithValue(@ regno,this.cmb1。 SelectedItem);

cmdDatabase.Parameters.AddWithValue(@ date1,this.dt1.Text);

cmdDatabase.Parameters.AddWithValue(@ date2,this。 dt2.Text);



试试

{

MySqlDataAdapter sda = new MySqlDataAdapter();

sda.SelectCommand = cmdDatabase;

DataTable dbdataset = new DataTable();

sda.Fill(dbdataset);

BindingSource bsource = new BindingSource();

bsource.DataSource = dbdataset;

dtgv1.DataSource = bsource;

sda.Update(dbdataset);



}

catch(例外情况)

{

MessageBox.Show(ex.Message);

}

}

else if((this.cmb2.SelectedItem.ToString ())==Maintenance_Log)

{

dtgv1.Visible = false;

dtgv4.Visible = true;

MySqlCommand cmdDatabase = new MySqlCommand(select * from vehicle_automation.maintenance_log where vehRegNo = @ regno and maintenance_date @ date1 and @ date2,conDatabase);

cmdDatabase.Parameters.AddWithValue( @regno,this.cmb1.SelectedItem);

cmdDatabase.Parameters.AddWithValue(@ date1,this.dt1.Text);

cmdDatabase.Parameters.AddWithValue (@ date2,this.dt2.Text);

试试

{

MySqlDataAdapter sda = new MySqlDataAdapter();

sda.SelectCommand = cmdDatabase;

DataTable dbdataset = new DataTable();

sda.Fill(dbdataset);

BindingSource bsource = new BindingSource();

bsource.DataSource = dbdataset;

dtgv4 .DataSource = bsource;

sda.Update(dbdataset);



}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

}


Quote:

嗨如果数据库中没有关于搜索条件的数据,我该如何显示消息?





查看此答案

[ ^ ]你可以使用 EmptyDataRow [ ^ ]模板样式。还有其他一些选择,请检查

http://forums.asp.net/t/1222441.aspx?how+to+display+empty+gridview+with+a+message+No+data+Avaialable+ [ ^ ]


Hi I have two tables : VehicleMaintenance_log and safety_log. i want to display only one table data at a time. i have one combobox with two options:maintenance log and safety_report, When i select on one item oncombobox ex: for maintenance log, the gridview should display only that maintenance log values according to registration number. How can i acheive this? I know only to select from a table at a time. I am doing this for the first time. I am using visual studio 2013 and for the database mysql.My data resides in two tables(maintenance_log and safety_report) in the same schema.This is what i tried i used " if" loops.please help.

private void button1_Click(object sender, EventArgs e)
        {
            string constring = "datasource=localhost;port=3306;username=root;password=Ammoos123";
            MySqlConnection conDatabase = new MySqlConnection(constring);
            if((this.cmb2.SelectedItem.ToString())=="SafetyInspection_Report")
            {
                MySqlCommand cmdDatabase = new MySqlCommand("select * from vehicle_automation.safetyinspection_report where vehRegNo=@regno and inspection_date between @date1 and @date2", conDatabase);
                cmdDatabase.Parameters.AddWithValue("@regno", this.cmb1.SelectedItem);
                cmdDatabase.Parameters.AddWithValue("@date1", this.dt1.Text);
                cmdDatabase.Parameters.AddWithValue("@date2", this.dt2.Text);
                try
                {
                    MySqlDataAdapter sda = new MySqlDataAdapter();
                    sda.SelectCommand = cmdDatabase;
                    DataTable dbdataset = new DataTable();
                    sda.Fill(dbdataset);
                    BindingSource bsource = new BindingSource();
                    bsource.DataSource = dbdataset;
                    dtgv1.DataSource = bsource;
                    sda.Update(dbdataset);

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else if((this.cmb2.SelectedItem.ToString())=="Maintenance_log")
            {
                 MySqlCommand cmdDatabase = new MySqlCommand("select * from vehicle_automation.maintenance_log where vehRegNo=@regno and inspection_date between @date1 and @date2", conDatabase);
                cmdDatabase.Parameters.AddWithValue("@regno", this.cmb1.SelectedItem);
                cmdDatabase.Parameters.AddWithValue("@date1", this.dt1.Text);
                cmdDatabase.Parameters.AddWithValue("@date2", this.dt2.Text);
            try
            {
                MySqlDataAdapter sda = new MySqlDataAdapter();
                sda.SelectCommand = cmdDatabase;
                DataTable dbdataset = new DataTable();
                sda.Fill(dbdataset);
                BindingSource bsource = new BindingSource();
                bsource.DataSource = dbdataset;
                dtgv1.DataSource = bsource;
                sda.Update(dbdataset);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }        
         }



additional information copied from comment below
Hai actually i dont know if this is the proper way to do it.Its working for safety inspection.bt if there is no data in the table also the gridview is giving the empty table there I need to display "there is no datas found".

解决方案

The is the solution.
private void button1_Click(object sender, EventArgs e)
{
string constring = "datasource=localhost;port=3306;username=root;password=Ammoos123";
MySqlConnection conDatabase = new MySqlConnection(constring);
if((this.cmb2.SelectedItem.ToString())=="SafetyInspection_Report")
{
dtgv4.Visible = false;
dtgv1.Visible = true;
MySqlCommand cmdDatabase = new MySqlCommand("select * from vehicle_automation.safetyinspection_report where vehRegNo=@regno and inspection_date between @date1 and @date2", conDatabase);
cmdDatabase.Parameters.AddWithValue("@regno", this.cmb1.SelectedItem);
cmdDatabase.Parameters.AddWithValue("@date1", this.dt1.Text);
cmdDatabase.Parameters.AddWithValue("@date2", this.dt2.Text);

try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDatabase;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bsource = new BindingSource();
bsource.DataSource = dbdataset;
dtgv1.DataSource = bsource;
sda.Update(dbdataset);

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else if((this.cmb2.SelectedItem.ToString())=="Maintenance_Log")
{
dtgv1.Visible = false;
dtgv4.Visible = true;
MySqlCommand cmdDatabase = new MySqlCommand("select * from vehicle_automation.maintenance_log where vehRegNo=@regno and maintenance_date between @date1 and @date2", conDatabase);
cmdDatabase.Parameters.AddWithValue("@regno", this.cmb1.SelectedItem);
cmdDatabase.Parameters.AddWithValue("@date1", this.dt1.Text);
cmdDatabase.Parameters.AddWithValue("@date2", this.dt2.Text);
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDatabase;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bsource = new BindingSource();
bsource.DataSource = dbdataset;
dtgv4.DataSource = bsource;
sda.Update(dbdataset);

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}


Quote:

Hi How can i show a message if there is no data on the database for the search criteria?



check this answer
[^] you can use EmptyDataRow [^]template style. there are some other alternatives, check
http://forums.asp.net/t/1222441.aspx?how+to+display+empty+gridview+with+a+message+No+data+Avaialable+[^]


这篇关于如何在同一数据库中的两个表之间切换,以便在C#中的Gridview中显示数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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