问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/10227949/getting-connection-string-from-an-external-class-in-c-sharp-after-parameters-inp