English 中文(简体)
Invalid Object Name: Beginner using the AdventureWords db from a class
原标题:

I m trying to learn some C#.net. I m just trying to expose the AdventureWorks database included in my C# class via a web interface. Here s the setup:

I ve got a DropDownList in on my ASPX page with an id of tableNameDropDown. It gets populated on Page_Load like this:

protected void Page_Load(object sender, EventArgs e)
    {
        conn.Open();

        String table_names_sql = "select Name from sysobjects where type= u  ORDER BY name";
        SqlCommand cmd = new SqlCommand(table_names_sql, conn);
        SqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {

            tableNameDropDown.Items.Add(reader[0].ToString());
        }

        conn.Close();

        tableNameDropDown.AutoPostBack = true;
    }

And that works just fine, I get a nice long list of the tables in the DB. When someone selects a table from the list, I want to display that table in a GridView control with an id of grid. This is what I ve got:

protected void tableNameDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataSet dataSet = new DataSet();

        String tableName = columnNameDropDown.SelectedItem.ToString();
        String table_sql = String.Format("SELECT * FROM {0};", tableName);
        SqlDataAdapter adapter = new SqlDataAdapter(table_sql, conn);
        adapter.Fill(dataSet, tableName);

        grid.DataSource = dataSet;
        grid.DataMember = tableName;
    }

When I debug the page, I get an error on the adapter.Fill(dataSet, tableName); line: SqlException: Inlvalid object name {tableName} .

The tables in the DB are the following:

dbo.AWBuildVersion
.... more dbo. tables

HumanResources.Department
HumanResources.Employee
.... more HumanResources tables

Person.Address
Person.AddressType
.... more Person tables

... Other prefixes are "Pdoduction, Purchasing, Sales"

There are probably ~50+ tables, and I get all their names (without the prefixes) into my DropDownList no problem, but I can t seem to query them.

Any ideas?

最佳回答

You ve already answered yourself: you need to use also the prefix in the select statement you re executing, like

Select * From Person.Address

Beside that you should not use the sysobject tables, from SQL Server 2005 you have system views that helps you, so you can write a better statement to select tables:

select * From INFORMATION_SCHEMA.TABLES

Check also this article.

Regards Massimo

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签