Re: SQL Server CE and PDA and C# by Ginny
Ginny
Mon Oct 04 12:03:42 CDT 2004
Hey, Darren, no problem! It's always better to have too many answers rather
than too few, and yours goes right to the heart of the question.
--
Ginny Caughey
.Net Compact Framework MVP
"Darren Shaffer" <darrens@connectedinnovation.com> wrote in message
news:uX7TPJjqEHA.3712@TK2MSFTNGP15.phx.gbl...
>I apologize Ginny - we must have been typing in our sample code at the same
> time and yours got posted first.
>
> I didn't mean to imply that there was anything lacking in your example
> (which is certainly more comprehensive).
>
> -Darren
>
>
> "Ginny Caughey [MVP]" <ginny.caughey.online@wasteworks.com> wrote in
> message news:eEULo7hqEHA.3464@tk2msftngp13.phx.gbl...
>> Oliver,
>>
>> Here's some simple code that illustrates the concepts:
>>
>> using System;
>> using System.IO;
>> using System.Text;
>> using System.Data;
>> using System.Data.SqlTypes;
>> using System.Data.SqlServerCe;
>> using System.Collections;
>> using System.Windows.Forms;
>> namespace SQLSeverDemo
>> {
>> /// <summary>
>> /// Summary description for WalkThrough.
>> /// </summary>
>> public class WalkThrough
>> {
>> public WalkThrough()
>> {
>> SqlCeConnection conn = null;
>> try
>> {
>> if (File.Exists ("Test.sdf") )
>> File.Delete ("Test.sdf");
>> MessageBox.Show("Creating database.");
>> SqlCeEngine engine = new SqlCeEngine ("Data Source = Test.sdf");
>> engine.CreateDatabase ();
>> MessageBox.Show("Opening connection.");
>> conn = new SqlCeConnection ("Data Source = Test.sdf");
>> conn.Open();
>> SqlCeCommand cmd = conn.CreateCommand ();
>> MessageBox.Show("Creating table");
>> cmd.CommandText =
>> "CREATE TABLE TestTbl (col1 INT PRIMARY KEY, col2 NTEXT, col3 MONEY)";
>> cmd.ExecuteNonQuery();
>> MessageBox.Show("Inserting initial values into table using SQL");
>> cmd.CommandText =
>> "INSERT INTO TestTbl (col1, col2, col3) VALUES (0, 'abc', 15.66)";
>> cmd.ExecuteNonQuery();
>> MessageBox.Show("inserting values into table using parameters");
>> cmd.CommandText = "INSERT INTO TestTbl (col1, col2, col3) VALUES (?, ?,
>> ?)";
>> cmd.Parameters.Add(new SqlCeParameter("p1", SqlDbType.Int));
>> cmd.Parameters.Add(new SqlCeParameter("p2", SqlDbType.NText));
>> cmd.Parameters.Add(new SqlCeParameter("p3", SqlDbType.Money));
>> cmd.Parameters["p2"].Size = 50;
>> cmd.Prepare();
>> cmd.Parameters["p1"].Value = 1;
>> cmd.Parameters["p2"].Value = "abc";
>> cmd.Parameters["p3"].Value = 15.66;
>> cmd.ExecuteNonQuery();
>> cmd.Parameters.Clear();
>> MessageBox.Show("Reading values from table");
>> cmd.CommandText = "SELECT * FROM TestTbl";
>> SqlCeDataReader rdr = cmd.ExecuteReader();
>> string s = "Data returned";
>> while (rdr.Read())
>> {
>> s += "\nRow: col1 = " + rdr.GetInt32(0) +
>> " col2 = " + rdr.GetString(1) +
>> " col3 = " + rdr.GetSqlMoney(2);
>> }
>> MessageBox.Show(s);
>> MessageBox.Show("updating table with new values where col1 = 0");
>> cmd.CommandText = "UPDATE TestTbl set col2='some new value' WHERE
>> col1=0";
>> cmd.ExecuteNonQuery();
>> cmd.CommandText = "SELECT * FROM TestTbl";
>> rdr = cmd.ExecuteReader();
>> s = "Data returned now";
>> while (rdr.Read())
>> {
>> s += "\nRow: col1 = " + rdr.GetInt32(0) +
>> " col2 = " + rdr.GetString(1) +
>> " col3 = " + rdr.GetSqlMoney(2);
>> }
>> MessageBox.Show(s);
>> }
>> catch (SqlCeException e)
>> {
>> ShowErrors(e);
>> }
>> finally
>> {
>> MessageBox.Show("Closing database connection");
>> conn.Close();
>> }
>> }
>> public static void ShowErrors(SqlCeException e)
>> {
>> SqlCeErrorCollection errorCollection = e.Errors;
>> StringBuilder bld = new StringBuilder();
>> Exception inner = e.InnerException;
>> foreach (SqlCeError err in errorCollection)
>> {
>> bld.Append("\n Error Code: " + err.HResult.ToString("X"));
>> bld.Append("\n Message : " + err.Message);
>> bld.Append("\n Minor Err.: " + err.NativeError);
>> bld.Append("\n Source : " + err.Source);
>> foreach (int numPar in err.NumericErrorParameters)
>> {
>> if (0 != numPar) bld.Append("\n Num. Par. : " + numPar);
>> }
>> foreach (string errPar in err.ErrorParameters)
>> {
>> if (String.Empty != errPar) bld.Append("\n Err. Par. : " + errPar);
>> }
>> MessageBox.Show(bld.ToString());
>> bld.Remove(0, bld.Length);
>> }
>> }
>> }
>> }
>>
>>
>> --
>> Ginny Caughey
>> .Net Compact Framework MVP
>>
>>
>>
>> "Oliver Huppert" <olleatsaar@yahoo.de> wrote in message
>> news:cjrk7b$68f$02$1@news.t-online.com...
>>> Hi all,
>>>
>>> I wanna test some things with my Pocket PC 2003 based PDA and SQL Server
>>> CE.
>>>
>>> My question is:
>>> Can I develop a "standalone application" for Pocket PC using the SQL
>>> Server
>>> CE without creating a remote access to a "real" server ?
>>>
>>> The example I found (this one with the golf results) on msdn creates a
>>> few
>>> tables using a remote machine with SQL Server 2000. That is not what I
>>> want
>>> to do.
>>> Also this example doesn't work because of some libs that are wrong. The
>>> example uses a Class named Engine under the SQLCeEngine Package.
>>> I have a class SQLCeEngine in the same package and use thisone. Then I
>>> developed a little application that creates a database using
>>> "CreateDatabase" method of this SQLCeEngine class. That works fine. But
>>> when
>>> I try to create a connection using the same connection string as for
>>> creating, I always get a exception with the message the property
>>> "provider"
>>> should be wrong.
>>>
>>> Can someone post me an example code for creating a database and
>>> establishing
>>> the connection including the connection string?
>>>
>>> Thanks in advance
>>> Olli
>>>
>>>
>>
>>
>
>