当前位置 : 主页 > 网络推广 > seo >

检索SQL表中的列数 – C#

来源:互联网 收集:自由互联 发布时间:2021-06-16
我对C#很新.我正在尝试使用以下方法检索列数: SELECT count(*) FROM sys.columns 您能解释一下如何使用该命令并将其放入变量中. string connectionString = "Data Source=(local);Initial Catalog=Northwind;" + "
我对C#很新.我正在尝试使用以下方法检索列数:

SELECT count(*) FROM sys.columns

您能解释一下如何使用该命令并将其放入变量中.

string connectionString =
            "Data Source=(local);Initial Catalog=Northwind;"
            + "Integrated Security=true";

        // Provide the query string with a parameter placeholder.
        string queryString =
            "SELECT Count(*) from sys.columns";

        // Specify the parameter value.
        int paramValue = 5;

        // Create and open the connection in a using block. This
        // ensures that all resources will be closed and disposed
        // when the code exits.
        using (SqlConnection connection =
            new SqlConnection(connectionString))
        {
            // Create the Command and Parameter objects.
            SqlCommand command = new SqlCommand(queryString, connection);

            // Open the connection in a try/catch block. 
            // Create and execute the DataReader, writing the result
            // set to the console window.
            try
            {
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine("\t{0}",
                        reader[0]);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
网友评论