I'm trying to populate a datagridview with SQL Server and MySQL tables to map data between the two tables. The problem I'm facing is getting the column from MySQL table on the same row as that on SQL Server table. Populating the datagridview individually is working fine.
Here's what I need after selecting a table from combobox.Sample result
Here's my code for querying:
string colName, dataType, charMaxLeng, isNull;
- SQL Database.
private void SQLMapping()
{
try
{
int i = 0;
string charMax;
dtgMappings.Rows.Clear();
Conn.Close();
Conn.Open();
CMD = new SqlCommand("SELECT COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,IS_NULLABLE FROM INFORMATION_SCHEMA. COLUMNS WHERE TABLE_NAME='" + cboTables.Text + "';", Conn);
Dr = CMD.ExecuteReader();
while (Dr.Read())
{
if(Dr.IsDBNull(2))
{
charMax = null;
}
else
{
charMax = Dr.GetInt32(2).ToString();
}
MySQLMapping();
i += 1;
dtgMappings.Rows.Add(null, i, Dr.GetString(0).ToString(), Dr.GetString(1).ToString(), charMax, Dr.GetString(3).ToString(), colName, colDataType.Items.Add(Convert.ToString(dataType)), charMaxLeng, colNull.Items.Add(Convert.ToString(isNull)));
}
Dr.Close();
Conn.Close();
groupBox2.Text = "Columns available for Mapping(" + dtgMappings.Rows.Count + ")";
}
catch (Exception ex)
{
Conn.Close();
MessageBox.Show(ex.Message, Application.ProductName);
}
}
- MySQL Database.
private void MySQLMapping()
{
try
{
MyConn.Close();
MyConn.Open();
MyCMD = new MySqlCommand("SELECT COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='" + cboTables.Text.ToLower() + "';", MyConn);
MyDr = MyCMD.ExecuteReader();
while (MyDr.Read())
{
if (MyDr.IsDBNull(2))
{
charMaxLeng = null;
}
else
{
charMaxLeng = MyDr.GetInt32(2).ToString();
}
colName = MyDr.GetString(0).ToString();
dataType = MyDr.GetString(1).ToString();
isNull = MyDr.GetString(3).ToString();
}
MyDr.Close();
MyConn.Close();
}
catch(Exception ex)
{
MyConn.Close();
MessageBox.Show(ex.Message, Application.ProductName);
}
}
Anyone to help me to populate the other columns (MySQL Column, Data Type, Length, Null) from MySQL Database
string colName, dataType, charMaxLeng, isNull;
) which is then called whine adding the rows to the datagridview. (` MySQLMapping(); i += 1; dtgMappings.Rows.Add(null, i, Dr.GetString(0).ToString(), Dr.GetString(1).ToString(), charMax, Dr.GetString(3).ToString(), colName, colDataType.Items.Add(Convert.ToString(dataType)), charMaxLeng, colNull.Items.Add(Convert.ToString(isNull)));`)