参数输入后,从C#中的外部类获取连接字符串 [英] Getting connection string from an external class in C# after parameters input

查看:181
本文介绍了参数输入后,从C#中的外部类获取连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有2个文本框的WinForm来输入ServerName和数据库,RadioButtons在提供者之间切换,1个Button可以根据输入构建ConnectionStrings。
_ServerName和_DatabaseName是全局变量。
我希望在Form外创建连接字符串,并将结果返回给我的Form中的标签控件,
我的外部类中的代码如下所示:

  public static string _ServerName {get;组; } 
public static string _Base {get;组; }
public static SqlConnection _Con {get;组; }
static void ConOption1()
{
Global._Con = new SqlConnection();
Global._Con.ConnectionString = @data source =+ Global._ServerName +; initial catalog =+ Global._Base +; Integrated Security = True;

$ / code>

我的表单(Form1)中的代码是:

  private void button1_Click(object sender,EventArgs e)
{
Global._ServerName = textBox1.Text;
Global._Base = textBox2.Text;
ConOption1();
label1.Text = Global._Con.ToString();





$ b

这里的问题是,我无法从Form1调用conOption1来获取构建的字符串在Label1.text中,感谢您的帮助。

解决方案

您没有标记 ConOption1

你真正的意思是我无法调用conOption1来获得构建的字符串?编译器不仅不会编译您的代码,而且还会指出确切的问题。在问题错误在哪里问题没有意义,因为编译器已经告诉你错误在哪里。


I Have a WinForm with 2 Text boxes to input ServerName and Database, RadioButtons to switch between providers, and 1 Button to build ConnectionStrings depending of inputs. _ServerName and _DatabaseName are Global variables. I would like to build the connection string outside the Form and get the result returned to a label control in my Form, the code in my external class is as next:

    public static string _ServerName { get; set; }
    public static string _Base { get; set; }
    public static SqlConnection _Con { get; set; }
    static void ConOption1()
    {
        Global._Con = new SqlConnection();
        Global._Con.ConnectionString = @"data source=" + Global._ServerName + "; initial catalog=" + Global._Base + "; Integrated Security=True";
    }

The code in my form (Form1) is:

        private void button1_Click(object sender, EventArgs e)
    {
        Global._ServerName = textBox1.Text;
        Global._Base = textBox2.Text;
        ConOption1();
        label1.Text = Global._Con.ToString();
    }

The problem here is that I cannot call conOption1 from Form1 to get the built string in Label1.text, thanks for your help.

解决方案

You did not mark ConOption1 as being public.

What do you exactly mean by "I cannot call conOption1 to get the built string"? The compiler not only won't compile your code but will also point at the exact problem. There is no point in asking the question on SO "Where is the error", as the compiler already told you where is the error.

这篇关于参数输入后,从C#中的外部类获取连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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