在表格2上使用Form1中的数据 [英] Use data from Form1 on form 2

查看:65
本文介绍了在表格2上使用Form1中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我不知道即时消息是否能够正确解释这一点,但我会尽力

我想做的是,在Form1上成功的mysqlconnection上,返回值为True,然后可以在Form 2上使用该True值



Form1
isconnected = true


Form2 if(isconnected = true){执行查询}

但是我不知道该怎么做...

Okay i dont know if im going to be able to explain this correctly but i will try

What im trying to do is, On a Successful mysqlconnection on Form1 have a value of True Returned, then be able to use that True value on Form 2

Somthing like

Form1
isconnected = true


Form2 if(isconnected = true){execute query}

But i have no clue on how to do this...
Any help would be appreciated

推荐答案

嘿,您将要使用的解决方案不是一种方法.如果要实现该解决方案,则需要遵循Object-面向对象的编程方法论,您可以在其中为MySQLConnection创建一个类,然后根据需要使用任意多的时间.
看一下如何创建它.

创建一个class并创建一个static方法
Hey the solution you are going to use is not a way.if you want to achieve it you need to follow the concept of Object-Oriented Programming Methodology in which you create a class for your MySQLConnection and then use as many time as you want.
take a look how you will create it.

Create a class and create a static method
public class myConnection
{
    public static MySqlConnection GetConnection()
    {
        string str = "SERVER=localhost;"+"DATABASE=mydatabase;"+"UID=testuser;" + "PASSWORD=testpassword;";
        // Note this String is a Example you need to write your own connection string
        MySqlConnection con = new MySqlConnection(str);
        con.Open();
        return con;
    }
}




如果要在运行时在连接字符串中传递值,则可以使用它




if you want to pass value in connection string at runtime then you can use it

class myConnection
  {
      public static string datasource = "";
      public static string initialcatalog = "";
      public static string uid = "";
      public static string pwd= "";
      public static MySqlConnection GetConnection()
      {
          string str = "SERVER='"+datasource+"';DATABASE='"+initialcatalog+"';UID='"+uid+"';PASSWORD='"+pwd+"'";
           MySqlConnection con = new MySqlConnection(str);
          con.Open();
          return con;
      }
  }



然后转到要在其中输入Connection Strng数据的Form,然后查看如何在myConnection类中传递值.
让此代码放置在连接按钮的单击事件中或您需要的任何位置



and then go to Form where you are going to Enter data for Connection Strng and then look how you will pass value in myConnection Class.
let this code is place in click event of connect button or any where you want as your requirement

// passing value from user to MyConnection Class variables.
MyConnection.datasource = txtdatsource.Text;
            MyConnection.initialcatalog = txtdatabase.Text;
            MyConnection.uid = txtuid.Text;
            MyConnection.pwd = txtpwd.Text;






现在可以使用
myConnection.GetConnection(); 而不是创建新的连接.
每当您调用它时,它都会为您提供连接.

制作一次,使用一次以上....






now when you can use
myConnection.GetConnection(); instead of creating a new connection.
when ever you call it it will give you a connection.

Make Once, use More than Once....


这是Web应用程序还是Windows应用程序?
如果是其Web应用程序-使用会话变量或查询字符串

如果是Windows应用程序,则可以简单地以形式1声明一个公共变量,然后从形式2读取它.


形式1:

public boolean isconnected;


//内部方法{
isconected = true;
}


形式2:
label1.Text = form1.isconnected.tostring();
is this Web Application or Windows application?
if its web application - use Session variables or Querystrings

if its windows application you can simply declare a public variable in form 1, and read it from form 2.
i.e

in form 1:

public boolean isconnected;


//inside method {
isconected = true;
}


in form 2:
label1.Text = form1.isconnected.tostring();


在Form 1中声明一个公共静态变量
public static bool isConnect=false;
进行操作并更改isConnection值

Declare a public static variable in Form 1
public static bool isConnect=false;
Do the operation and change the isConnection value

private void Connect()
        {
             //Your code 
            isConnect = true;
        }




从Form2调用值

bool connected=Form1.isConnect;

您将获得变量值.由于将变量声明为静态变量,因此无需创建类的实例来访问变量

希望对您有帮助:)




Call the value from the Form2

bool connected=Form1.isConnect;

You will get the variable value.Since the variable is declared as static no need to create an instance of the class to access the variable

Hope this will help you :)


这篇关于在表格2上使用Form1中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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