在另一个表单上的参数化查询中使用tabcontrol.selectedindex.value [英] Use tabcontrol.selectedindex.value on parameterized query on another form

查看:33
本文介绍了在另一个表单上的参数化查询中使用tabcontrol.selectedindex.value的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示数据的表单,按id过滤,另一个表单将值添加到同一个数据库。



I have a form to display data, filtered by id and another form which adds values to the same database.

string SQL = "INSERT INTO entradas (id_veiculo,id_empresa,nome_condutor,empresa_visitante,empresa_visitar,visitado,ncartao,data,hora,obs,sector) VALUES (@matricula,@idempresa,@nomecondutor,@empvisitante,@empvisitar,@visitado,@ncartao,@data,@hora,@obs,@sector)";

           using (var cn = new MySqlConnection("server=localhost;user id=root;password=12345;persistsecurityinfo=True;database=portaria;allowuservariables=True"))
           {
               using (var cmd = new MySqlCommand(SQL, cn))
               {
                   cmd.Parameters.AddWithValue("@matricula", txtmatricula.Text);
                   cmd.Parameters.AddWithValue("@idempresa", #);
                   cmd.Parameters.AddWithValue("@nomecondutor", txtnomecondutor.Text);
                   cmd.Parameters.AddWithValue("@empvisitante", txtempvis.Text);
                   cmd.Parameters.AddWithValue("@empvisitar", comboBox1.SelectedValue);
                   cmd.Parameters.AddWithValue("@visitado", txtpessoavisitar.Text);
                   cmd.Parameters.AddWithValue("@ncartao", txtncartao.Text);
                   cmd.Parameters.AddWithValue("@data", DateTime.Now.ToString("yyyy-MM-dd"));
                   cmd.Parameters.AddWithValue("@hora", DateTime.Now.ToShortTimeString());
                   cmd.Parameters.AddWithValue("@obs", txtobs.Text);
                   cmd.Parameters.AddWithValue("@sector", txtsector.Text);
                   cn.Open();

                   try
                   {
                       cmd.ExecuteNonQuery();
                   }
                   catch (MySqlException e)
                   {
                       // Do some logging or something.
                       MessageBox.Show("There was an error accessing your data. DETAIL: " + e.ToString());
                   }
               }
           }





在参数上查找#符号of @ idempresa

#here我想要检查所选标签值



我尝试了什么:



我还没有尝试任何东西,因为我对这个话题有点困惑: /



Look for the # symbol on the parameter of "@idempresa"
#here i want something to check the selected tab value

What I have tried:

i've not tryied anything yet because i'm a bit confused about this topic :/

推荐答案

创建一个类和如下所示的属性,假设你在Form1中有TabControl,

Hi, Create a class and a property like the following, I assuming you are having TabControl in Form1,
public static class SharedObjects
{
  private static Form _objForm1;
  
  //Property for holding reference of Form1
  public static Form objForm1 
  {
    get { return _objForm1; }
    set { _objForm1 = value; }
  }
}



现在,只需将Form1的引用分配给Form1的Load事件中的objForm1,如下所示,


Now, just assign the reference of Form1 to objForm1 in Load event of Form1 like follows,

SharedObjects.objForm1 = this;



现在我们有参考在SharedObjects类的Form1中,我们可以在项目内的任何位置使用它。因此,您可以像下面一样访问Form2中Form1中的tabcontrol,


Now we have the reference of Form1 in SharedObjects class, we can use this anywhere inside the project. So you can access the tabcontrol in Form1 in Form2 like following,

cmd.Parameters.AddWithValue("@idempresa", SharedObjects.objForm1.YourTabControl.SelectedIndex);



试试这个,


Try this,


这篇关于在另一个表单上的参数化查询中使用tabcontrol.selectedindex.value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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