简体   繁体   English

无法连接到SQL数据库-用户名和密码丢失

[英]Cannot connect to SQL database - loss of username and password

having difficulty with a bit of code I have. 遇到一些代码困难。 I think variables are stuck inside the method I'm invoking and when I go to run that method again variables are no longer there. 我认为变量卡在了我正在调用的方法中,当我再次运行该方法时,变量不再存在。

Below is an example of my main: 下面是我的主要例子:

        public partial class Login : Form
{

    public Login()
    {
        InitializeComponent();
    }
    sqlconnect sql = new sqlconnect();
    public string pass;
    public string user;


    public void button1_Click(object sender, EventArgs e)
    {
        //Username and password textboxes send to public strings
        user = textBox1.Text;
        pass = textBox2.Text;

            try
            {
                //try connecting to SQL database using SQL class
                sql.GetSqlConnection();
                sql.myConnection.Open();
                this.Hide();

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

Here is the SQL connection class: 这是SQL连接类:

    namespace NHS_Workshop_System
         {

public class sqlconnect
{
    public SqlConnection myConnection { get; set; }
    Settings1 set = new Settings1();
    Login var = new Login();

    public SqlConnection GetSqlConnection()
    {
        if (myConnection == null)
            myConnection = new SqlConnection("user id="+(var.user)+"; password="+(var.pass)+";server="+(set.SelectServer)+";Trusted_Connection="+(set.SelectContype)+";database="+(set.SelectDataBase)+"; connection timeout="+(set.Condrop)+"");

        return myConnection;
    }


}

} }

So when I try to login it may work first time but if another form tries to run that method the username and password are missing. 因此,当我尝试登录时,它可能会在第一次工作,但如果其他表单尝试运行该方法,则用户名和密码会丢失。 I'm not sure how to make the variables global so that every time I run the method it calls those details without fail. 我不确定如何使变量成为全局变量,以便每次运行该方法时都不会失败地调用这些详细信息。 So the second time it uses sqlconnection on another form the ex message states that username and password are non existent. 因此,它第二次在另一种形式上使用sqlconnection时,ex消息指出用户名和密码不存在。 Is this a scoping issue if so can someone shed some light on how i might go about this maybe a new method of gaining access to an SQL server? 这是否是一个范围界定的问题,如果有人可以阐明我该如何处理,也许这是一种获取SQL Server访问权限的新方法? Thank you tearing out my hair 谢谢你扯我的头发

Below is the answer for your question the way you have it now. 以下是您现在的问题答案。 But I strongly recommend you to think about what you are doing since your class logics doesn't seem to be very good. 但是我强烈建议您考虑一下自己在做什么,因为您的类逻辑似乎不太好。 I see that your trying to implement Singleton which is good, but you can still make more instances of sqlconnect if you would like. 我看到您尝试实现Singleton的尝试很好,但是如果愿意,您仍然可以创建更多sqlconnect实例。


In your Login class, pass this instance of Login so the sqlconnector knows the user and pass that is declared here. 在您的Login类中,传递此Login实例,以便sqlconnector知道用户并传递此处声明的内容。

public partial class Login : Form
{

// Old style - sqlconnect sql = new sqlconnect();
sqlconnect sql;
public string pass; // These arent declared and wont be of use for your sql connect class
public string user;

public Login( String pass, String user )
{
    this.pass = pass;
    this.user = user;
    // Now that we have a login with declared variables, pass it to the sqlconnect object
    this.sql = new sqlconnect(this);
    InitializeComponent();
}

Now in your sqlconnect class, before your Login variable was just empty. 现在在您的sqlconnect类中,在Login变量为空之前。 Now you have received an instance of it in your constructor that has it's variables set, 现在,您在构造函数中收到了一个实例,该实例已设置了变量,

public class sqlconnect
{
    public SqlConnection myConnection { get; set; }
    Settings1 set = new Settings1();
    // Here you maked a new one, use the existing instead from which this class is called.
    // changed variable named from var to login..
    Login login; 


    // *NEW* Constructor where you pass your login object
    public sqlconnect(Login login)
    {
        this.login = login;
    }

    public SqlConnection GetSqlConnection()
    {
        if (myConnection == null)
            myConnection = new SqlConnection("user id="+(login.user)+"; password="+(login.pass)+";server="+(set.SelectServer)+";Trusted_Connection="+(set.SelectContype)+";database="+(set.SelectDataBase)+"; connection timeout="+(set.Condrop)+"");

        return myConnection;
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM