adapter.Fill方法中的“无效对象名称”错误

时间:2012-10-09 23:20:48

标签: sql ado.net dataset

我正在尝试从AdventureWorks2012数据库的所有表中创建一个DataSet。但是我在adapter.Fill方法中遇到“Invalid object name”错误。

有什么想法吗?

private void openDBButton_Click(object sender, RoutedEventArgs e)
    {
        DataSet myDataSet = new DataSet();
        SqlDataAdapter myDataAdapter;
        SqlCommand myCommand;

        string conStr = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + dbPath.Text +
            ";Integrated Security=true";
        SqlConnection connection = new SqlConnection(conStr);
        connection.Open();

        DataTable tables = connection.GetSchema("Tables");

        foreach (DataRow table in tables.Rows)
        {
            if (table["TABLE_TYPE"].ToString() != "BASE TABLE")
                continue;
            string tableName = table["TABLE_NAME"].ToString();
            string sqlCmd = "SELECT * FROM " + "[" + tableName + "]";
            //SqlDataAdapter adapt = new SqlDataAdapter(new SqlCommand(sqlCmd, connection));
            //adapt.FillSchema(myDataSet, SchemaType.Mapped, tableName);

            myCommand = new SqlCommand(sqlCmd, connection);
            myDataAdapter = new SqlDataAdapter(myCommand);
            myDataAdapter.Fill(myDataSet, tableName); // here i'm get in trouble :)
        }
    }

0 个答案:

没有答案