-2
我有一個數據訪問類,它使用某些存儲過程來選擇/插入/更新數據庫中的數據。我怎樣才能將這個類轉換爲Web服務和所有方法作爲Web方法?我使用vs 2012和c#。這datacess類的部分的一個例子是如何將現有數據訪問類轉換爲web服務
namespace HPS.Thesaurus.Data
{
public class TermDB : DbObjectStatic
{
#region Public Methods
#region CRUD Methods
public static int Add(Term term, string useString, string useForString, string broaderTermString, string narrowerTermString, string relatedTermString, string userName, ref int historyTermId)
{
int rowsAffected = 0;
SqlParameter[] parameter =
{
new SqlParameter("@name", SqlDbType.VarChar, 200),
new SqlParameter("@type", SqlDbType.Int),
new SqlParameter("@scope", SqlDbType.VarChar, 2000),
new SqlParameter("@hpspublic", SqlDbType.Bit),
new SqlParameter("@additionalnotes",SqlDbType.VarChar,2000),
new SqlParameter("@usestring", SqlDbType.VarChar, 2000),
new SqlParameter("@useforstring", SqlDbType.VarChar, 2000),
new SqlParameter("@broaderstring", SqlDbType.VarChar, 2000),
new SqlParameter("@narrowerstring", SqlDbType.VarChar, 2000),
new SqlParameter("@relatedstring", SqlDbType.VarChar, 2000),
new SqlParameter("@changedby", SqlDbType.VarChar, 15),
new SqlParameter("@id", SqlDbType.Int),
new SqlParameter("@historytermid", SqlDbType.Int)
};
parameter[0].Value = term.Name;
parameter[1].Value = term.Type;
parameter[2].Value = term.Scope;
parameter[3].Value = term.HPSPublic;
parameter[4].Value = term.AdditionalNotes;
if (useString.Trim().Length > 0)
{
parameter[5].Value = useString;
}
if (useForString.Trim().Length > 0)
{
parameter[6].Value = useForString;
}
if (broaderTermString.Trim().Length > 0)
{
parameter[7].Value = broaderTermString;
}
if (narrowerTermString.Trim().Length > 0)
{
parameter[8].Value = narrowerTermString;
}
if (relatedTermString.Trim().Length > 0)
{
parameter[9].Value = relatedTermString;
}
if (userName.Trim().Length > 0)
{
parameter[10].Value = userName;
}
parameter[11].Direction = ParameterDirection.Output;
parameter[12].Direction = ParameterDirection.Output;
try
{
RunProcedure("Term_Add", parameter, out rowsAffected, AppConfiguration.ConnectionString());
}
catch
{
return -1;
}
historyTermId = (int)parameter[12].Value;
return (int)parameter[11].Value;
}
public static bool Delete(Term term)
{
int rowsAffected = 0;
SqlParameter[] parameter =
{
new SqlParameter("@id", SqlDbType.Int)
};
parameter[0].Value = term.Id;
try
{
RunProcedure("Term_Delete", parameter, out rowsAffected, AppConfiguration.ConnectionString());
}
catch
{
return false;
}
return rowsAffected > 0;
}
public static Term GetTerm(int termId)
{
SqlParameter[] parameters =
{
new SqlParameter("@id", SqlDbType.Int)
};
parameters[0].Value = termId;
using (DataTable dt = RunProcedure("Term_Get", parameters, "terms", AppConfiguration.ConnectionString()))
{
if (dt.Rows.Count > 0)
{
return FillData(dt.Rows[0]);
}
else
{
return null;
}
}
}
public static Term GetTerm(string termName)
{
SqlParameter[] parameters =
{
new SqlParameter("@name", SqlDbType.VarChar, 1000)
};
parameters[0].Value = termName;
using (DataTable dt = RunProcedure("Term_GetByName", parameters, "terms", AppConfiguration.ConnectionString()))
{
if (dt.Rows.Count > 0)
{
return FillData(dt.Rows[0]);
}
else
{
return null;
}
}
}
public static bool Update(Term term, string useString, string useForString, string broaderTermString, string narrowerTermString, string relatedTermString, string changedBy, ref int historyTermId)
{
int rowsAffected = 0;
SqlParameter[] parameter =
{
new SqlParameter("@id", SqlDbType.Int),
new SqlParameter("@name", SqlDbType.VarChar, 200),
new SqlParameter("@type", SqlDbType.Int),
new SqlParameter("@scope", SqlDbType.VarChar, 2000),
new SqlParameter("@hpspublic", SqlDbType.Bit),
new SqlParameter("@additionalnotes",SqlDbType.VarChar,2000),
new SqlParameter("@usestring", SqlDbType.VarChar, 2000),
new SqlParameter("@useforstring", SqlDbType.VarChar, 2000),
new SqlParameter("@broaderstring", SqlDbType.VarChar, 2000),
new SqlParameter("@narrowerstring", SqlDbType.VarChar, 2000),
new SqlParameter("@relatedstring", SqlDbType.VarChar, 2000),
new SqlParameter("@changedBy", SqlDbType.VarChar, 15),
new SqlParameter("@historyTermId", SqlDbType.Int),
};
parameter[0].Value = term.Id;
parameter[1].Value = term.Name;
parameter[2].Value = term.Type;
parameter[3].Value = term.Scope;
parameter[4].Value = term.HPSPublic;
parameter[5].Value = term.AdditionalNotes;
if (useString.Trim().Length > 0)
{
parameter[6].Value = useString;
}
if (useForString.Trim().Length > 0)
{
parameter[7].Value = useForString;
}
if (broaderTermString.Trim().Length > 0)
{
parameter[8].Value = broaderTermString;
}
if (narrowerTermString.Trim().Length > 0)
{
parameter[9].Value = narrowerTermString;
}
if (relatedTermString.Trim().Length > 0)
{
parameter[10].Value = relatedTermString;
}
if (changedBy.Trim().Length > 0)
{
parameter[11].Value = changedBy;
}
parameter[12].Direction = ParameterDirection.Output;
try
{
RunProcedure("Term_Update", parameter, out rowsAffected, AppConfiguration.ConnectionString());
}
catch
{
return false;
}
historyTermId = (int)parameter[12].Value;
return rowsAffected > 0;
}
#endregion
public static TermList GetAll()
{
TermList tl = null;
using (DataTable dt = RunProcedure("Term_GetAll", new IDataParameter[] { }, "terms", AppConfiguration.ConnectionString()))
{
tl = new TermList();
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
tl.Add(FillData(dr));
}
}
}
return tl;
}