C#:将多个字符串从表单哈希到 Class,返回到表单到 SQL

C#: Hash Multiple Strings from a Form to Class, back to the form to SQL

我正在想办法做到这一点。

我想用我的主表单中的盐进行散列,然后 post 它到 SQL 服务器,我的问题是目前我似乎无法弄清楚如何调用散列代码SQL声明。

问题是我似乎无法弄清楚如何将散列字符串调用到变量中。我得到的错误之一是:

An Object of reference for the non-static field, method, or property'Functions.UniqueID'

如果我试过Functions myFunction = new Functions();

我得到了:

There is no argument given that corresponds to the required formal parameter

我尝试查找它,但实际上是一片空白,因为我是编码新手,不太理解每个人抛出的所有术语。

我想要在 class 中使用它的原因是我将在应用程序的多个实例中使用此 hash/salt。

让我展示代码,也许有人可以提供帮助:

主窗体: 吸气剂和吸气剂:

        public string IdentifyOrder
        {
            get { return txtOrder.Text; }
            set { txtOrder.Text = value; }
        }
        public string IdentifyStandard
        {
            get { return cmbStandard.Text; }
            set { cmbStandard.Text = value; }
        }
        public string IdentifyNote
        {
            get { return cmbNote.Text; }
            set { cmbNote.Text = value; }
        }

SQL 声明:

private void btnSubmitInfo_Click(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(Connection.MTRDataBaseConn))
                {

                    con.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandText = "INSERT INTO dbo.[myDatabase] ([Purchase Order], [Standard], [Notes], [Unique Identifier]) VALUES(@PurchaseOrder,@Standard,@Notes,@UniqueIdentifier)";

                    cmd.Connection = con;

                    SqlParameter pPurchaseOrder = new SqlParameter("@PurchaseOrder", SqlDbType.VarChar, 50);
                    SqlParameter pStandard = new SqlParameter("@Standard", SqlDbType.VarChar, 50);
                    SqlParameter pNotes = new SqlParameter("@Notes", SqlDbType.VarChar, 50);
                    SqlParameter pUID = new SqlParameter("@UniqueIdentifier", SqlDbType.VarChar, 50);


                    pPurchaseOrder.Value = txtPurchaseOrder.Text;
                    pStandard.Value = cmbStandard.Text;
                    pNotes.Value = txtNotes.Text;
                    pUID.Value = Functions.UniqueID;

                    cmd.Parameters.Add(pPurchaseOrder);
                    cmd.Parameters.Add(pStandard);
                    cmd.Parameters.Add(pNotes);
                    cmd.Parameters.Add(pUID);

                    //execute
                    cmd.ExecuteNonQuery();

                }
            }
            catch (SqlException ex)
            {
                //catch error
                MessageBox.Show(ex.Message);
            }
       }

我的Class"Functions":

public class Functions
    {
        public readonly MainForm Identifiers;
        public Functions(MainForm Identifiers)
        {
            this.Identifiers = Identifiers;
        }

        public void GenerateUniqueIdentifier()
        {
            string orderID = Identifiers.IdentifyOrder;
            string standardID = Identifiers.IdentifyStandard;
            string noteID = Identifiers.IdentiftNote;

           string salt = "" + orderID + "" + standardID + "" + noteID + "";
        }

        public String GenHash(String input, String salt)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(input + salt);
            System.Security.Cryptography.SHA256Managed sha256hashstring =
                new System.Security.Cryptography.SHA256Managed();
            byte[] hash = sha256hashstring.ComputeHash(bytes);

            return Convert.ToBase64String(hash);
        }

        public string UniqueID { get; set; }

    }

您需要创建一个 Functions class 的对象,然后调用它的方法或访问它的属性。

类似下面的内容。

//Previous code...

var functionsObj = new Functions(mainForm); // You need to pass the
     // object of class MainForm as argument to Functions constructor 
     //If this code is running in code behind for "MainForm" then you can do as following.
var functionObj = new Functions(this);
pPurchaseOrder.Value = txtPurchaseOrder.Text;
pStandard.Value = cmbStandard.Text;
pNotes.Value = txtNotes.Text;
pUID.Value = functionsObj.UniqueID;

//Next Code....

这应该可以解决问题。