Преобразование из элемента считывателя данных в код
Это мой проект:
https://www4.0zz0.com/2020/05/13/03/372544645.jpg[^]
и это мой дизайн базы данных:
https://www10.0zz0.com/2020/05/13/03/171174839.jpg[^]
с помощью выбранного индекса CmbInfoType изменилось событие
текстовая строка из устройства чтения данных превращается в код
который открывает файл из ресурсов в richtextbox
Что я уже пробовал:
пробовал этот код, но выдает ошибку:
(Не удалось скомпилировать скрипт)
Imports System.CodeDom.Compiler Imports System.Data.OleDb Imports System.Reflection Public Class Form1 Dim cnn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Informations.accdb") Dim da As OleDbDataAdapter Dim dr As OleDbDataReader Public Function GenerateScript(code As String) As IScript Using provider As New VBCodeProvider() Dim parameters As New CompilerParameters() parameters.GenerateInMemory = True parameters.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location) parameters.ReferencedAssemblies.Add("System.Data.dll") parameters.ReferencedAssemblies.Add("System.Xml.dll") Dim interfaceNamespace As String = GetType(IScript).Namespace Dim codeArray() As String = New String() {"Imports " & interfaceNamespace & Environment.NewLine & code} Dim results As CompilerResults = provider.CompileAssemblyFromSource(parameters, codeArray) If results.Errors.HasErrors Then Throw New Exception("Failed to compile script") Else Return CType(results.CompiledAssembly.CreateInstance("Script"), IScript) End If End Using End Function Public Interface IScript Function DoWork() As String End Interface Private Sub CmbInfoCategory_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbInfoCategory.SelectedIndexChanged CmbInfoType.Items.Clear() If CmbInfoCategory.SelectedItem = ("Diaries") Then Dim dt1 As New DataTable dt1.Clear() Dim sql As String = "SELECT * FROM Diaries" da = New OleDbDataAdapter(sql, cnn) da.Fill(dt1) For ii As Integer = 0 To dt1.Rows.Count - 1 CmbInfoType.Items.Add(dt1(ii)(0)) Next End If End Sub Private Sub CmbInfoType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbInfoType.SelectedIndexChanged Using cnn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Informations.accdb") cnn.Open() Try Dim sql As String = "SELECT * From Diaries WHERE DiaryName= @myname" Dim cmd As New OleDbCommand(sql, cnn) cmd.Parameters.AddWithValue("@myname", CmbInfoType.SelectedItem) dr = cmd.ExecuteReader If dr.Read = False Then MsgBox("No Patient is Registered Under This Number") Else Dim script As IScript = GenerateScript(dr.Item(1).ToString()) script.DoWork() End If Catch ex As Exception MsgBox(ex.Message) End Try cnn.Close() End Using End Sub End Class