Click here to Skip to main content
15,891,621 members
Articles / Programming Languages / SQL

Getting started with SQL Server Everywhere

Rate me:
Please Sign up or sign in to vote.
4.05/5 (13 votes)
7 Jul 20064 min read 99.1K   1.4K   66  
How to start using SQL Server Everywhere in C# programs.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.IO;

namespace GetStartedWithSQLServerEverywhere
{
	public partial class Form1 : Form
	{
		SqlServerEverywhere sqlEverywhere = new SqlServerEverywhere();

		public Form1()
		{
			InitializeComponent();
		}

		private void toolStripButton1_Click(object sender, EventArgs e)
		{
			dataGridView1.ReadOnly = false;
			dataGridView1.Rows.Clear();
			dataGridView1.Columns.Clear();
			SqlServerEverywhereResultSet resultSet = sqlEverywhere.execute(textBox1.Text);
			if (resultSet != null)
			{
				foreach (string column in resultSet.columns)
				{
					dataGridView1.Columns.Add(column, column);
				}
				foreach (List<string> row in resultSet.rows)
				{
					dataGridView1.Rows.Add(row.ToArray());
				}
			}
			dataGridView1.ReadOnly = true;
		}
	}

	class SqlServerEverywhereResultSet
	{
		public List<string> columns = new List<string>();
		public List<List<string>> rows = new List<List<string>>();
	}

	class SqlServerEverywhere
	{
		const string ConnectionString = "Data Source='Test.sdf';";
		public SqlServerEverywhere()
		{
			// First check if the database exist
			if (!(File.Exists("Test.sdf")))
			{
				// The database does not exist so create it
				SqlCeEngine engine = new SqlCeEngine(ConnectionString);
				engine.CreateDatabase();
				// Setup a connection to the database
				SqlCeConnection connection = new SqlCeConnection(engine.LocalConnectionString);
				connection.Open();
				SqlCeCommand command = connection.CreateCommand();
				command.CommandText = "SELECT count(*) FROM INFORMATION_SCHEMA.TABLES " +
					"where TABLE_NAME = 'customer'";
				int result = (System.Int32)(command.ExecuteScalar());
				if (result == 0)
				{
					command.CommandText = 
						"create table customer (name nvarchar(100), id int, phone nvarchar(20))";
					command.ExecuteNonQuery();
					command.CommandText = 
						"insert into customer (name, id, phone) values ('Bill', 2, '555-1234')";
					command.ExecuteNonQuery();
					command.CommandText =
						"insert into customer (name, id, phone) values ('Ted', 3, '555-4234')";
					command.ExecuteNonQuery();
					command.CommandText =
						"insert into customer (name, id, phone) values ('Tom', 1, '555-3234')";
					command.ExecuteNonQuery();
				}
				connection.Close();
			}
		}

		public SqlServerEverywhereResultSet execute(string commandString)
		{
			try
			{
				SqlCeConnection connection = new SqlCeConnection(ConnectionString);
				connection.Open();
				SqlCeCommand command = connection.CreateCommand();
				command.CommandText = commandString;
				SqlCeDataReader dataReader = command.ExecuteReader();
				SqlServerEverywhereResultSet resultSet = new SqlServerEverywhereResultSet();
				if (dataReader != null)
				{
					for (int i = 0; i < dataReader.FieldCount; i++)
					{
						resultSet.columns.Add(dataReader.GetName(i));
					}
					while (dataReader.Read())
					{
						List<string> row = new List<string>();
						for (int i = 0; i < dataReader.FieldCount; i++)
						{
							row.Add(dataReader.GetValue(i).ToString());
						}
						resultSet.rows.Add(row);
					}
				}
				connection.Close();
				return resultSet;
			}
			catch (SqlCeException ex)
			{
				MessageBox.Show(ex.Message, "Error executing SQL Statement");
				return null;
			}
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Founder
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions