[英]Error while gathering tablenames from Database c# and asp.net
數據庫名稱為ONLINEEXAM
我在數據庫中有幾個表,我想列出一些表名,在asp.net的Dropdownlist中以字母“ set%”開頭。
我使用以下代碼,但出現錯誤: 無效的對象名稱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 { }
}
可能是您正在針對其他數據庫運行查詢,請在sql服務器中運行查詢以對其進行檢查。 也試試這個
SqlCommand cmd = new SqlCommand("select * from sys.objects where name like 'Set%'", con);
或使用它來獲取所有表格
select * from sys.tables where name like 'Set%'
看一下以確認用戶ID可以訪問sysobjects
表。 假設您正在運行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.