Using Remote Data Access with SQL Server CE 2.0
Pages: 1, 2, 3
Selecting, Updating, and Deleting Records
Now that the records are saved locally, the user can display the student information by selecting the class and lesson:
|
| Figure 11. Displaying the student records from the local SQL Server CE database |
So let's code the Display button now. I will have a function displayStudent() to do this. First, ensure that the class and lesson are selected:
Public Sub displayStudents()
' display the students according to Class and Lesson
If cmbClass.Text = "" Or cmbLesson.Text = "" Then
MsgBox("Please select Class and Lesson", _
MsgBoxStyle.Exclamation, "Select Class and Lesson")
Return
End If
Then, open the connection to the local SQL Server CE database and retrieve the appropriate records, and add three columns to the ListView control:
conn.Open()
Dim reader As SqlCeDataReader
Dim cmd As New SqlCeCommand( _
"SELECT StudentID, StudentName, " & _
cmbLesson.Text & _
" FROM Module WHERE ClassID='" & _
cmbClass.Text & "'", conn)
reader = cmd.ExecuteReader
ListView1.Clear()
ListView1.Columns.Add("StudentID", -1, _
HorizontalAlignment.Center)
ListView1.Columns.Add("Name", 150, _
HorizontalAlignment.Left)
ListView1.Columns.Add(cmbLesson.Text, -1, _
HorizontalAlignment.Center)
Finally, add the record to the ListView control:
While reader.Read
Dim ls As New ListViewItem(reader.Item("StudentID").ToString)
ls.SubItems.Add(reader.Item("StudentName").ToString)
ls.SubItems.Add(reader.Item(cmbLesson.Text).ToString)
ListView1.Items.Add(ls)
End While
conn.Close()
End Sub
To change the attendance information of a student, select the student and right-click on it (on the emulator) to invoke the context menu.
![]() |
| Figure 12. Invoking the context menu to change the attendance of a student |
This is done by servicing the Click event of each menu item:
Private Sub MenuItem1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MenuItem1.Click
In each menu item, we invoke the setStatus() method with the appropriate argument -- PR for present, AB for absent and MC for Medical Certificate:
Public Sub setStatus(ByVal status As String)
' set the status of the attendance
Dim i As Integer
Dim studentID As String
Try
i = ListView1.SelectedIndices.Item(0)
Catch ex As Exception
MsgBox("Please select item", MsgBoxStyle.Exclamation, _
"Student not selected")
Return
End Try
studentID = ListView1.Items.Item(i).Text
Dim sql As String = "UPDATE Module Set " & cmbLesson.Text & _
"='" & status & "' WHERE StudentID='" & studentID & "'"
conn.Open()
Dim cmd As New SqlCeCommand(sql, conn)
cmd.ExecuteNonQuery()
conn.Close()
displayStudents()
End Sub
Essentially, the local SQL Server CE database is updated.
Pushing Data Back to SQL Server 2000
When the user gets back to the office, he can then push the updated data back into the SQL Server 2000. Here, we code the Push button.
As usual, set the connection string to connect to SQL Server 2000:
Dim rdaOleDbConnectString As String = _
"Provider=sqloledb; Data Source=mercury2;" & _
"Initial Catalog=Attendance; User Id=sa;Password="
And finally, create the RDA object and push the local SQL Server CE table back to the SQL Server 2000 database:
' Initialize the RDA object.
Dim rda As SqlCeRemoteDataAccess = Nothing
Try
rda = New SqlCeRemoteDataAccess
rda.InternetUrl = "http://mercury2/SQLCE/sscesa20.dll"
rda.LocalConnectionString = _
"Provider=Microsoft.SQLSERVER." & _
"OLEDB.CE.2.0;Data Source=\My Documents\ssce.sdf"
rda.Push("Module", rdaOleDbConnectString, _
RdaBatchOption.BatchingOn)
MsgBox("Push operation completed", _
MsgBoxStyle.Information, "Push")
Catch err As SqlCeException
ShowErrors(err)
Finally
rda.Dispose()
End Try
That's it! You can now make changes to the SQL Server CE database and update the SQL Server 2000 database.
SQL Server CE Analyzer
Before I end this article, I want to quickly show you the included SQL Server CE Query Analyzer. When you first run your mobile application with the SQL Server CE assembly added, the SQL Server CE Query Analyzer will automatically be installed. You can find it at Programs->SQLCE Query:
|
| Figure 13. Running SQL Server CE Query Analyzer |
Using the SQL Server CE Query analyzer, you can connect to the local database and examine the database structure, as well as execute SQL queries.
![]() |
| Figure 14. Examining the local SQL Server CE database |
Debugging Tips
As I worked through the sample codes in this article, there are various sources of errors that I encountered. Some are silly mistakes that I made, while a significant portion were caused by the quirks of Visual Studio .NET and the emulators. I will share with you some of the pitfalls and hopefully, you can save yourself some frustrating hours chasing after an error that wasn't caused by you.
- Always trap your errors by using the
Try-Catch-Finallyblock and send the error object to theShowErrors()method. TheShowErrors()method will reveal a lot more information and save you a lot of time wondering about the source of the error. - Always stop your debugging process by closing the application within the emulator. Using the Stop button is Visual Studio .NET often causes errors the next time you run the application again. You will see an error message like the following:
The file 'SmartDeviceApplication3.pdb' cannot be copied to the run directory. The process cannot access the file because it is being used by another process.
Could not copy temporary files to the output directory.
In such cases, simply restarting Visual Studio .NET should solve the problem.
- I constantly encounter the following errors when I try the pull operation on my application for the first time:
|
| Figure 15. A common error connecting to the IIS |
After some trial and error, I realized that the problem could be solved most of the time by invoking Internet Explorer on the emulator and connecting to the URL http://mercury2/SQLCE/sscesa20.dll. This URL points to the SQL Server CE Server Agent, and you should see the following message in return: "SQL Server CE Server Agent."
Try this a few times and your application should work fine. If not, restart
your emulator and Visual Studio .NET.
Wei-Meng Lee (Microsoft MVP) http://weimenglee.blogspot.com is a technologist and founder of Developer Learning Solutions http://www.developerlearningsolutions.com, a technology company specializing in hands-on training on the latest Microsoft technologies.
Return to ONDotnet.com
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 40 of 40.
-
SQL Server CE Community Portal
2007-03-10 14:13:23 Nick_Randolph [Reply | View]
With the recent release of SQL Server 2005 Compact Edition (v3.1) a new portal has been set up that is dedicated to building a community of interest around this product. If you wish to read more and/or contribute based on your experience with this product then head over to http://www.sqlserverce.org
-
Need some advice
2007-01-08 19:21:01 joeyOng [Reply | View]
The article is such a great kick start to develop my first pocket pc application and the best of all, it is something similar to the current project that im working with.
I am a beginner to .net cf and sql server ce, currently trying hard to complete my project which is a student attendance taking app using pda. And while searching for useful and understandable information, i am confused with the wide variety of choices that we can opt in developing the app.
i need some advice on the following subjects :
1) I've proposed a disconnected environment for my project where i'll get the data from central storage (SQL Server 2000) and sync it to PDA (in XML file) to be used as local data storage. When the information is updated, it will be pushed back to SQL Server. I think that this works OK in a limited connection environment, but it will violate the real time data access criteria of mobile computing. Can anybody tell me whether this decision is appropriate or not?
2) type of database that i should use when deal with pocket pc application. As shown in this article, SQL Server CE 2.0 was used and i really think it is cool. But i am weighing the benefits of using xml textfiles as my local data storage at the same time. Could anybody help me to justify, which one is better? in terms of ease of development, resource consumption - which would be more lightweight (on the pda), which is more preferable. I knew one of the consideration point would be the size of data that we want to parse, and it is not advisable to use xml if the qty of data is too big. When we say qty data is not too big, how do we actually measure whether it is big or small? what is the size that is considered as big and vice versa?
2) How to pull data from SQL Server 2000 to xml file to be used as local storage( in a disconnected environment) and update the data before pushing it back to SQL Server 2000?
Need feedback urgently.. Any helps and advises given will be greatly appreciated. Thank you
-
thnks a lot
2006-10-13 03:51:13 beolily [Reply | View]
Very nice written... using your primer i make my RDA push and pull code to work fine for about 2 hours...
-
useful info and source code
2006-09-01 15:59:28 raskoni [Reply | View]
it works well.I have tested it almost 600 record in 1 mb table.
thanks
-
Excellent Notes
2006-03-09 01:08:42 vsaravanan_mca [Reply | View]
i need Source code for that . it is very useful for my project .
thanking you
v.saravanan
-
Can i use embedded visual C++ 4.0 for SQL Server CE ?
2005-07-28 20:04:47 trible777 [Reply | View]
I wanna check whether can i use SQL Server CE
for embedded visual c++ 4.0?
To retrieve data from SQL Server CE.
Your tutorial is good.But is there any tutorial on SQL SERVER CE ON C++ LANG.
-
How to set SECURE connection to sql ce database
2005-06-08 00:58:26 Jambotkar [Reply | View]
inorder to protect sql ce database from Internet Users AND also by NOT programing the embeeded user id & passwd....how can we SET a secure connection by which we can protect the sql ce database.
-
Re: PDA end Pull
2005-04-11 19:22:15 Nupur [Reply | View]
It was indeed a very quick guide to get a newbie to get started with code developing a pDA.
However, i want to be able to automatically trigger the Pull function from a win application.
Any sugestions??
Thanks
-
source code
2005-03-14 19:32:43 Thorell [Reply | View]
An excellent article! But do I really need to type in the source code manually?
-
total insertion time of records grows exponentionally with number of records
2005-02-16 08:15:26 Arif. [Reply | View]
Hi,
I want to access SqlServerCe database through my desktop C#.Net application, so that I can insert huge records very fast and then copy this database file to Pocket PC device.
For inventory purpose, I have to insert 14,000 records in SqlServerCe database. It is taking much time. I am building a desktop application/smart device application and giving the path for SqlCeConnection("Data Source=C:\\mydatabase; Password=p;");
I added the reference to System.Data.SqlServerCe.dll by browing to "D:\Program Files\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE".
But doing this gives me a waning message this will alter your existing references. and then my form designer corrupts, however code exists.
Alternatively I build Smart Application and give the same path SqlServerCe database. Compilation OK, and even run if i don't use SqlServerCe database statements. But when write SqlServerCe statements , at runtime this gives me message "The assembly System.Data.SqlServerCe.dll was invalid".
I have another idea to use Emulator to get my fast database insertion, but don't know how to invoke Emulator through my application.
Please help me in achieving my fast SqlServerCe database insertions. Because 14,000 records is taking much time.
I will be very thankful.
Arif
-
Why INSERTing records in SqlServerCe database taking much time?
2005-02-16 00:37:22 Arif. [Reply | View]
Hi,
If I insert about 150 records (insides a loop) into SqlServerCe database, it takes approximately 57 seconds. But when I insert about 2000 records(insides a loop) it takes approximately 55 minutes to more than an hour. The code snippet is as follows:
System.IO.FileInfo info = new System.IO.FileInfo( appPath + "\\products.txt");
System.IO.StreamReader reader = info.OpenText();
while( (tokenstr = reader.ReadLine())!=null )
{
tokens = tokenstr.Split(',');
cmd.CommandText = "insert into products (bar_code,description,price) " +
"values ('"+tokens[0] + "','" + tokens[1] + "'," + tokens[2] +")";
try
{
cmd.ExecuteNonQuery();
}
catch(SqlCeException sqlceE)
{
MessageBox.Show("SqlCeException: " + sqlceE.Message);
}
}// end while loop
If 150 records takes 60 seconds then 2000 records must take:
150 / 2000 records = 60 / x seconds
=> x = (60 * 2000) / 150
=> x = 800 seconds = 800 / 60 minutes = 13.333 minutes
So 2000 records insertion must take 14 to 20 minutes, but it is taking 55 minutes to more than an hour. This is a huge time.
Please help me to reduce this insertion time. Also what may be the cause of this delay.
Arif.
-
Why INSERTing records in SqlServerCe database taking too much time
2005-02-16 00:06:26 Arif. [Reply | View]
Hi,
When I insert about 150 records (insides a loop) into SqlServerCe database, it takes approximately 57 seconds. But when I insert about 2000 records(insides a loop) it takes approximately 55 minutes to more than an hour. The code snippet is as follows:
System.IO.FileInfo info = new System.IO.FileInfo( appPath + "\\products.txt");
System.IO.StreamReader reader = info.OpenText();
while( (tokenstr = reader.ReadLine())!=null )
{
tokens = tokenstr.Split(',');
cmd.CommandText = "insert into products (bar_code,description,price) " +
"values ('"+tokens[0] + "','" + tokens[1] + "'," + tokens[2] +")";
try
{
cmd.ExecuteNonQuery();
}
catch(SqlCeException sqlceE)
{
MessageBox.Show("SqlCeException: " + sqlceE.Message);
}
}// end while loop
If 150 records takes 60 seconds then 2000 records must take:
150 / 2000 records = 60 / x seconds
=> x = (60 * 2000) / 150
=> x = 800 seconds = 800 / 60 minutes = 13.333 minutes
So 2000 records insertion must take 14 to 20 minutes, but it is taking 55 minutes to more than an hour. This is a huge time.
Please help me to reduce this insertion time. Also what may be the cause of this delay.
Arif.
-
Closing the login form in a PDA Application
2004-12-05 20:59:16 Murali_V [Reply | View]
In my PDA application (developed in Visual Studio .Net 2003) i have a login form. On click of Ok button on the login form, the user is validated and another form is displayed. this is working fine. I have written a code like, after the user is validated, close the login form and display the next form.
I have used frmLogin.Close() method. I am not getting any errors, but the form is not getting closed. Kindly let me know how to achieve this task....Thanks in advance
Murali
-
Using Remote Data Access with SQL Server CE 2.0
2004-12-05 05:31:51 Murali_V [Reply | View]
Excellent Article. I would like to really thank the author of this article which helped me building my first pocket pc application. For a long time I couldnt come out of the problem with the rda.push method that is discussed in this article. But after a series of experiments with the PDA IE, I was able to access the sseca20.dll thru PDA IE.
Every time I used to give http://localhost/SQLCE/sseca20.dll, it used to show me the error "The page you are trying to access could not be found".Instead of localhost i just tried
http://<ip address>/SQLCE/sseca20.dll and to my surprise i could see SQL Server CE Server Agent on my PDA IE. After this i was able to pull the data from SQL Server 2000 to SQL Server CE.
Thank you very much.
Murali
-
Missing Code
2004-06-18 08:38:52 EKontos [Reply | View]
It seems that there are quite a few pieces of code missing from your example, for instance when do the comboboxes for class and lesson get filled in? Or is this primarily for us to fill in the blanks?
-
Help on understanding the code
2004-01-08 00:08:12 anonymous2 [Reply | View]
This is a very interesting article but as a beginner in Visual Basic there was a lot of things in the article that I did not understand. Is it possible to get a sample code for this article?
-
Help Needed
2003-11-03 12:35:25 anonymous2 [Reply | View]
I'm a Pakistani student.. I have to submit my finalproject i need some help by u.. i want to do MOBILE connectivity to the any device like that i wanna connect my TV with mobile that i send the msg through mobile & Tv get on.. I need the code & ur help if possible 4 u.. thanx
My E-mail is karachiette@yahoo.com
-
I need c# example code....please~~~
2003-10-07 10:18:57 anonymous2 [Reply | View]
i'm korean student...
I learned c#. so I can't understand vb code..
I hope you give me c# code~~
please~~ my e-mail ==> hokill6511@hanmail.net
-
I need c# example code....please~~~
2003-10-07 10:17:14 anonymous2 [Reply | View]
i'm korean student...
I learned c#. so I can't understand vb code..
I hope you give me c# code~~
(¤Ñ¤Ì) please~~
-
Need Help Urgently!!
2003-09-21 05:09:00 anonymous2 [Reply | View]
Im sorry, but i need some big help... First thing is... does the SQL Server 2000 need to set up anything?? like server replication or anything... or is everything goin to be fine by just using codes??? plz reply asap.... wong_yew_teck@hotmail.com THX!!
-
Fed Up of Error 80072EFD
2003-07-08 13:59:56 anonymous2 [Reply | View]
Please how can you explain the error of "A Request to send data to computer running IIS failed"???? I'm following all of your steps exactly and I've configured the complete environment perfectly and I still get this error. The browser is OK now after I by chance configured this dump emulator ntwork settings and proxy settings it is able now to connect to IIS via IE but not my application PLEASE HELP ME!!!
-
thanks you very much
2003-06-24 02:28:03 anonymous2 [Reply | View]
well i was getting error b4 i come to this page and after few modification by refering to the codes my project runs! yahoo... thanks again..
can't really tells how exciting i am now.. haha..
:)
best regards,
boonlee oon, penang, malaysia -
Problem with the RDA
2005-12-10 20:15:53 ArunReginald [Reply | View]
I cannot seem to figure out the problem with the RDA. Whenever I try to pull data with thePull(...)method, I get the error someone's been talking about in this comment forum. The same old 'A request to send data to the computer running IIS has failed. For more, see HRESULT' Heck, I remember the whole sentence by heart now.
My computer has:
Windows XP Service Pack 2 (XPSP2)
Microsoft Visual Studio.Net 2003
SQL Server CE 2.0
eMbedded Visual Tools 3.0
I know this is a known bug with the Windows Firewall not configured properly. I have configured the settings as this document had suggested - http://support.microsoft.com/?kbid=842907 - in vain.
I even increased timeout intervals for the RDA script and the IIS itself. Oh the agony, I even tried 'Merge Replication'.
-
context menu does not fire
2003-06-19 10:05:09 hse [Reply | View]
Excellent article.
I am developing on the pocket pc and cannot get the contextmenu to fire.
-
context menu does not fire
2003-06-19 10:05:05 anonymous2 [Reply | View]
Excellent article.
I am developing on the pocket pc and cannot get the contextmenu to fire.
-
Error when pulling data
2003-06-13 08:31:15 anonymous2 [Reply | View]
hi
I am using MSDE integrated with VS.NET and SQLServerce 1.0 . I get this error -
Error code: 80004005
Message: SQL ServerCE Encountered Problems in opening the
SQL server CE database [,,,Database name,,]
Minor Err : 28559
what could be the Reason??.
i have created New database 'Module' and Table 'Module' with the Same Definition..
but i am able to invoke Internet Explorer on the emulator and able to connect to the URL http://Kamal/SQLCE/sscesa10.dll"
i couldn't find sscesa20.dll file that's why i used sscesa10.dll..(may be the change in version)...
when i change the localconnectstring as below ( OLEDB1.0..is it correct??)
rda.LocalConnectionString = "Provider=Microsoft.SQLSERVER.OLEDB.CE.1.0;Data Source=\ssce.sdf"
i am getting error code : 800040E21 but with the same Error Message...
please help me..
please reply to kamal_neel@yahoo.com
thanks in advance.
thanks,
kamal
-
Working with MSDE
2003-03-08 21:03:12 vineetb [Reply | View]
Refer to
INFO: MSDE Security and Authentication
http://support.microsoft.com/default.aspx?scid=kb;EN-US;325022
-
Working with MSDE
2003-03-08 19:39:18 anonymous2 [Reply | View]
I am using MSDE integrated with VS.NET. I get this error - Error code: 80004005 Message: Login failed for user 'sa'. Reason: Not trusted SQL Server connection. What do I use for user id and password.
Thanks,
vineet
-
Error when pulling data
2003-03-01 01:20:56 anonymous2 [Reply | View]
I'm not sure whether u guys face tis problem but unluckily, i met up with it.
When i play the application using the emulator the 1st time; pulling of the data is unsuccessful. However, if i keep the emulator on but stop the program using the .NET, and run it the second time, then the data can be pulled.
Can anione tell me what happen?
-
Using Remote Data Access with SQL Server CE 2.0
2003-02-16 10:57:37 anonymous2 [Reply | View]
Excellent article
Helped me to learn quickly.
Thanks,
-
Excelent
2003-02-11 15:56:17 anonymous2 [Reply | View]
This article is excelent. Thanks for sharing your know-how with us.
João Ferreira (joao99@netc.pt)
-
thanks!
2003-01-27 09:54:33 anonymous2 [Reply | View]
my name is ziad itani i work on a toshiba e730 pocket pc i found your article very helpful and clear thanks !!! so much
But i would like to say that in the code for creating the database to do the pull part the
"sqlceEngine" object gave me error instead for my application to work i used "engine" altough i have added the required reference
anyways i appreciate this article because it is the first worldwide july(20002) to talk about this kind of application using the new .net CF
please keep on! we want more







