Question

I am currently trying to set up a sqlconnection to our database that will return the datatypes of all the columns / rows (this needs to be done so we can enter the data into an API). This data is being placed in a datatable and then through a hashtable to get the type.

The problem is that when the program is run, no errors occur but it does not return anything in the console writelines that have been specified. I'm not an overly experienced .net developer so I'm not sure what I'm missing, but my guess would be in the order of how the sql command / connection is opened?

static void Main(string[] args)
{
    Hashtable sqlDatatypeholder = new Hashtable();

   //Sql Connection      
    string _mySqlUrl = "connection is correct and works in other test apps";    
    string _mySqlQuery = "Query is here, it works fine in SQL management studio";

    SqlConnection conn = new SqlConnection(_mySqlUrl);
    SqlCommand comm = new SqlCommand(_mySqlQuery, conn);

    DataTable _tempTable = new DataTable();

    using (conn)
    {
        SqlCommand command = new SqlCommand(_mySqlQuery,conn);
        conn.Open();

        SqlDataReader reader = command.ExecuteReader();    
        if (reader.HasRows)
        {
            while (reader.Read())
            {
                for (int v = 0; v < _tempTable.Columns.Count; v++)
                {
                    DataColumn dc = _tempTable.Columns[v];
                    sqlDatatypeholder.Add(dc.ColumnName.ToString(), Convert.ToString(reader.GetSqlValue(v).GetType()));
                }

                foreach (DictionaryEntry hr in sqlDatatypeholder)
                {
                    Console.WriteLine(hr.Key + " " + hr.Value);
                }
            }
        }
        else
        {
             Console.WriteLine("Connection Open - No rows found.");
        }
        reader.Close();
    }

    //Console.WriteLine("Created Sheet " + SmartSheetid);
     Console.ReadLine();

}

Was it helpful?

Solution

your _tempTable Table is empty you are not filling it up that's why you are unable to get anything. try this code hope it will help.

DataTable _tempTable = new DataTable();
SqlDataAdapter a = new SqlDataAdapter("Your Query",conn);
a.Fill(_tempTable);

You can use this code as well it will work

static void Main(string[] args)
{
    Hashtable sqlDatatypeholder = new Hashtable();
    //Sql Connection      
    string _mySqlUrl = "connection is correct and works in other test apps";
    string _mySqlQuery = "Query is here, it works fine in SQL management studio";

    SqlConnection conn = new SqlConnection(_mySqlUrl);
    SqlCommand comm = new SqlCommand(_mySqlQuery, conn);

    DataTable _tempTable = new DataTable();

    using (conn)
    {
        SqlCommand command = new SqlCommand(_mySqlQuery, conn);
        conn.Open();

        SqlDataReader reader = command.ExecuteReader();
        _tempTable.Load(reader);

        if (_tempTable != null && _tempTable.Rows.Count > 0)
        {
            for (int v = 0; v < _tempTable.Columns.Count; v++)
            {
                DataColumn dc = _tempTable.Columns[v];
                sqlDatatypeholder.Add(dc.ColumnName.ToString(), Convert.ToString(reader.GetSqlValue(v).GetType()));
            }
            foreach (DictionaryEntry hr in sqlDatatypeholder)
            {
                Console.WriteLine(hr.Key + " " + hr.Value);
            }
        }
        else
        {
            Console.WriteLine("Connection Open - No rows found.");
        }   
        reader.Close();             
    }
}

OTHER TIPS

I see following changes required to get values in your hashtable. Note exception handling is must try adding it:

    static void Main(string[] args)
    {
        Hashtable sqlDatatypeholder = new Hashtable();
        //Sql Connection      
        string _mySqlUrl = "connection is correct and works in other test apps";
        string _mySqlQuery = "Query is here, it works fine in SQL management studio";

        SqlConnection conn = new SqlConnection(_mySqlUrl);
        SqlCommand comm = new SqlCommand(_mySqlQuery, conn);

        DataTable _tempTable = new DataTable();

        using (conn)
        {
            SqlCommand command = new SqlCommand(_mySqlQuery, conn);
            conn.Open();

            SqlDataReader reader = command.ExecuteReader();
            _tempTable.Load(reader);
            reader.Close();

            if (_tempTable != null && _tempTable.Rows.Count > 0)
            {
                for (int v = 0; v < _tempTable.Columns.Count; v++)
                {
                    DataColumn dc = _tempTable.Columns[v];
                    sqlDatatypeholder.Add(dc.ColumnName.ToString(), dc.GetType()); //dc.DataType //Convert.ToString(reader.GetSqlValue(v).GetType()));
                }
                foreach (DictionaryEntry hr in sqlDatatypeholder)
                {
                    Console.WriteLine(hr.Key + " " + hr.Value);
                }
            }
            else
            {
                Console.WriteLine("Connection Open - No rows found.");
            }                
        }
    }

An Answer me and another developer came up was with the help of the posts here was, Thanks to everyone who helped.

static void Main(string[] args)
        {
            Hashtable sqlDatatypeholder = new Hashtable();
            DataTable tempTable = new DataTable();
            //Sql Connection      
            string _mySqlUrl = "connection;";
            string _mySqlQuery = "query";

            SqlConnection conn = new SqlConnection(_mySqlUrl);

            using (conn)
            {
                SqlCommand command = new SqlCommand(_mySqlQuery, conn);
                conn.Open();

                SqlDataReader reader = command.ExecuteReader();
                DataTable schemaTable = reader.GetSchemaTable(); //stores datatypes from sql
                tempTable.Load(reader); //stores data rows from sql
                reader.Close();


                if (tempTable != null && tempTable.Rows.Count > 0)
                {
                    foreach (DataRow row in schemaTable.Rows)
                    {
                        sqlDatatypeholder.Add(row["ColumnName"], row["DataTypeName"]);
                    }

                    foreach (DictionaryEntry a in sqlDatatypeholder) {
                        Console.WriteLine(a.Key + " " + a.Value);
                    }

                    Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("Connection Open - No rows found.");
                    Console.ReadLine();
                }
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top