简体   繁体   English

从数据库c#和asp.net收集表名时出错

[英]Error while gathering tablenames from Database c# and asp.net

Database Name is ONLINEEXAM 数据库名称为ONLINEEXAM

I have several tables in the db and I want to list some table names starts with letters "set % " in Dropdownlist in asp.net. 我在数据库中有几个表,我想列出一些表名,在asp.net的Dropdownlist中以字母“ set%”开头。

I use the following code and I'm getting the error : invalid object name ONLINEEXAM.dbo.sysobjects 我使用以下代码,但出现错误: 无效的对象名称ONLINEEXAM.dbo.sysobjects

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack == false)
    {
        paperset();
    }  
}


private void paperset()
{
    try
    {
        string conn = ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString;
        SqlConnection con = new SqlConnection(conn);
        con.Open();

        SqlCommand cmd = new SqlCommand(
            "select * from ONLINEEXAM.dbo.sysobjects where name like 'Set%'", con);
        SqlDataReader dr = cmd.ExecuteReader();

        while (dr.Read())
        {
            ListItem item = new ListItem();
            item.Value = dr[0].ToString();
            papersetlist.Items.Add(item);
        }

        dr.Close();
        con.Close();
    }
    catch (System.Exception ex) 
    { 
        MessageBox.Show(ex.Message); 
    }
    finally { }
}

May be you are running query against a different database, run you query in sql server to check it. 可能是您正在针对其他数据库运行查询,请在sql服务器中运行查询以对其进行检查。 also try this 也试试这个

SqlCommand cmd = new SqlCommand("select * from sys.objects where name like 'Set%'", con);

or use this to get all the tables 或使用它来获取所有表格

select * from sys.tables where name like 'Set%'

Take a look to verify the user ID has access to the sysobjects table. 看一下以确认用户ID可以访问sysobjects表。 Assuming you are running SQL2005 or later, you can also look at the INFORMATION_SCHEMA schema and review the TABLES view: 假设您正在运行SQL2005或更高版本,则还可以查看INFORMATION_SCHEMA模式并查看TABLES视图:

Select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE 
from [database.]INFORMATION_SCHEMA.TABLES

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM