I am workin on something like a "SQL Query generator".
To be able to biuld WHERE clauses I need to find out which type the value has in the database.
i.e: if the value is an int I will use operators like (<,>,=), on DateTime I want to add <,>,= and a combobox where the user can select sth like "DateTime.Now".
So thats why I need to differ between the types.
Is there any way to get the Types from the columns of a table converted to a .NET Type ?
I currently got this code:
using (SqlConnection conn = new SqlConnection(MonitoringContext.Instance.ConnectionString))
{
conn.Open();
string query = $"SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{SelectedDatabaseItem}' AND COLUMN_NAME = '{Criteria[currentIndex].ColumnValueComboBox.SelectedItem}'";
SqlCommand command = new SqlCommand(query, conn);
object reader = command.ExecuteScalar();
valueType = Type.GetType(reader.ToString());
}
if(valueType != null)
{
if (valueType == typeof(DateTime))
{
List<string> operators = new List<string>() { "<", ">", "=" };
Criteria[currentIndex].OperatorValueComboBox.ItemsSource = operators;
//Add datePicker
return;
}
if(valueType == typeof(int))
{
//Add textbox to criteria selector
return;
}
}
So assuming the type in the DB is int this is working fine 'cause Type.GetType("int") will return typeof(int):
But as soon as it's for example "nvarchar" I cannot convert it to string.
Is there any builtin function to convert this ?