【问题标题】:C# Reading SQL tables and viewsC# 读取 SQL 表和视图
【发布时间】:2009-03-10 23:49:34
【问题描述】:

我目前正在读取 SQL 数据库中的表列表并使用表名填充组合框。我想在同一个列表中包含 SQL 视图。表的 sysobjects 类型是“U”,视图是“V”。我将如何更改 OdbcCommand 行以检索 U 和 V?谢谢。

OdbcConnection cn=getConnection(); 
OdbcCommand cmdList; 
cmdList = new OdbcCommand("select name, user_name(uid) from sysobjects where type='U'",cn);
cn.Open();

        OdbcDataReader reader = cmdList.ExecuteReader();
        while (reader.Read())
        {
            for (int i=0;i<reader.FieldCount;i++)
            {
                if (!reader.IsDBNull(i))
                {
                    if (reader.GetName(i).ToUpper()=="NAME")
                    {
                            comboBoxTables.Items.Add(reader.GetString(i));
                    }
                }
            }
        }
cn.Close();

【问题讨论】:

    标签: c# sql views


    【解决方案1】:

    试试:

    "... where type='U' or type='V'"
    

    【讨论】:

    • 我为什么不试试呢!谢谢
    【解决方案2】:

    您使用的是什么版本的 SQL Server?如果它是 Sql 2000 之后的版本,请不要使用 sysobjects,因为它已被弃用。请改用 sys.objects。

    此外,您可能应该使用 SMO(SQL Server 管理对象)而不是编写自己的查询。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-18
      • 1970-01-01
      • 2022-01-17
      • 2014-07-26
      • 2022-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多