SQL Server Compact Edition program build steps

Source: Internet
Author: User
Tags anonymous definition sql string variable management studio access sql server management

Before you create an application, you must configure the publication in SQL Server 2005. In the following steps, you will create a sample database and then publish the data from that database. You will use the pre-built script file to create the database, and then use the New Publication Wizard to create the publication.

Create a database and populate it with data

Prepare the server to publish data

Before you create a publication, you must create a Snapshot Agent user account and create a shared folder in which the snapshot files will be stored, preparing the server for publication. After the snapshot folder is created, the folder is used for all publications on the server. If you have previously created a publication on this server, you can skip these steps.

Create a Snapshot user account

Create snapshot Folder

Create a publication

Set permissions

You must grant permissions to the Snapshot Agent account and the IIS anonymous user account. You must also add the IIS anonymous user account to the publication access list (PAL)

Set database Permissions

Grant Publishing permissions

Create a Publish Snapshot

Subscriptions to SQLMobile publications can be initialized only after the publication snapshot has been created.

Create a Publish Snapshot
_______________________________________________________________________________________

Configure IIS and SQL Server 2005 to implement WEB synchronization
Now that you have configured a publication for SQL Server, you must make the publication available for use by the SQL Server Compact Edition client over the network. The SQL Server Compact Edition can be connected to SQL Server through IIS. That is, the virtual directory is created and configured to allow clients access to the SQL Server Compact Edition Server Agent.
Install the SQL Server Compact Edition server component
To configure a publication for WEB synchronization
_______________________________________________________________________________________

SQL Server Compact Edition task
If you create a SQL Server Compact Edition database and subscriptions before you develop an application that uses SQL Server Compact Edition, you typically save a lot of time. SQL Server Management Studio allows you to create and use the SQL Server Compact Edition database on your local computer. You can then use this database when you are developing your application.
Create a new SQL Server Compact Edition database
Create a new subscription

Three steps to configure: Create a publication-->web sync--> Create a subscription
_______________________________________________________________________________________

Build the Application

Create a new Smart Device project

Adding references

Add Data connection

Select the data to display

Add node

In the code page of your application, you can add a string variable that contains the path and name of the database file, code to delete the database file if it already exists, to create a connection to the SQL Server publication, to synchronize the data, and to create a new local database that contains the published data.

Add code

In Solution Explorer, right-click Form1, and then select View Code.

In the code page, look for the Form1 class definition. Add a string variable and assign the path and file name of the. sdf file. The data source that Visual Studio created in the previous step requires that the database file reside in the \program Files\applicationname folder, where ApplicationName is the name of the application. For example, if you name a new project SQLMobile, you should set the string variable to \program Files\sqlmobile\sqlmobile.sdf.

The first few lines of a class definition should resemble the following code:

[C #]
Copy Code
public partial class Form1:System.Windows.Forms.Form
{
Private System.Windows.Forms.MainMenu mainMenu1;
string filename = @ "\program Files\sqlmobile\sqlmobile.sdf";

Public Form1 ()
{
InitializeComponent ();
}

If the database file is currently present, a new method is created to delete the database file. This will ensure that the application loads the most current data each time it is run. This method should be named Deletedb. This code should resemble the following code:

[C #]
Copy Code
private void Deletedb ()
{
if (System.IO.File.Exists (filename))
{
System.IO.File.Delete (filename);
}
}

Create a new method that performs synchronization named "Sync." To do this, you will use the code copied from the New Publication Wizard in the previous step. After you paste the code, you must perform two changes to the code:

Change the subscriberconnectionstring value so that the value points to the correct path and file name specified in the file name variable.

Change the AddOption value from ExistingDatabase to CreateDatabase.

After the changes are complete, the Sync method should look like this:

[C #]
Copy Code
private void Sync ()
{
SqlCeReplication repl = new SqlCeReplication ();

   Repl. InternetURL = @ "Http://<computername>/sqlmobile/sqlcesa30.dll";
   Repl. Publisher = @ "<computername>";
   Repl. PublisherDatabase = @ "SQLMobile";
   Repl. PublisherSecurityMode = securitytype.ntauthentication;
   Repl. Publication = @ "SQLMobile";
   Repl. Subscriber = @ "SQLMobile";
   Repl. SubscriberConnectionString = @ "Data source= '" + filename +
     "; Password= '; Max Database size= ' 128 ';D efault Lock escalation
     = ' 100 ';
   try
   {
      repl. AddSubscription (addoption. CreateDatabase);
      Repl. Synchronize ();
  }
   catch (sqlceexception e)
   {
      MessageBox.Show (E. ToString ());
  }
}

Finally, add code at the beginning of the Form1_Load event handler that invokes the newly created two methods. The Form1_Load event handler should look like the following:

[C #]
Copy Code
private void Form1_Load (object sender, EventArgs e)
{
Deletedb ();
Sync ();

Todo:delete this line of code.
This.flightDataTableAdapter.Fill (This.sqlmobileDataSet.FlightData);
Todo:delete this line of code.
This.membershipDataTableAdapter.Fill (This.sqlmobileDataSet.MembershipData);
}
_______________________________________________________________________________________

Deploy and test your application

deploying applications

please contact the site, timely note your name. Contact Email: edu#chinaz.com (change # to @).



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.