How to change values of column of DataTable and sh

2019-09-19 17:33发布

问题:

I have a column with encrypted name(all other columns are not encrypted) in SQL Database table. And I have to decrypt the column with encrypted name to show in DataGrid to users of my application but the actual table of SQL database should not be changed.(has to remain as encrypted name).

I think UpdateCommand works to update the actual table and I have to find an alternative than below UpdateCommand.

Or is there alternative way to decrypt only 1 column on DataTable which is not influencing the actual table of database?

My simple code is,

SqlCommand gridcomm = new SqlCommand();
gridcomm.Connection = Conn;

gridcomm.CommandText = "SELECT Id, customername, phonenumber FROM customers";

SqlDataAdapter gridda = new SqlDataAdapter(gridcomm);

SqlDataReader gridreader = gridcomm.ExecuteReader();
while (gridreader.Read())
{
}
gridreader.Close();

DataTable griddt = new DataTable("customers");
gridda.Fill(griddt);

foreach (DataRow row in griddt.Rows)
{
    string strcustomername = (string) row["customername"].ToString();
    bytecustomername = Convert.FromBase64String(strcustomername);
    string decryptedcustomername = DecryptStringFromBytes_Aes(bytecustomername, byteAESKey, byteAESIV);

    row["customername"] = decryptedcustomername;
}

gridda.UpdateCommand = new SqlCommandBuilder(gridda).GetUpdateCommand();

dataGrid_Totalcustomerlist.ItemsSource = griddt.DefaultView;
gridda.Update(griddt);

回答1:

Hello Kay Lee: I think that if you look at implementing a Coverter in your View you will get exactly what you are looking for. In your IValueConverter implementation you can Implement the Decrypt routine. A Converter is the extended syntax in a WPF Binding Statement. If this is not clear then I will flesh out some more. Here is a great reference for Converters: http://www.wpf-tutorial.com/data-binding/value-conversion-with-ivalueconverter/

Kind Regards, Mark Wardell



回答2:

I've read many posts but there were no solution for me as this case is unusual. However, I just thought logically and finally found solution by myself.

We just need to delete 2 line of Update related code because we don't need to update.

gridda.UpdateCommand = new SqlCommandBuilder(gridda).GetUpdateCommand();

gridda.Update(griddt);

Hope this helps someone..