Hrm. It appears that I wasn't setting InitialCatalog when building the connection string, at least not in the right place. You don't have to have an initial catalog when testing a connection to a database, which is code near what I'm doing, but you do have to have it to access database tables. I was worried that it was something simple - turns out it was. Upon reflection, I'm kind of glad.
patzerFish
Posts
-
"Invalid object name" SqlException -
"Invalid object name" SqlExceptionHere's what I'm trying to do, using C#: 1 - Connect to SQL Server 2 - Check if a database is present. 3 - Create the database if it isn't. 4 - Create a login/user pair for that database. 5 - Store the connection string for future use. I have this done, confirmed by using SQL Server Management Studio Express. The following method throws an SqlException:
private static SqlDataReader executeQuery(String query) { SqlCommand command = new SqlCommand(query, Connection); SqlDataReader reader; try { openConnection(); reader = command.ExecuteReader(); return reader; } catch { throw; } finally { //connection left open because the reader is worked on elsewhere. } }
The query: SELECT ReportGroupName FROM dbo.ReportGroups The error message: Invalid object name 'dbo.ReportGroups'. The offending line of code: reader = command.ExecuteReader(); Some lines from the script I used to create the database, tables and user:
CREATE LOGIN [bim] WITH PASSWORD=N'*******', DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
EXEC sys.sp_addsrvrolemember @loginame = N'bim', @rolename = N'dbcreator'
CREATE USER [bim] FOR LOGIN [bim] WITH DEFAULT_SCHEMA=[dbo]
CREATE TABLE [dbo].[ReportGroups]I have a feeling that the problem lies with how I setup the default user, since I'm still learning databases, and it's been pretty hit or miss. However, the application worked previously using the above method with the same query string - the exception has only occurred recently, after creating the database from a script rather than in using the visual editors in SQL Server Management Studio. The script I used to create the database was derived from exports of the original database and tables, with only modifications made to create a login and a user. Any assistance on tracking down how to fix this error would be greatly appreciated.
-
Data Binding, Combo Boxes, and Related tables.Thanks for the feedback! I have begun to implement my own DAL building the CRUD (create, retrieve, update, delete) methods - but I've managed to do it without using datatables, following the model I used in a project where I was using C# to MySQL:
private static void executeNonQuery(String query) { SqlCommand command = new SqlCommand(query, Connection); try { openConnection(); command.ExecuteNonQuery(); } catch { throw; } finally { closeConnection(); } } private static SqlDataReader executeQuery(String query) { SqlCommand command = new SqlCommand(query, Connection); SqlDataReader reader; try { openConnection(); reader = command.ExecuteReader(); return reader; } catch { throw; } finally { //connection left open because the reader is worked on elsewhere. } }
These methods are the core of the DAL class. Each other static method (create, retrieve, update, delete) uses one of these two methods and pretty much has the responsibility of building a query string and passing it on. In the case of SELECT queries, a SqlDataReader is returned to be worked on. I haven't felt the need to push this data into a datatable, since it's fairly easy to just move this data into a data object or List<> of data objects. It will require more work on the side of binding data to form controls, but I think I prefer having more control over what is going on anyway. Thanks again for your quick response!
-
Data Binding, Combo Boxes, and Related tables.I have two sql tables with the following layout: Table: Calls ID - int, PK ReportGroupID - int, FK -> ReportGroups.ID Name - nvarchar(50) PhoneNumber - nvarchar(50) (other data items) Table: ReportGroups ID - int, PK ReportGroupName - nvarchar(50) I'm developing in C#, VS 2008. The windows forms interface has the following elements: listbox - lists the Data in Calls.PhoneNumber - I have this data bound to a dataset that has this data. form controls - when a user selects an entry in the listbox, the data is displayed in these controls. - Fairly straightforward data binding. report groups combo box - this combo box will display the ReportGroupName based on the Calls.ReportGroupID of the selected item in the listbox. - HELP!! I managed to get this working to some degree at one point, but saving the data did not work - despite the dataset being persisted between changing of records. Here's what I really want help with: 1) I have not been able to find a good resource that goes in depth into the whole Dataset->DataAdapter->TableAdapter->TableAdapterManager relationship. Any pointers in the right direction either online or printed would be great. I feel like I'm just on the cusp of getting the right information out. 2) If there is a specific and simple way I am missing the implementation of this sort of behavior by using the Visual Studio designers, it would really help things along on my end. Thanks!
-
.dll file are not replacing when install new set up.I just had a similar problem. Get Orca, a program which is part of the Microsoft SDK. Open your .msi installer file in Orca. Select properties from the left hand side. Insert a new row into the table: - Property: REINSTALLMODE - Value: amus This will cause your installer to overwrite previous versions when installing. Remember for each release that you need to update the Version property for the Setup project, which (in VS 2008) will prompt you to create a new ProductCode GUID.