473,774 Members | 2,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Connect to SQL Server CE

15 New Member
I am new to mobile development and for now I'm trying to create a mobile application using SQL Server CE as database. Could someone please provide a sample code in vb that will connect to SQL CE? Or the connection string at least?
Any help would be greatly appreciated
Jul 18 '08 #1
10 7583
markmcgookin
648 Recognized Expert Contributor
Expand|Select|Wrap|Line Numbers
  1. Private Sub btnCreateDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateDB.Click
  2.         'Verify if the WildFlowers database already exists.
  3.         If File.Exists("WildFlowers.sdf") = False Then
  4.             'Allow creation of the database
  5.             Dim engine As New SqlCeEngine("Data Source = WildFlowers.sdf")
  6.             engine.CreateDatabase()
  7.             MsgBox("Wildflowers database successfully created.", MsgBoxStyle.OKOnly, "Create DataBase")
  8.         Else
  9.             MsgBox("Wildflowers database already exists, no action taken.", MsgBoxStyle.OKOnly, "Create DataBase")
  10.         End If
  11.     End Sub
  12.  
  13.     Private Sub btnCreateTables_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateTables.Click
  14.         'Create connection with database and then Open connection
  15.         Dim conn As New SqlCeConnection("Data Source = WildFlowers.sdf")
  16.         Try
  17.             conn.Open()  'Open Connection to the Database
  18.  
  19.             'Create SQL command to create the tblDetails table
  20.             Dim cmdTabla As New SqlCeCommand("CREATE TABLE tblDetails(regID int IDENTITY(0,1) PRIMARY KEY, Flower_ID NTEXT, Name_Eng NTEXT, Name_Lat NTEXT, Location_ID NTEXT, Habitat_ID NTEXT, Season_ID NTEXT, Family_ID NTEXT, Height NTEXT, Flower_Shape NTEXT, Leaf_Shape NTEXT, Colour NTEXT, Plant_Type NTEXT, Bloom_Size NTEXT, Cluster_Type NTEXT, Description NTEXT)", conn)
  21.             'Excecute Create Table command
  22.             cmdTabla.ExecuteNonQuery()
  23.  
  24.         Catch ex As SqlCeException
  25.             MsgBox(ex.Message)
  26.  
  27.         Finally
  28.             MsgBox("Tables were successfully created.", MsgBoxStyle.OKOnly, "Create Tables")
  29.             conn.Close() 'Close database
  30.         End Try
  31.     End Sub
  32.  
Hope this helps... it is connection to a database in the root of the PDA ... so if the database is somewhere else ... i.e. \Program Files\MyApp\MyD b.sdf the path will have to represent that.

There are two subroutines in the above code the first creates the database and the second connects to the database and executes some SQL to create a table

Hope that helps

Mark
Jul 18 '08 #2
memermore
15 New Member
Thanks Mark. This is better. Just a few question though. Suppose I created the database on runtime based on your code, where can I find it? Or suppose i have created the database somewhere(like SQL server 2005), where should I place my SDF file to avoid problems in my connection string? Actually I think my problem here is my connection string.
Jul 21 '08 #3
markmcgookin
648 Recognized Expert Contributor
Thanks Mark. This is better. Just a few question though. Suppose I created the database on runtime based on your code, where can I find it? Or suppose i have created the database somewhere(like SQL server 2005), where should I place my SDF file to avoid problems in my connection string? Actually I think my problem here is my connection string.
Personally I tend to keep the database in my application folder to avoid issues.

i.e.

Expand|Select|Wrap|Line Numbers
  1. Dim conn As New SqlCeConnection("Data Source = \Program Files\MyApplication\myDatabase.sdf")
Then when you deploy using Visual Studio it will be in the right place. If you have already created the database and deployed it to the device but can't connect. Just use File Explorer to navigate to the folder to get the path.

If you use my code above, as I have not specified a path for the db it will be created in the device root.

Are you getting an error?

Hope this helps,

Mark
Jul 21 '08 #4
memermore
15 New Member
Thanks. I can finally connect to my database, I tried to create a function that will add record to my table tblProducts which is empty at first

Private Sub add_record()
cn = New SqlCeConnection (Data Source=" + (System.IO.Path .GetDirectoryNa me(System.Refle ction.Assembly. GetExecutingAss embly().GetName ().CodeBase)) + "\\productsDB.s df;Persist Security Info=False;)

cn.Open()

cmd = New SqlCeCommand("I NSERT INTO tblProducts(Pro ductID, ProductName) VALUES(" & txtID.Text & ", '" & txtName.Text & "')", cn)

cmd.CommandType = CommandType.Tex t

cmd.ExecuteNonQ uery()

cn.Close()

MessageBox.Show ("New record added")
End Sub

At first I thought this was working fine, my datagrid control was updated by the new record I added but when restart my application, the newly added record was gone. And tblProducts is still empty. Is there something wrong in my code?
Jul 22 '08 #5
freedom021
1 New Member
bump........... ............... ............
Jul 22 '08 #6
markmcgookin
648 Recognized Expert Contributor
Thanks. I can finally connect to my database, I tried to create a function that will add record to my table tblProducts which is empty at first

Private Sub add_record()
cn = New SqlCeConnection (Data Source=" + (System.IO.Path .GetDirectoryNa me(System.Refle ction.Assembly. GetExecutingAss embly().GetName ().CodeBase)) + "\\productsDB.s df;Persist Security Info=False;)

cn.Open()

cmd = New SqlCeCommand("I NSERT INTO tblProducts(Pro ductID, ProductName) VALUES(" & txtID.Text & ", '" & txtName.Text & "')", cn)

cmd.CommandType = CommandType.Tex t

cmd.ExecuteNonQ uery()

cn.Close()

MessageBox.Show ("New record added")
End Sub

At first I thought this was working fine, my datagrid control was updated by the new record I added but when restart my application, the newly added record was gone. And tblProducts is still empty. Is there something wrong in my code?
When you say "restart" your application did you mean that you ran debug again and deployed it to the mobile again? Chances are that the database is being copied across ontop of the original again if this is the case. Because it looks like the insert it going in properly. If it is in the solution explorer in VS you can click on properties > build action > Do not copy to avoid this (but you will obviously need it copied across at least once to show up!)
Jul 22 '08 #7
memermore
15 New Member
Yes, I did ran debug and deployed my application. I actually created my database in VS and is located in the application folder. What must I do to make sure that the database table is really updated?
Jul 23 '08 #8
markmcgookin
648 Recognized Expert Contributor
Yes, I did ran debug and deployed my application. I actually created my database in VS and is located in the application folder. What must I do to make sure that the database table is really updated?
Well when you are deploying your application you are RE-deploying the database ontop of the old one and over-writing it. A safe bet would be to deploy your application and add your row to the database. THEN run this without copying over the database; you can either stop copying it as mentioned above or go to

File Explorer (on your device/emulator) > Program Files > YourApplication > YourApplication .exe

and run it directly from the device. If you do this it will run the application without copying over a new blank database and your row should be in it.

When you deploy an application it re-copies any files you have added or altered and datasources, including your database, this is why it is getting over written.
Jul 23 '08 #9
eqquito
1 New Member
Personally I tend to keep the database in my application folder to avoid issues.

i.e.

Expand|Select|Wrap|Line Numbers
  1. Dim conn As New SqlCeConnection("Data Source = \Program Files\MyApplication\myDatabase.sdf")
Then when you deploy using Visual Studio it will be in the right place. If you have already created the database and deployed it to the device but can't connect. Just use File Explorer to navigate to the folder to get the path.
You cant imagine how thankfull I am, thanks for the great explanation. Sometimes we the devs are stuck in something easy like where the heck I place my db file so the emulator can use it since it is not reading from my computer. I searched lot and lots of pages, sample code and I created my own samples.

I just registered to say thanks. Now will also help if I can.
Oct 5 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
3070
by: Jerry Orr | last post by:
We are trying to determine the easiest (and cheapest) way to get connections for about 50 Win2K workstations on our LAN to DB2 on a z/OS server. We could install DB2 Connect Personal Edition on every workstation, but that would require purchasing a license for each workstation (or, more likely, a site license). We would like to avoid that option. I've heard mixed accounts of what DB2 Connect ENTERPRISE Edition can do. One source says...
4
5603
by: Scott Holland | last post by:
HELP - Need to connect to DB2 database on AIX from NT server. Also AS/400 from NT Server -- I am experienced in ORACLE and a novice at DB2. What tools would be the equivalent of Net*8 or SQLNET in ORACLE that would allow me to connect to a database on a remote box (either AIX or AS/400)? I have read that DB2 Connect appears to be the ticket for the Db2 server if it is on AS/400 or OS/390. What if it is on AIX? Does the same tool...
0
4546
by: Zorba.GR | last post by:
IBM DB2 Connect Enterprise Edition v8.2, other IBM DB2 (32 bit, 64 bit) (MULTiOS, Windows, Linux, Solaris), IBM iSoft Commerce Suite Server Enterprise v3.2.01, IBM Tivoli Storage Resource Manager Express Edition v1.3.2 Win, IBM Tivoli System Automation v1.2.0 Linux, IBM Tivoli Workload Scheduler Virtualized Data Centers v8.2 , other IBM Tivoli CDs, WEBSPHERE EVERYPLACE MOBILE PORTAL v5.0 - ALTIUM , other IBM WebSphere Business CDs...
5
5689
by: mayamorning123 | last post by:
A comparison among six VSS remote tools including SourceOffSite , SourceAnyWhere, VSS Connect, SourceXT, VSS Remoting, VSS.NET To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool This article makes a detailed comparison among SourceAnyWhere 4.0, SourceOffSite 4.1, VSS Connect 1.5, SourceXT 2.1, VSS Remoting 2.5,
14
25270
by: DaTurk | last post by:
I am makeing a Multicast server client setup and was wondering what the difference is between Socket.Connect, and Socket.Bind. It may be a stupid question, but I was just curious. Because I tried a test, and I can't bind two sockets to the same port/ip but I can connect with one socket, and bind with another to the same ip/port. Thanks
0
2767
by: Clodoaldo Pinto | last post by:
Can't connect to postgresql server php error log message: PHP Warning: pg_connect() : Unable to connect to PostgreSQL server: could not connect to server: Permission denied\n\tIs the server running on host "127.0.0.1" and accepting\n\tTCP/IP connections on port 5432? php connection string:
14
7042
by: Marcus | last post by:
I have a function that simply returns TRUE if it can connect to a particular Sql Server 2005 express, or FALSE if it cannot. I am getting some strange error codes returned when the computer that sql server resides on is not reachable. The error is different depending on the connection string that I use. If I use the following connection string: "server=192.1.1.1; Initial Catalog=master; uid=The_User; password=The_Password; Connect...
0
4885
by: tamayi | last post by:
I have a problem (like most others posting issues on this forum :) ) I have a remote server running Windows XP SP2, with both SQL Server 2005 Express with Advanced Features and SQL 2000 installed. I can remotely connect to the SQL Server 2000 instance using Query Analyser or Enterprise Manager. I can also remotely connect to the SQLEXPRESS using Management Studio Express Edition. I am writing an ASP.NET 2.0 with VS2005 application...
7
5943
by: RN1 | last post by:
Is it possible to connect to my local SQL Server 2005 database from a remote web server? If yes, what ConnectionString do I use? Thanks, Ron
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9915
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8939
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5358
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.