2017-02-07 5 views
0

私はこれを行う方法を理解しようとしています。C#:フォームからSQLへのフォームに複数の文字列をハッシュ

私のメインフォームの塩をハッシュしてSQL Serverに投稿したいのですが、私の問題は現在、SQL文のハッシュコードを呼び出す方法がわかりません。

問題は、ハッシュされた文字列を変数などに呼び出す方法がわかりません。私が得るのいずれかのエラーがある:私はFunctions myFunction = new Functions();

をしようとした場合

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

私が得た:

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

は、私はそれを見てみましたが、私は新たなんだので、本当にただの空白思い付い誰もが捨てるすべての用語を理解しているわけではありません。

私がクラスでそれを望んでいた理由は、このハッシュ/塩をアプリケーションの複数のインスタンスで使用することでした。

私は、コードをお見せしましょう、多分誰かが助けることができる:

メインフォーム: 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; } 

    } 
+0

エラーは、Functionクラス(新しい_関数(...)_がある場所)を作成しても、そのコードを表示していない点を指しているようです。 – Steve

+0

あなたのコードのハッシュを生成?ハッシュをデータベースに保存したいのですか? –

+0

私は試したことの他のものへの参照としてそのエラーを投稿するつもりだった。 @ChetanRanpariya私は、変数にハッシュコードを格納したいと思います。意味は完成したハッシュ..私は..これは本当に新しいですね。 – Mokey

答えて

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

これで問題が解決するはずです。

+0

私はこれを試してみましたが、私はそれを見て再び試しました。これはエラーを返します: '必要な仮引数 'Identifiers' of 'Functions.Functions(MainForm)'に対応する引数はありません。 – Mokey

+0

ありがとう!魅力のように動作します。私は 'var functionObj = new Functions(this); 'を使う必要がありました。 – Mokey

関連する問題