Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8394621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:06:25+00:00 2026-06-09T20:06:25+00:00

I am working on database encryption , i.e. cell based symmetric encryption in SQL

  • 0

I am working on database encryption, i.e. cell based symmetric encryption in SQL server 2008 Express.
But the problem is that the parameterized queries for insertion are not working. Help me please . . .

Edit:

I am using the following query as Example:

foreach (var list in from DataRow row in dataTable.Rows select new ArrayList
                           {
                                 String.Format("@var1, {0}", row["Column1"]), 
                                 String.Format("@var2, {0}", row["Column2"]),
                                 String.Format("@var3, {0}", row["Column3"])
                           })
    {
         var query = String.Format(@"OPEN SYMMETRIC KEY {0} DECRYPTION BY CERTIFICATE {1} 
                     INSERT INTO TableA (Column1, Column2, Column3) VALUES (@ENCRYPTBYKEY(KEY_GUID('symKey'), '{2}'), ENCRYPTBYKEY(KEY_GUID('symKey'), '{3}'), ENCRYPTBYKEY(KEY_GUID('symKey'), '{4}'))", symKey, symCer, "@var1", "@var2", "@var3");
         con.Execute.ExecuteParameterizedQuery(query, list);
    }

public string ExecuteParameterizedQuery(string query, ArrayList parametersList)
{
      errorFlag = Connect(un, pasw, 3);
      if ((String.CompareOrdinal(errorFlag, "Open") == 0))
      {
          var myTran = myConnection.BeginTransaction();
          cmd = new SqlCommand(query, myConnection) { Transaction = myTran };
          for (var i = 0; i < parametersList.Count; i++)
          {
              var split = parametersList[i].ToString().Split(',');
              cmd.Parameters.AddWithValue(split[0], split[1]);
          }
          try
          {
              cmd.CommandText = query;
              cmd.ExecuteNonQuery();
              myTran.Commit();
              errorFlag = string.Empty;
          }
          catch (Exception e)
          {
              errorFlag = e.Message;
          }
          finally
          {
              myConnection.Close();
              myConnection.Dispose();
          }
          return errorFlag;
      }
      myConnection.Close();
      myConnection.Dispose();
      return errorFlag;
  }

EDIT 2:

CREATE PROCEDURE ng_encryptString
(
    @PlaneText VARCHAR(500), @SipherText VARBINARY(2000) OUT
)
AS
BEGIN
OPEN SYMMETRIC KEY symKey DECRYPTION BY CERTIFICATE SymCert 
SELECT ENCRYPTBYKEY(KEY_GUID('SymKey'), @PlaneText)
END
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:06:26+00:00Added an answer on June 9, 2026 at 8:06 pm

    Here what I would suggest is,

    1. CREATE a procedure with

      INSERT INTO TableA (Column1, Column2, Column3) VALUES(@val1,@val2,@val3)
      
    2. Now create a encryptThestring(string val) function in C# and it should return you the encrypted values of the given normal input value.

    3. Finally call the procedure in C# and pass the parameter as

      encryptThestring(string val1)
      encryptThestring(string val2)
      encryptThestring(string val3)
      

    This is the C#.net function

    public string EncryptString(string val)
                {
                    SqlConnection sqlconn = new SqlConnection("conn_string");
                    sqlconn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = sqlconn;
    
                    cmd.CommandText = "ng_encryptString"; // This is the sproc which will encrypt the string
                    cmd.CommandType = CommandType.StoredProcedure;
    
                    SqlParameter param1 = cmd.Parameters.Add("inpuStr", SqlDbType.VarChar, 500);
                    param1.Direction = ParameterDirection.Input;
    
                    SqlParameter param3 = cmd.Parameters.Add("@encryptedStr", SqlDbType.VarChar, 2000);
                    param3.Direction = ParameterDirection.Output;
    
                    param1.Value = val;
    
                    cmd.ExecuteNonQuery();
                    sqlconn.Close();
                    return (string)param3.Value;
    
                }
    

    sproc ng_encryptString

    CREATE Procedure [dbo].[ng_encryptString]  
    @string varchar(255),  
    @encryptedStr varbinary(2000)  OUTPUT  
    
    As  
    Begin  
    
    
    Declare @res varbinary(2000)  
    
    IF NOT EXISTS(select * from sys.symmetric_keys where name='##MS_DatabaseMasterKey##')  
    CREATE MASTER KEY ENCRYPTION  
    BY PASSWORD = 'yourpassword'  
    
    
    IF NOT EXISTS(select * from sys.certificates where name='EncryptTestCert')  
    CREATE CERTIFICATE EncryptTestCert  
    WITH SUBJECT = 'yoursubject'  
    
    IF NOT EXISTS(select * from sys.symmetric_keys where name='TestTableKey')   
    CREATE SYMMETRIC KEY TestTableKey  
    WITH ALGORITHM = TRIPLE_DES ENCRYPTION  
    BY CERTIFICATE EncryptTestCert  
    
    OPEN SYMMETRIC KEY TestTableKey DECRYPTION  
    BY CERTIFICATE EncryptTestCert  
    
    
    SELECT @encryptedStr=ENCRYPTBYKEY(KEY_GUID('TestTableKey'),@string)  
    
    end  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database working in my local sql server 2005 express edition. I
I am working on a database that is expected to operate over multiple distributed
I've just moved a database from a SQL 2000 instance to a SQL 2008
I am working with database data that manipulates college students exam results. Basically, I
While working with Database records ,displaying them on HTML and storing their ID into
I am working on database migration tool in java. The tool is copying database
I am working on Database where some of the Encrypted data I inserted in
I'm working on database migration program and I'm trying to migrate one database to
Case Currently I'm working on database seeding script, executed with sqlcmd . For example
Does anybody have an example of working with database using Visual C++ and OLEDB?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.