Connect to SQL Server CE with ADODB or LINQ - sql-server

I'm trying to access a Sql Server Compact Database.
It's a clickonce application, so I'd like it if the database can be created when the application is installed.
I got it so that when the application is started the database is created by using SqlCeEngine, SqlCeConnection, etc.
However, querying and inserting this way is complicated, so I was hoping to get it working with ADODB.
Dim MyCn As New ADODB.Connection
MyCn.Provider = "Microsoft.SQLSERVER.CE.OLEDB.3.5"
MyCn.ConnectionString = My.Settings.LocalConnectionString
MyCn.Open()
Dim rSelect As New ADODB.Recordset
With rSelect
.Open("Select wID, DirPath, Children From Watches Where DirPath like '" & dialog.SelectedPath & "'", MyCn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)
If .EOF Then
.AddNew()
.Fields!DirPath.Value = dialog.SelectedPath
.Fields!Children.Value = True
.Update()
End If
.Close()
End With
but I get an error:
In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user.
Alternately, I wouldn't mind learning how to use LINQ to SQL, as 3.5 supports it, but I haven't found how to connect to a database that might not exist until the program starts for the first time, meaning I can't use the database wizard.

It's just an opinion, why not use entity framework:
Here is a way to make the connection manual
It's just an opinion, I hope the same helps you...

You're using a SQL Server CE, this article is the walk-through for your application
http://blogs.msdn.com/b/stevelasker/archive/2008/02/11/stored-procedures-and-sql-server-compact-the-great-debate.aspx

Found some tutorials to use LINQ to SQL.
It's not quite as easy to use as ADODB is with regular SQL Server, since you have to make classes for each table, but it's not to bad.

Related

Use database with linked tables in Access 2000 or use the ADODB.RecordSet - Which is better?

I am reworking old VB6 apps into VB.NET with Visual Studio 2005 and all of them suffer from the same problems.
One of those is, access the DB2 database by using an Access 2000 file which has links to the tables and second approach is, using ADODB.RecordSet with concatenated string SQL queries directly run on to the database.
I know I can use Linq which is the right tool for the job, but I don't have time to learn it at the moment. I have to finish this job quickly.
Examples:
Function selectNA_FromMyTable_ByNA(ByVal na As String) As String
Dim sql As String = "SELECT na FROM DB2Scheme.MyTable "
sql = sql & "WHERE (na = '" & na & "')"
Return sql
End Function
and
Function selectNA_FromMyTable_ByNA(ByVal na As String) As String
Dim sql As String = "SELECT na FROM DB2Scheme_MyTable "
sql = sql & "WHERE (na = '" & na & "')"
Return sql
End Function
where DB2Scheme_MyTable is link to DB2Scheme.MyTable table.
I don't like mixing approaches although they both work properly.
Which is better approach?
Which approach would be better for debugging? For example, how can I detect that the user using the application does not have privileges to write or read data from a certain table in the scheme?
well certainly eliminating the Access database would be ideal. i assume in the prior model, the Access database also served as the front-end? if you're moving to .net, then i don't see the point in keeping the access database. go directly to the db2 database, just be mindful of the database drivers that may need to be installed when distributing this app. less of a problem if its a web app.
generic error handling in .net should reveal if users have access issues. you may want to check that up front when your application is launched if your application is going to use the users credentials, and not a user id of its own to access the database. I am not sure this should be something of a concern when rewriting the app, are you not using the same authentication, or logon credentials used in the formal app?

Script to replicate SQL Server data dynamicaly

I have an access 2010 application with a SQL Server database.
But I need to do an offline version. So I thought I would create a local SQL Server database on their computers then they can run a script to update their data before they go on the road.
NOTE: There won't be any sync. The data in offline mode is only for read-only and any changes will be lost.
I tried with Management Studio like this:
But I realized that the data is hard coded instead of doing inserts from selects.
Is there any easy way to create the script?
What I have so far is my pass through query in access to create the backup of my online database.
Then I have my pass through query to restore the backup to the local server.
The only problem is how can I build the connection string for the second query. It's currently set to this one
ODBC;DRIVER=SQL Server;SERVER=010-068\SQLEXPRESS;UID=marcAndreL;Trusted_Connection=Yes;DATABASE=SMD
but because it's a different database for everyone, it won't work.
How can we build a custom connection string?
I am using SQL Server Express 2012 and Windows Authentication, so using the answer provided here, I find this works for me:
Sub TestCon()
Dim cn As New ADODB.Connection
cn.Open ServerConLocal
End Sub
Function ServerConLocal()
''OleDB Connection
ServerConLocal = "Provider=sqloledb;Data Source=localhost\SQLEXPRESS;" _
& "Initial Catalog=Test;Integrated Security=SSPI;"
End Function
For an ODBC connection string in a Pass-through query, this works for me:
ODBC;Driver={SQL Server Native Client 11.0};Server=localhost\SQLEXPRESS;Database=test;
Trusted_Connection=yes;
Take a look at download-only articles for merge replication. MSDN.

MSAccess 2007 changing connection properties in form

I would like some help in creating a form with-in an MS Access front end to select a database/database server for the access backend.
A little history first;
I created a MSAccess 2007 acccdp database, it was designed as a single user to be run locally. (For a university research project with little or no funds for this).
Eventually there was a need to expand the database to be used by several people at once in the same office so what i did;
Install SQLExpress 2008r2 on a desktop.
Used Database Tools > Move Data > Sql Sever to migrate the tables to the new SQL server.
Now I want to create a second test database for development/training etc... Also, I need to adjust the properties for a different database sever (at home).
So I figure I need a form to auto exec (rather than the switchboard) which will allow you to select the database server and Database to join the linked tables to, then if successfully connects to open the switchboard.
I can create the form, but i need help with the vba to adjust the database connection properties.
I am just trying to help this project and am not a programmer in any way.
Thanks in advance
Roger
If your choice is between two connections, you can check if the existing connection is working and if not, switch to the second. However, a button may still be the best bet. This snippet works for me, but it sets the connection timeout to just a second, so the server may be available if you had only waited a little longer, and even with this time out, there is enough of a delay for an impatient person to start clicking.
Function IsSQLServer() As String
''Reference Microsoft ActiveX Data Objects x.x Library
Dim cn As New adodb.Connection
''http://msdn.microsoft.com/en-us/library/windows/desktop/ms676718(v=vs.85).aspx
''Provider=sqloledb
cn.ConnectionString = ServerCon & "Connect Timeout=1;"
On Error Resume Next
cn.Open
If Err.Number <> 0 Then
IsSQLServer = "No;" & Err.Description
Err.Clear
Else
If cn.State = adStateOpen Then
IsSQLServer = "Yes"
Else
IsSQLServer = "Maybe"
End If
cn.Close
End If
Set cn = Nothing
End Function

How do I automatically compact and repair an Access 2007 database?

I have a site that uses classic ASP and a database in Access 2007 format (.accdb). It is encrypted with a password and is about 300 MiB in file size.
The site works fine by itself but every now and then the database gets corrupted (the error is "unrecognized database format"). It can be fixed easily by opening the database in MS Access, then it will be repaired. Problem is it can take days before I notice the database is corrupted and during that time the site will be useless. Sometimes it takes months between each corruption, other times only a week or two.
What I want is the site to be able to call the "Compact and Repair Database" function itself every now and then (once a day or so) to keep the database in a working condition.
My question is how do I do this, make it repair itself?
I found this article: How do I compact and repair an ACCESS 2007 database by .NET code?
...but I don't understand how I can make that work for me. I only know classic ASP and Java.
Can anyone write a little isolated ASP code that does this: "open connection to password-protected database", "repairs the database", "close the connection".
The site and database is on a dedicated server which I have full control over so I can implement any solution that exists.
Thank you very much!
You can write a script that repairs the database every now and then. But there are two problems:
1) How to detect the database is corrupt
2) How to minimize data loss.
The biggest problem is that you are trying to fix a problem which lies within the access database itself (frequent data corruption). Because of this, the product is not suitable for any serious application. So why don't you switch to a more reliable database? (MS SQL, MYSQL, ORACLE, SQL Lite, and more to choose from).
Can SQL Server Express be legally licensed for use on a web server? If so then I'd suggest moving your data to it instead. If not there are other options mentioned elsewhere. At the following page SQL Server 2008 Express I see mention of a Web Platform Installer so that would imploy you can use it on a web server. But I do not interpret EULAs so I leave that decision to you.
I got the answer I needed in another question I asked:
Why can't I use "CompactDatabase" in DAO.DBEngine.36 using VBscript?
The user "HansUp" gave me the following code:
Dim objFSO
Dim objEngine
Dim strLckFile
Dim strSrcName
Dim strDstName
Dim strPassword
strLckFile = "C:\Access\webforums\foo.laccdb"
strSrcName = "C:\Access\webforums\foo.accdb"
strDstName = "C:\Access\webforums\compacted.accdb"
strBackup = "C:\Access\webforums\foobackup.accdb"
strPassword = "foo"
Set objEngine = CreateObject("DAO.DBEngine.120")
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not (objFSO.FileExists(strLckFile)) Then
If (objFSO.FileExists(strBackup)) Then
objFSO.DeleteFile strBackup
End If
If (objFSO.FileExists(strDstName)) Then
objFSO.DeleteFile strDstName
End If
objFSO.CopyFile strSrcName, strBackup
''dbVersion120 = 128
objEngine.CompactDatabase strSrcName, strDstName, , 128, ";pwd=" & strPassword
objFSO.DeleteFile strSrcName
objFSO.MoveFile strDstName, strSrcName
End If 'LckFile
(The code got a few extra line breaks when I copied it for some reason, follow the link at the beginning of this post for a little cleaner version of the code if you wish.)
It compresses a Access 2007 database to a Access 2007 database (no format change) AND it also repairs any corruption (inconsistent state) in the database! Just what I was looking for. =)

Updating access 2000 database through code in VB6

I have an application that uses an access 2000 database currently in distribution.
I need to update one of the recordsets with additional fields on my customer's computers.
My data controls work fine as I have them set to connect in access 2000 format. But, when I try to open the database in code, I get an unrecognized data format error.
What is the best way to replace or add to the database on their machines?
It is possible to update an Access database using VBScript, ADO and DDL.
strCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Example.mdb;" _
& "Jet OLEDB:Database Password=pass;"
Set cn=CreateObject("ADODB.Connection")
cn.Open strCon
strSQL="ALTER TABLE Example ADD COLUMN Example Text (20)"
cn.Execute strSQL
More connections strings: www.connectionstrings.com
I much prefer using DAO collections to updating BE database schemas as it gives you much more control over what you can do. For example you can easily delete or create tables, records, indexes and relationships. See the TempTables.MDB page at my website which illustrates how to use a temporary MDB in your app and has sample code to get you started.

Resources