Реализовать клиент серверное приложение - C#
Формулировка задачи:
Здраствуйте,дали задание реализовать клиент серверное приложение:
Реализовать с клиента: просмотр словаря по алфавиту, поиск введенного термина по слову и по букве, добавление термина, удаление термина, редактирование.
Вся информация хранится на сервере.
Может у кого нибудь есть что нибудь похожее,или кто нибудь подскажет как сделать?
Решение задачи: «Реализовать клиент серверное приложение»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SQLite;
using System.ServiceModel;
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
class MyService : IContract {
private readonly string CONNECT_STR = string.Empty;
public MyService() {
CONNECT_STR = string.Format("Data Source={0}", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database.db"));
if (!File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database.db"))) {
throw new FaultException("Database file error!");
}
}
public string[] GetAllWords(bool ordered) {
try {
List<string> temp = new List<string>();
using (SQLiteConnection cnn = new SQLiteConnection(CONNECT_STR)) {
cnn.Open();
string selectStr = "SELECT * FROM words";
using (SQLiteCommand cmd = new SQLiteCommand(selectStr, cnn)) {
using (SQLiteDataReader dr = cmd.ExecuteReader()) {
while (dr.Read()) {
temp.Add((string)dr["word"]);
}
}
}
}
return ordered ? temp.ToArray() : temp.OrderBy(s => s).ToArray();
}
catch (Exception ex) {
ExceptionDetail detail = new ExceptionDetail(ex);
FaultException<ExceptionDetail> error = new FaultException<ExceptionDetail>(detail, ex.Message);
throw error;
}
}
public string[] GetWordsByWord(string word) {
return this.GetAllWords(false).Where(s => s == word).ToArray();
}
public string[] GetWordsByChar(char ch) {
return this.GetAllWords(false).
Where(s => s.StartsWith(ch.ToString(), StringComparison.InvariantCultureIgnoreCase)).
ToArray();
}
public void AddWord(string word) {
try {
string insertStr = string.Format("INSERT INTO words VALUES('{0}')", word);
using (SQLiteConnection cnn = new SQLiteConnection(CONNECT_STR)) {
cnn.Open();
using (SQLiteCommand cmd = new SQLiteCommand(insertStr, cnn)) {
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex) {
ExceptionDetail detail = new ExceptionDetail(ex);
FaultException<ExceptionDetail> error = new FaultException<ExceptionDetail>(detail, ex.Message);
throw error;
}
}
public void DeleteWord(string word) {
throw new NotImplementedException();
}
}
[ServiceContract]
interface IContract {
[OperationContract]
string[] GetAllWords(bool ordered);
[OperationContract]
string[] GetWordsByWord(string word);
[OperationContract]
string[] GetWordsByChar(char ch);
[OperationContract]
void AddWord(string word);
[OperationContract]
void DeleteWord(string word);
}