Connect to Database server-side - sql

I've essentially made a MVC application following the tutorial at http://www.asp.net/mvc/tutorials/getting-started-with-mvc3-part1-cs
I can upload it to a server and have the main page run just fine.. but running a different page that interacts with a database brings up the error
" Invalid object name 'dbo.Lyrics'. "
Now I can connect to the database that I'm trying to use (on the server) remotely using management studio just fine.. its called Lyrics and the table is Default.Lyrics ..
The connection string I'm using is "connectionString="Data Source=74.86.97.85;Initial Catalog=Lyrics;User Id=Default;Password=****;""
So my question is .. why is my application trying to use an object with the name "dbo.Lyrics" when my entire application doesn't have that text in it? How can I solve this?
I know that the dbo prefix means DataBase Owner.. and its like a public table.. but since I'm specifying a User ID shouldn't it look for tables with my ID as the prefix?

dbo at the beginning of an object name is a schema. Schemas partition the objects in your database. dbo is simply the default schema.
So, if you have an object named Lyrics, then it's really dbo.Lyrics.

Related

ODBC linked database with / in field name

I'm trying to link an ODBC database - which I have no control on - in MS Access 2007 using a Machine Data Source - I don't know if that's relevant, from what I got this means that the access is set only on this computer -.
When I follow the wizard I can select the table but when the time comes to link it I get the error message:
The database engine can't find 'WTD.DATAPOINT_5/1000'. Make sure it is a valid parameter or alias name, that it doesn't include characters or punctuation, and that the name isn't too long
I think that the problem is that one of the field is named WTD.DATAPOINT_5/1000 and that Access interprets /as a symbol of its own.
The thing is that I don't even need the data stored in this column. Right now I don't know which way to go.
Find a way to tell Access that the / is part of the field name. (Highly improbable)
Retrieve only some fields from the table using built-in Access functions.
Set the connection manually using vba and retrieving only some of the fiels. If this is the way to go I would like some pointers as I have no idea where to start.
Solution number 2: use a passthrough SQL query.
Everything is explained in this tutorial.
Solution number 3: I tried to connect directly in VBA. The code bellow works like a charm for other tables but I still get an error for the table containing the problematic field.
Dim ConnectionStr As String
ConnectionStr = "ODBC;Driver={Oracle in OraHome92};Dbq=BLA1;Uid=BLA2;Pwd=BLA3;"
DoCmd.TransferDatabase acImport, "ODBC Database", ConnectionStr, acTable, "MyTable", "NewTable"

How to Join two MongoDB Collections with the Unity JDBC driver?

From the Unity JDBC download page:
If the SQL query requires joins or functions not supported by MongoDB, then the query is promoted to UnityJDBC (trial version). The UnityJDBC trial version has no expiration date and is fully functioning except that it is limited to returning up to 100 results.
However, when I try to join two tables using any syntax like
SELECT * from a, b WHERE a.id = b.id
SELECT * from a INNER JOIN b ON a.id = b.id
SELECT * from a INNER JOIN b USING (id)
Results in the following:
Exception: java.sql.SQLException: ERROR: No schema defined. The default schema location is _schema in the current database. You need write permission to create this collection. Otherwise, use the schema parameter to set a file location (e.g. schema=mongo.xml) to store the schema. See connection parameters at http://www.unityjdbc.com/mongojdbc/ for more details.
java.sql.SQLException: ERROR: No schema defined. The default schema location is _schema in the current database. You need write permission to create this collection. Otherwise, use the schema parameter to set a file location (e.g. schema=mongo.xml) to store the schema. See connection parameters at http://www.unityjdbc.com/mongojdbc/ for more details.
at mongodb.conn.ServerConnection.processMongoWithUnity(Unknown Source)
at mongodb.conn.ServerConnection.executeQuery(Unknown Source)
at mongodb.jdbc.MongoStatement.executeQuery(Unknown Source)
at mongodb.ExampleMongoJDBC.doQuery(ExampleMongoJDBC.java:222)
at mongodb.ExampleMongoJDBC.main(ExampleMongoJDBC.java:66)
Ok, so I took a look in the readme and found it mentioning the code/test/dspec/ folder with some files related to schemas. I opened a few up, they are highly detailed xml files of all the collections mapping them to relational data types.
Do I have to write one of these out, or is there a way to auto generate it?
I received a (fast) response from the Unity team.
The MongoDB JDBC driver has two modes. For single collection queries, it does not build a schema. For queries involving joins or expressions it builds a schema and by default stores it in the _schema collection in the current Mongo database. If you do not have permission to write to the database, an error is thrown.
As mentioned in the error, you can set the schema parameter to be a local file name (such as mongo.xml) and it will store it on your computer rather than in the Mongo database. You could also use an account that has write permissions.
To make this work, add schema=mongo.xml to your connection URL like this:
jdbc:mongo://localhost/dbname?schema=mongo.xml?rebuildschema=true
After this is done the first time, you can remove the rebuildschema=true or it will rebuild it every time.
The only thing I'm confused about is that I'm using a database which doesn't require authentication. I even made a user with write permissions, connected to him, and still received the above error.
--EDIT
I realized that you could also just do jdbc:mongo://localhost/dbname?rebuildschema=true. If the schema hasn't been created, then the previous error will be thrown.

play20 ebean generated sql throws syntax error on postgresql

I'm trying to get work my play20 application with postgresql so I can use and later deploy to Heroku. I followed this answer.
Basically, I made connection to database (so connection from local application to Heroku postgresql database worked), but I was not able to initialise database with generated 1.sql evolution. But generated sql was not working because of postgresql is using schema (it should work without schema anyway, but apparently I'm doing something wrong or database is doing something wrong).
create table user (
id bigint not null,
email varchar(255),
gender varchar(1),
constraint pk_user primary key (id));
resulted in
ERROR: syntax error at or near "user"
Position: 14 [ERROR:0, SQLSTATE:42601]
I fixed that with adding schema to table name
create table public.user(
...
);
Ok, everything worked until I tried to read or write to database. I got again sql syntax exception and can't work with database. Seems like sql queries are somehow wrong.
Any suggestions where could be problem?
That's very common mistake while developing application with other database than in production, but fortunately there is also common solution. You can still use User model, however you have to make sure that creates database table with changed name:
#Entity
#Table(name = "users")
public class User extends Model {
...
}
In most cases in your controllers and models name-switch will be transparent for you. Only place where you have to remember the switch are RawSql queries.
BTW, that's good idea to install locally the same database for developing cause there's a lot of differences between most popular databases, like other reserved keywords, other allowed types, even other auto incrementing methods for id, so finding and fixing proper values is just easier on localhost.
Well, due to my little knowledge about postgresql, I was struggling with this all day. Here's simple solution. Don't use table called "user" on postgreqsl. This table is already used.
But why my evolution sql query worked for initialisation of database? Well if I explicitly specify in which schema I want to create table "user", that basically works.
But if schema is not specified, is used current schema. From documentation:
If a schema name is given (for example, CREATE TABLE myschema.mytable ...) then the table is created in the specified schema. Otherwise it is created in the current schema
So that explains it. But for my project, using "user" model was perfectly reasonable and for H2 file based databased it was working, so I assumed that problem was somewhere else...

How to identify a database as valid model?

Im having trouble identifying my databases as correct and valid version of my model. I have a GUID-id for my model I could use, but where do I put it? I dont want an entire table with only one row for this GUID. Is there any metadata-repository for databases (SQL Server 2008) or is there any other methods of identifying databases except their names? I could name the database to my model-GUID but it doesnt seem right...
The scenario is like this: I have a Entity Framework-model and I identify it by a GUID. I let the user pick a database-connectionstring and now I want to verify that its a valid database (which has been created from my app). I want to do this every time the app starts just to be sure. What Im doing now is checking that a certain table with a certain name exists but thats not good enough. I want to be 99% sure.
Edit: I would prefer to set this value from the DDL-script which is already setting up the database and I want to be able to get it from a SqlCommand in ado.net.
Use an Extended database property
In script:
USE preet;
GO
EXEC sys.sp_addextendedproperty
#name = N'myVersion',
#value = N'999-abc-123-nop';
GO
and
In the UI

How to create Sql Synonym or "Alias" for Database Name?

I'm using ms sql 2008 and trying to create a database name that references another database. For example 'Dev', 'Test', 'Demo' would be database names that i could reference from my multiple config files, but each name would point to another database such as 'db20080101' or 'db20080114'.
[Edit]Some of the configs are for applications that i control the code and some aren't (ex. MS Reporting service datasource file configs)[/Edit]
It seems that sqlserver only supports synonyms for View,Table,Sproc, or Function. And Alias' are for table and column names.
Is there a way to do this that i missed in the docs?
Any one have any suggestions on a workaround?
use 3 part notation and alias up to the table, example
select * from tempdb.dbo.sysobjects a
join master.dbo.sysobjects b on a.id = b.id
There is a way to simulate this using a linked server. This assumes you have two SQL servers with the same set of databases one for development/test and one live.
Open SQL Server Management Studio on your development/test server
Right click Server Objects > Linked Servers
Select New Linked Server...
Select the General page
Specify alias name in Linked server field - this would normally be the name of your live server
Select SQL Native Client as the provider
Enter sql_server for Product Name
In Data Source specify the name of the development server
Add Security and Server Options to taste
Click OK
The above is for SQL Server 2005 but should be similar for 2008
Once you've done that you can write SQL like this:
SELECT * FROM liveservername.databasename.dbo.tablename
Now when your scripts are run on the development server with the linked server back to itself they will work correctly pulling data from the development server and when the exact same scripts are run on the live server they will work normally.
I've done something similar to this using another config file.
The new config file maps your generic name to all of the information needed to connect to that database (db name, user name, password, etc.) and then your connection function takes your generic name as an argument.
db.config:
DEV_DB_NAME = db20080101
DEV_DB_USER = dev_user
DEV_DB_PASS = dev_pass
TEST_DB_NAME = db20070101
TEST_DB_USER = test_user
TEST_DB_PASS = test_pass
connection code:
db_connection get_connection(string prefix) {
db_connection db_conn = new db_connection;
string db_name = get_config_value(config_path, prefix + "_DB_NAME");
string db_user = get_config_value(config_path, prefix + "_DB_USER");
string db_pass = get_config_value(config_path, prefix + "_DB_PASS");
db_conn.connect(db_name, db_user, db_pass);
return db_conn;
}
Then you just call get_connection() with your db alias as the argument.
I know this probably will not help in all situations, but you still have the option of using views. You can insert, delete, update, select into a view as long as it has a proper identity key (Primary Key). If you point it to another database, you should drop and recreate to get the different schema (in case you're working between production and test while making changes to the schema in test and/or production.
Synonyms are useful for when you're going to another database and have a 3 or 4 part name, but when you want to make it so you can have a set name, a linked server will also work which will let you use a fixed name if the table names are the same in both databases and you're just pointing between prod and test.