我不明白我将同一个变量赋值两次背后的逻辑

本文关键字:两次 背后 明白 同一个 赋值 变量 | 更新日期: 2023-09-27 17:54:46

我第一次在这个网站上提问。我在Visual c# 2008中创建了一个简单的窗体,我将两个文本框的值添加到access 2010数据库中的两个字段。该数据库名为TestDatabase。accdb,表为TestTable。两个变量,FirstName &地址分配给文本框。txt的文本值。名字和文本地址。在我看来,我也在为OleDBCommand类添加参数的语句中为这两个变量添加值:

myCommand.Parameters.Add("@FirstName", OleDbType.VarChar).Value = txtName.Text;

所以基本上它是工作的,但我不明白其中的逻辑因为它看起来好像我不需要使用

FirstName = txtName.Text;

Address = txtAddress.Text;

语句。如果我从TextChanged事件中删除代码,我将收到几个警告。

代码如下:

namespace Test
{
    public partial class Form1 : Form
    {
        private string FirstName;
        private  string Address;
        private void cmdAdd_Click(object sender, EventArgs e)
        {
            string strSQL = "INSERT INTO TestTable(Name1, Address) VALUES(@FirstName, '@Address')";
            // represents an open connection to a data source.  Is a class
            OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:''TEMP''TestDatabase.accdb");
            // represents an SQL statement or stored procedure to execute against a data source
            //( takes care of passing queries to the database).  Is a class.
            OleDbCommand myCommand = new OleDbCommand(strSQL, myConnection);
            myCommand.Parameters.Add("@FirstName", OleDbType.VarChar).Value = txtName.Text;
            myCommand.Parameters.Add("@Address", OleDbType.VarChar).Value = txtAddress.Text;
            try
            {
                myConnection.Open();
                myCommand.ExecuteNonQuery();
            }
            catch (Exception)
            {
                MessageBox.Show("Something went wrong");
            }
            finally
            {
                myConnection.Close();
            }
        }
        private void txtName_TextChanged(object sender, EventArgs e)
        {
            //IsNullOrEmpty indicates whether the string is null ot an Empty string
            //true if the value parameter is null or an empty string(""); otherwise, false
            if (string.IsNullOrEmpty(txtName.Text))
            {
                //has no value
            }
            else
            {
                FirstName = txtName.Text;
            }
        }
        private void txtAddress_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtAddress.Text))
            {
                //has no value
            }
            else
            {
                Address = txtAddress.Text;
            }
        }
    }
}

我不明白我将同一个变量赋值两次背后的逻辑

欢迎来到SO, Kevin!

澄清一下,这里没有设置两次值。

    private void txtName_TextChanged(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtName.Text))
        {
            //has no value
        }
        else
        {
            FirstName = txtName.Text;
        }
    }
    private void txtAddress_TextChanged(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtAddress.Text))
        {
            //has no value
        }
        else
        {
            Address = txtAddress.Text;
        }
    }

您正在设置上面代码中的值。

    private void cmdAdd_Click(object sender, EventArgs e)
    {
        string strSQL = "INSERT INTO TestTable(Name1, Address) VALUES(@FirstName, @Address)";
        OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:''TEMP''TestDatabase.accdb");
        OleDbCommand myCommand = new OleDbCommand(strSQL, myConnection);
        myCommand.Parameters.Add("@FirstName", OleDbType.VarChar).Value = txtName.Text;
        myCommand.Parameters.Add("@Address", OleDbType.VarChar).Value = txtAddress.Text;
        try
        {
            myConnection.Open();
            myCommand.ExecuteNonQuery();
        }
        catch (Exception)
        {
            MessageBox.Show("Something went wrong");
        }
        finally
        {
            myConnection.Close();
        }
    }

在上面的代码中,您正在的值重新分配给参数,或者换句话说,的值参数化。这是一个很好的使用和理解的做法,因为它可以帮助保护您免受SQL注入攻击。

你的sql文本中有一个错误

string strSQL = "INSERT INTO TestTable(Name1, Address) VALUES(@FirstName, '@Address')";

你应该写

string strSQL = "INSERT INTO TestTable(Name1, Address) VALUES(@FirstName, @Address)";

全局变量Address和FirstName不是真正需要的,除非有其他代码使用它们作为外部代码可用的表单属性-

// Trailing ' To Be Removed
String strSQL = "INSERT INTO TestTable(Name1, Address) VALUES(@FirstName, @Address)";
// Incorrect Parameters To Be Fixed
myCommand.Parameters.AddWithValue("@FirstName", txtName.Text);
myCommand.Parameters.AddWithValue("@Address", txtAddress.Text);

我想他想知道为什么他有TextChanged方法来填充私有变量而不使用它们?

myCommand.Parameters.Add("@FirstName", OleDbType.VarChar).Value = FirstName;
myCommand.Parameters.Add("@Address", OleDbType.VarChar).Value = Address;

利用了它们,所以你可以设置、操作它们,等等。