Data in SQL wont sync with Linq DataContext using traditional SQL syntax - sql

I perform selects with linq to sql in my app and other things (insert,update,delete) by traditional sql syntax.
But, when I insert or delete something, my datacontext doesn't update. So, if I search for the ID of selected object, an empty query returns. I checked the DB and I'm sure that sql syntax works fine.
In short, when I do something with my DB with sql syntax it changes my db but not the DataContext instance!

Are you saving the data? E.g. calling Context.SaveChanges() or whatever the equivalent is?

Link to SQL DataContext have a cache system. Look at the DataContext.Refresh method group for methods to refresh your entities. Another simple solution is to reinstanciate your DataContext after doing some raw SQL.

If you want to use Linq to Sql you should do all operations with that,It means if you want to do some raw Sql still you should use Linq to Sql syntax if not your link to data base won't update.
So I switched to Linq to Sql completely.
Thanks All
Useful info : http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-1-introduction-and-model.aspx

Related

Alternate codes for ExecuteReader

It is my first time to post here. My question is what code can we use instead of ExecuteReader? We're currently doing a total production program and we want to input the productions, Projects 1-4 (I label it P1-P4) into the text box, but i have read that ExecuteReader is only for SQL, we're only using Access DB. Our code for displaying the projects to textboxes
You apparently don't know what "SQL" is. SQL (structured query language) is not a database. It is a language that databases use to define and manipulate data. Just about all databases use SQL for this purpose, including Access.
There are a number of databases with "SQL" in the name, e.g. SQL Server, MySQL and SQLite, but there's no database named "SQL". Some people lazily refer to Microsoft's SQL Server as just SQL or maybe MS SQL but its name is SQL Server.
As for using ExecuteReader, it's for any database. If you were connecting to SQL Server then you'd use a System.Data.SqlClient.SqlCommand object and call ExecuteReader on that. As you're using Access, you need to use a System.Data.OleDb.OleDbCommand and call ExecuteReader on that. They both work the same way but the different providers are for different data sources.

Moving from Oracle SQL to ANSI SQL pros and cons

I work in a project where the UI has direct access to the database through SQL code. The company has a framework where we create UI pages in xml and after that it is parsed and creates FLEX pages. If we want some data from the DB (Oracle) we add a sql query in the xml (instead of databinding with a datacontext object like we could do with WPF). If we want to add some logic, there is no code behind, we call store procedures. After we have the data we need the parser does the job.
The new requirements are to use the framework and create a new product that will be compatible with SQL Server and the thoughts are to start transforming the (Oracle)SQL queries to ANSI SQL.
Can somebody tell me the benefits and mainly the problems that we are going to face doing that?
Do you think there is a better way?
Note: The framework is really big and there are a lot of products built on that so managers are not keen to just throw it away(I tried but.. :))
Each dialect of SQL is different. You could use ANSI SQL but a) not all of ANSI SQL is implemented by most DBMS and b) most DBMS's have implementation-specific optimisations which will perform better for some cases.
So I'd say, don't go for ANSI SQL. It won't always work and sometimes it will work slower than taking advantage of a vendor's non-standard implementations.
Specifically, Oracle requires a StoredProcedure to return a REF_CURSOR from a stored procedure to fill a DataSet. SQL Server doesnt; the SP returns what the sp SELECTed. You're going to have to change your SP's to get rid of the returned REF_CURSOR.
Date handling is quite different: Oracle needs a to_date to turn a string into a date in where clauses etc; SQL Server just takes the string and converts it for you. And so on and so on. (I'm not at all sure what the ANSI Standard is, or even if it covers this!) To avoid changing your SQL you could add create SQL Server function called to_date, but this is now going to slow up your SQL.
If you have much PL/SQL in stored procedures, you have a big job converting it to T-SQL. They are quite different.
Good luck!

How does Linq to SQL do DeleteAllOnSubmit?

I don't want to know how to do it, I want to know the underlying code. Every google query comes up as a tutorial on how to delete from the database.
It's not clear what exactly you mean. But I'll take a stab at it. For each entity you want to delete, L2S issues a single T-SQL Delete statement to delete the entity. Therefore, if you are deleting say 20 entities, 20 Delete statements will be issued to SQL Server.
As Randy says, it does indeed issue individual SQL deletes for each entity :(
This is obviously very inefficient compared to a single SQL "delete where"... therefore, it seems, people write SQL and pass it through to ExecuteCommand instead.
Alternatively, you can use the DeleteBatch extensions discussed here:
http://www.aneyfamily.com/terryandann/post/2008/04/Batch-Updates-and-Deletes-with-LINQ-to-SQL.aspx

Hibernate complex query

I am trying to execute a query against a MySQL database.
The query is fairly complex it has 5 inner joins, including 1 join to itself and
it returns 3 pieces of information from 2 different tables.
We are using hibernate and till now I have used it for simple queries only.
I have written the sql query and tested it too. I am wondering how to implement this using
hibernate, can I execute plain sql statements with hibernate? If so what do I need, a separate hbm.xml?
If I use hibernate and execute the plain sql query can I still utilize caching later on?
Yes, you can execute plain SQL queries with Hibernate.
No, you don't need a separate hbm.xml mapping file (unless you WANT to separate sql queries from the rest, in which case you can do so). You can map your named SQL query the same way you do with named HQL queries.
Whether you will be able to "utilize caching" depends on what exactly you understand by "caching" and how you're going to map your SQL query; it's impossible to answer without knowing more details.
All that said, you may not need to resort to SQL query; HQL is quite powerful and it may very well be possible (assuming appropriate mappings exist) to write your query as HQL. Can you post relevant mappings / schemas and your SQL query?
I strongly recommend criteria queries over HQL queries. They are much closer to your program code without sacrificing any expression power. They DO however depend on relations to be explicitly mapped, otherwise they get quite complicated.
To speed up development, set property hibernate.show_sql=true, and play with the system in the debugger, using the "reload modified class" and "drop stack frame" features of the IDE+jvm until the SQL emitted looks like the one you've posted.

Is there an Access equivalent of the SQL Server NewId() function?

I have written SQL statements (stored in a text document) that load data into a SQL Server database. These statements need to be repeated daily. Some of the statements use the NewId() function to populate a keyed field in the database, and this works fine.
While I'm in the process of writing an application to replicate these statements, I want to use Access queries and macros instead of copying and pasting queries into SQL Server, thus saving me time on a daily basis. All is working fine but I can't find any function that will replace the SQL Server NewId() function. Does one exist or is there a work around?
I'm using SQL Server 2005 and Access 2007.
On top of matt's answer, you could simply use a pass-through query and just use your existing, working queries from MS Access.
A solution would be to insert the stguidgen() function in your code, as you can find it here: http://trigeminal.fmsinc.com/code/guids.bas https://web.archive.org/web/20190129105748/http://trigeminal.fmsinc.com/code/guids.bas
The only workaround I can think of would be to define the column in your access database of type "Replication ID" and make it an autonumber field. That will automatically generate a unique GUID for each row and you won't need to use newid() at all. In SQL server, you would just make the default value for the column "newid()".
Again, there seems to be confusion here.
If I'm understanding correctly:
You have an Access front end.
You have a SQL Server 2005 back end.
What you need is the ability to generate the GUID in the SQL Server table. So, answers taht suggest adding an AutoNumber field of type ReplicationID in Access aren't going to help, as the table isn't a Jet table, but a SQL Server table.
The SQL can certainly be executed as a passthrough query, which will hand off everything to the SQL Server for processing, but I wonder why there isn't a default value for this field in SQL Server? Can SQL Server 2005 tables not have NewId() as the default value? Or is there some other method for having a field populate with a new GUID? I seem to recall something about using GUIDs and marking them "not for replication" (I don't have access to a SQL Server right at the moment to look this up).
Seems to me it's better to let the database engine do this kind of thing, rather than executing a function in your SQL to do it, but perhaps someone can enlighten me on why I'm wrong on that.