【问题标题】:C#: Hash Multiple Strings from a Form to Class, back to the form to SQLC#:将多个字符串从表单散列到类,再返回表单到 SQL
【发布时间】:2017-02-07 23:51:51
【问题描述】:

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

我想用我的主表单中的盐进行散列并将其发布到 SQL Server,我的问题是目前我似乎无法弄清楚如何为 SQL 语句调用散列代码。

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

非静态字段、方法的引用对象或 属性'Functions.UniqueID'

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

我明白了:

没有给出与所需形式相对应的参数 参数

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

我想要在一个类中使用它的原因是我将在应用程序的多个实例中使用这个哈希/盐。

让我展示一下代码,也许有人可以帮忙:

主要形式: Getter 和 Setter:

        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);
            }
       }

我的班级“功能”:

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; }

    }

【问题讨论】:

  • 该错误似乎是指您创建 Function 类的位置(在某处您有一个 new Functions(....))但您没有显示代码...
  • 您在代码中究竟在哪里使用了生成的哈希?您想将哈希存储在数据库中吗?
  • 我打算发布该错误作为对我尝试过的其他事情的参考。 @ChetanRanpariya 我想将散列代码存储在一个变量中。意思是完成的哈希..我想..我对此真的很陌生。
  • UniqueID 是您想用来从Functions 类访问哈希的属性吗?您想为表单的每个输入值生成哈希?
  • btnSubmitInfo 是同一个表单(MainForm)?

标签: c# sql sql-server class sha


【解决方案1】:

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

如下所示。

//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....

这应该可以解决问题。

【讨论】:

  • 我试过这个,我又试了一次,发现是我,但是。这将返回错误:There is no argument given that corresponds to the required formal parameter 'Identifiers' of 'Functions.Functions(MainForm)'
  • 谢谢!奇迹般有效。我不得不使用var functionObj = new Functions(this);
猜你喜欢
  • 2015-12-08
  • 1970-01-01
  • 1970-01-01
  • 2015-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多