SQLCommand没有在Writeline中返回任何内容

时间:2014-03-21 09:59:37

标签: c# .net datatable hashtable sqlcommand

我目前正在尝试为我们的数据库建立一个sqlconnection,它将返回所有列/行的数据类型(这需要完成,以便我们可以将数据输入到API中)。此数据放在数据表中,然后通过哈希表获取类型。

问题是当程序运行时,不会发生错误,但它不会返回已指定的控制台writelines中的任何内容。我不是一个经验丰富的.net开发人员,所以我不确定我错过了什么,但我的猜测是按照sql命令/连接的打开顺序排列的?

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();

}

3 个答案:

答案 0 :(得分:3)

你的_tempTable Table是空的,你没有填写,这就是你无法得到任何东西的原因。尝试此代码希望它会有所帮助。

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

您也可以使用此代码

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();             
    }
}

答案 1 :(得分:1)

我看到在哈希表中获取值所需的以下更改。注意异常处理必须尝试添加它:

    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.");
            }                
        }
    }

答案 2 :(得分:0)

答案我和其他开发人员在这里的帖子的帮助下,感谢所有帮助过的人。

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();
                }
            }
        }