我们在前一篇的基础上添加功能,允许添加项目。
从Web服务DataService.asmx开始:添加Web方法InsertProject()
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
/// <summary>
/// DataService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class DataService : System.Web.Services.WebService
{
private SqlConnection _conn;
private SqlDataAdapter _daProjects;
private SqlCommand _selectProjectsCommand;
public DataService()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
_conn = new SqlConnection("Data Source=(local);Initial Catalog=TaskVision;User ID=sa;Password=password;");
_daProjects = new SqlDataAdapter();
_selectProjectsCommand = new SqlCommand();
_selectProjectsCommand.Connection = _conn;
_selectProjectsCommand.CommandText = "SELECT ProjectID, ProjectName, ProjectDescription, DateCreated FROM Projects WHERE (IsDeleted = 0)";
_daProjects.SelectCommand = _selectProjectsCommand;
}
[WebMethod]
public DataSetProjects GetProjects()
{
DataSetProjects ds = new DataSetProjects();
_daProjects.Fill(ds.Projects);
return ds;
}
[WebMethod]
public int InsertProject(string projectName, string projectDescription)
{
SqlCommand insertProjectCommand = new SqlCommand();
insertProjectCommand.Connection = _conn;
insertProjectCommand.CommandText = "INSERT INTO Projects(ProjectName,ProjectDescription) VALUES(@ProjectName,@ProjectDescription);SELECT SCOPE_IDENTITY()";
SqlParameter parmProjectName = new SqlParameter("@ProjectName", SqlDbType.VarChar, 20);
parmProjectName.Value = projectName;
SqlParameter parmProjectDescription = new SqlParameter("@ProjectDescription", SqlDbType.VarChar, 100);
parmProjectDescription.Value = projectDescription;
insertProjectCommand.Parameters.Add(parmProjectName);
insertProjectCommand.Parameters.Add(parmProjectDescription);
object projectID;
try
{
_conn.Open();
projectID = insertProjectCommand.ExecuteScalar();
}
finally
{
_conn.Close();
}
if (projectID == null)
{
return -1;
}
else
{
return Convert.ToInt32(projectID);
}
}
}
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
/// <summary>
/// DataService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class DataService : System.Web.Services.WebService
{
private SqlConnection _conn;
private SqlDataAdapter _daProjects;
private SqlCommand _selectProjectsCommand;
public DataService()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
_conn = new SqlConnection("Data Source=(local);Initial Catalog=TaskVision;User ID=sa;Password=password;");
_daProjects = new SqlDataAdapter();
_selectProjectsCommand = new SqlCommand();
_selectProjectsCommand.Connection = _conn;
_selectProjectsCommand.CommandText = "SELECT ProjectID, ProjectName, ProjectDescription, DateCreated FROM Projects WHERE (IsDeleted = 0)";
_daProjects.SelectCommand = _selectProjectsCommand;
}
[WebMethod]
public DataSetProjects GetProjects()
{
DataSetProjects ds = new DataSetProjects();
_daProjects.Fill(ds.Projects);
return ds;
}
[WebMethod]
public int InsertProject(string projectName, string projectDescription)
{
SqlCommand insertProjectCommand = new SqlCommand();
insertProjectCommand.Connection = _conn;
insertProjectCommand.CommandText = "INSERT INTO Projects(ProjectName,ProjectDescription) VALUES(@ProjectName,@ProjectDescription);SELECT SCOPE_IDENTITY()";
SqlParameter parmProjectName = new SqlParameter("@ProjectName", SqlDbType.VarChar, 20);
parmProjectName.Value = projectName;
SqlParameter parmProjectDescription = new SqlParameter("@ProjectDescription", SqlDbType.VarChar, 100);
parmProjectDescription.Value = projectDescription;
insertProjectCommand.Parameters.Add(parmProjectName);
insertProjectCommand.Parameters.Add(parmProjectDescription);
object projectID;
try
{
_conn.Open();
projectID = insertProjectCommand.ExecuteScalar();
}
finally
{
_conn.Close();
}
if (projectID == null)
{
return -1;
}
else
{
return Convert.ToInt32(projectID);
}
}
}
相应的修改本地应用程序的DataLayer:
从上面的代码大家可以很直观的看到重复的部份代码:catch(WebException){}catch(SoapException){}catch(Exception){}using System;
using TaskVision.DataWS;
namespace TaskVision
{
public enum WebServicesExceptionType
{
WebException = 0,
SoapException = 1,
Exception = 2,
None = 3
}
public class DataLayer
{
private DataService _dataService;
private DataSetProjects _dsProjects = new DataSetProjects();
public DataSetProjects.ProjectsDataTable Projects
{
get
{
return _dsProjects.Projects;
}
}
public DataLayer()
{
_dataService = GetWebServiceReference();
}
public WebServicesExceptionType GetProjects()
{
DataSetProjects dsProjects = null;
WebServicesExceptionType exceptionType = WebServicesExceptionType.None;
try
{
dsProjects = _dataService.GetProjects();
}
catch (System.Net.WebException)
{
exceptionType = WebServicesExceptionType.WebException;
}
catch (System.Web.Services.Protocols.SoapException)
{
exceptionType = WebServicesExceptionType.SoapException;
}
catch (System.Exception)
{
exceptionType = WebServicesExceptionType.Exception;
}
if (dsProjects != null)
{
_dsProjects.Clear();
_dsProjects.Merge(dsProjects);
}
return exceptionType;
}
public WebServicesExceptionType InsertProject(ref int projectID, string projectName, string projectDescription)
{
WebServicesExceptionType exceptionType = WebServicesExceptionType.None;
try
{
projectID = _dataService.InsertProject(projectName, projectDescription);
}
catch (System.Net.WebException)
{
exceptionType = WebServicesExceptionType.WebException;
}
catch (System.Web.Services.Protocols.SoapException)
{
exceptionType = WebServicesExceptionType.SoapException;
}
catch (System.Exception)
{
exceptionType = WebServicesExceptionType.Exception;
}
return exceptionType;
}
private DataService GetWebServiceReference()
{
DataService dataService = new DataService();
string urlSetting = System.Configuration.ConfigurationManager.AppSettings["DataServiceUrl"];
if (urlSetting != null)
{
dataService.Url = urlSetting;
}
else
{
dataService.Url = "http://localhost/TaskVisionWS/DataService.asmx";
}
return dataService;
}
}
}
using TaskVision.DataWS;
namespace TaskVision
{
public enum WebServicesExceptionType
{
WebException = 0,
SoapException = 1,
Exception = 2,
None = 3
}
public class DataLayer
{
private DataService _dataService;
private DataSetProjects _dsProjects = new DataSetProjects();
public DataSetProjects.ProjectsDataTable Projects
{
get
{
return _dsProjects.Projects;
}
}
public DataLayer()
{
_dataService = GetWebServiceReference();
}
public WebServicesExceptionType GetProjects()
{
DataSetProjects dsProjects = null;
WebServicesExceptionType exceptionType = WebServicesExceptionType.None;
try
{
dsProjects = _dataService.GetProjects();
}
catch (System.Net.WebException)
{
exceptionType = WebServicesExceptionType.WebException;
}
catch (System.Web.Services.Protocols.SoapException)
{
exceptionType = WebServicesExceptionType.SoapException;
}
catch (System.Exception)
{
exceptionType = WebServicesExceptionType.Exception;
}
if (dsProjects != null)
{
_dsProjects.Clear();
_dsProjects.Merge(dsProjects);
}
return exceptionType;
}
public WebServicesExceptionType InsertProject(ref int projectID, string projectName, string projectDescription)
{
WebServicesExceptionType exceptionType = WebServicesExceptionType.None;
try
{
projectID = _dataService.InsertProject(projectName, projectDescription);
}
catch (System.Net.WebException)
{
exceptionType = WebServicesExceptionType.WebException;
}
catch (System.Web.Services.Protocols.SoapException)
{
exceptionType = WebServicesExceptionType.SoapException;
}
catch (System.Exception)
{
exceptionType = WebServicesExceptionType.Exception;
}
return exceptionType;
}
private DataService GetWebServiceReference()
{
DataService dataService = new DataService();
string urlSetting = System.Configuration.ConfigurationManager.AppSettings["DataServiceUrl"];
if (urlSetting != null)
{
dataService.Url = urlSetting;
}
else
{
dataService.Url = "http://localhost/TaskVisionWS/DataService.asmx";
}
return dataService;
}
}
}
在软件的设计原则有好多,不过在真正的开发中很容易被大家忘的一干二净,这时记住一点就可以了:减少重复代码。
改进后的DataLayer代码:
using System;
using TaskVision.DataWS;
namespace TaskVision
{
public enum WebServicesExceptionType
{
WebException = 0,
SoapException = 1,
Exception = 2,
None = 3
}
public class DataLayer
{
private DataService _dataService;
private DataSetProjects _dsProjects = new DataSetProjects();
public DataSetProjects.ProjectsDataTable Projects
{
get
{
return _dsProjects.Projects;
}
}
public DataLayer()
{
_dataService = GetWebServiceReference();
}
public WebServicesExceptionType GetProjects()
{
DataSetProjects dsProjects = null;
WebServicesExceptionType exceptionType = WebServicesExceptionType.None;
try
{
dsProjects = _dataService.GetProjects();
}
catch (System.Exception ex)
{
exceptionType = HandleException(ex);
}
if (dsProjects != null)
{
_dsProjects.Clear();
_dsProjects.Merge(dsProjects);
}
return exceptionType;
}
public WebServicesExceptionType InsertProject(ref int projectID, string projectName, string projectDescription)
{
WebServicesExceptionType exceptionType = WebServicesExceptionType.None;
try
{
projectID = _dataService.InsertProject(projectName, projectDescription);
}
catch (System.Exception ex)
{
exceptionType = HandleException(ex);
}
return exceptionType;
}
private WebServicesExceptionType HandleException(Exception ex)
{
if (ex is System.Net.WebException)
return WebServicesExceptionType.WebException;
else if (ex is System.Web.Services.Protocols.SoapException)
return WebServicesExceptionType.SoapException;
else
return WebServicesExceptionType.Exception;
}
private DataService GetWebServiceReference()
{
DataService dataService = new DataService();
string urlSetting = System.Configuration.ConfigurationManager.AppSettings["DataServiceUrl"];
if (urlSetting != null)
{
dataService.Url = urlSetting;
}
else
{
dataService.Url = "http://localhost/TaskVisionWS/DataService.asmx";
}
return dataService;
}
}
}
using TaskVision.DataWS;
namespace TaskVision
{
public enum WebServicesExceptionType
{
WebException = 0,
SoapException = 1,
Exception = 2,
None = 3
}
public class DataLayer
{
private DataService _dataService;
private DataSetProjects _dsProjects = new DataSetProjects();
public DataSetProjects.ProjectsDataTable Projects
{
get
{
return _dsProjects.Projects;
}
}
public DataLayer()
{
_dataService = GetWebServiceReference();
}
public WebServicesExceptionType GetProjects()
{
DataSetProjects dsProjects = null;
WebServicesExceptionType exceptionType = WebServicesExceptionType.None;
try
{
dsProjects = _dataService.GetProjects();
}
catch (System.Exception ex)
{
exceptionType = HandleException(ex);
}
if (dsProjects != null)
{
_dsProjects.Clear();
_dsProjects.Merge(dsProjects);
}
return exceptionType;
}
public WebServicesExceptionType InsertProject(ref int projectID, string projectName, string projectDescription)
{
WebServicesExceptionType exceptionType = WebServicesExceptionType.None;
try
{
projectID = _dataService.InsertProject(projectName, projectDescription);
}
catch (System.Exception ex)
{
exceptionType = HandleException(ex);
}
return exceptionType;
}
private WebServicesExceptionType HandleException(Exception ex)
{
if (ex is System.Net.WebException)
return WebServicesExceptionType.WebException;
else if (ex is System.Web.Services.Protocols.SoapException)
return WebServicesExceptionType.SoapException;
else
return WebServicesExceptionType.Exception;
}
private DataService GetWebServiceReference()
{
DataService dataService = new DataService();
string urlSetting = System.Configuration.ConfigurationManager.AppSettings["DataServiceUrl"];
if (urlSetting != null)
{
dataService.Url = urlSetting;
}
else
{
dataService.Url = "http://localhost/TaskVisionWS/DataService.asmx";
}
return dataService;
}
}
}
接着我们新建一个窗体:AddProjectForm
TextBox:txtProjectName
TextBox:txtProjectDescription
Button:btnOk
Button:btnCancel
我们在主窗体中显示它:
AddProjectForm窗体的代码如下:
TextBox:txtProjectName
TextBox:txtProjectDescription
Button:btnOk
Button:btnCancel
我们在主窗体中显示它:
private void miManageAddProject_Click(object sender, System.EventArgs e)
{
AddProjectForm addProjectForm = new AddProjectForm(_dataLayer);
addProjectForm.ShowDialog();
}
{
AddProjectForm addProjectForm = new AddProjectForm(_dataLayer);
addProjectForm.ShowDialog();
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TaskVision
{
public partial class AddProjectForm : Form
{
private DataLayer _dataLayer;
public AddProjectForm(DataLayer dataLayer)
{
InitializeComponent();
_dataLayer = dataLayer;
}
private void btnOk_Click(object sender, EventArgs e)
{
if (txtProjectName.Text.Trim() != string.Empty && txtProjectDescription.Text.Trim() != string.Empty)
{
int projectID = -1;
WebServicesExceptionType exceptionType = _dataLayer.InsertProject(ref projectID, txtProjectName.Text.Trim(), txtProjectDescription.Text.Trim());
// if database update was successful add the project to our local data
if (exceptionType == WebServicesExceptionType.None)
{
DataWS.DataSetProjects.ProjectsRow dr = _dataLayer.Projects.NewProjectsRow();
dr.ProjectID = projectID;
dr.ProjectName = txtProjectName.Text.Trim();
dr.ProjectDescription = txtProjectDescription.Text.Trim();
dr.DateCreated = DateTime.Now;
_dataLayer.Projects.Rows.Add(dr);
}
else
{
MessageBox.Show("添加新项目失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.DialogResult = DialogResult.None;
}
}
else
{
this.DialogResult = DialogResult.None;
}
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
测试:using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TaskVision
{
public partial class AddProjectForm : Form
{
private DataLayer _dataLayer;
public AddProjectForm(DataLayer dataLayer)
{
InitializeComponent();
_dataLayer = dataLayer;
}
private void btnOk_Click(object sender, EventArgs e)
{
if (txtProjectName.Text.Trim() != string.Empty && txtProjectDescription.Text.Trim() != string.Empty)
{
int projectID = -1;
WebServicesExceptionType exceptionType = _dataLayer.InsertProject(ref projectID, txtProjectName.Text.Trim(), txtProjectDescription.Text.Trim());
// if database update was successful add the project to our local data
if (exceptionType == WebServicesExceptionType.None)
{
DataWS.DataSetProjects.ProjectsRow dr = _dataLayer.Projects.NewProjectsRow();
dr.ProjectID = projectID;
dr.ProjectName = txtProjectName.Text.Trim();
dr.ProjectDescription = txtProjectDescription.Text.Trim();
dr.DateCreated = DateTime.Now;
_dataLayer.Projects.Rows.Add(dr);
}
else
{
MessageBox.Show("添加新项目失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.DialogResult = DialogResult.None;
}
}
else
{
this.DialogResult = DialogResult.None;
}
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
点击添加。
添加成功。
好了这篇就说到这了,下篇会进入重点,通过ProjectID获取任务.