Usually user interface implementation is a tedious job.. Using XML data to design and implement the user interface will tremendously reduce the lines of code and make the UI design more
Trang 1278
dac.Dispose();
}
}
return sb;
}
protected StringBuilder ExecuteStoredProcedure(
SQLDatabaseAccessRootSqlDataService dataService)
{
StringBuilder sb = new StringBuilder();
int rowAffected = 0;
SQLDataAccessComponent dac = null;
SqlCommand Command = null;
try
{
SQLDatabaseAccessRootSqlDataServiceCommand serviceCommand =
dataService.Command;
string storedProcedure = serviceCommand.Text;
dac = new SQLDataAccessComponent(this.ConnectionString);
Command = new SqlCommand(storedProcedure, new SqlConnection()); Command.CommandType = CommandType.StoredProcedure;
bool output = this PopulateParameters(serviceCommand, ref Command); dac.BeginTrans();
rowAffected = (int)dac.ExecuteNonQuery(Command);
dac.CommitTrans();
if (output)
{
foreach (SqlParameter parameter in Command.Parameters)
{
if (parameter.Direction == ParameterDirection.Output) {
sb.Append(parameter.Value.ToString());
sb.Append(Environment.NewLine);
}
}
}
sb.Append(
string.Format(
" - SQL execute success, row affected : {0} -{1}", rowAffected, Environment.NewLine
)
);
sb.Append(Environment.NewLine);
}
finally
{
if (null != Command)
{
Trang 2279
Command.Dispose();
}
if (null != dac)
{
dac.Dispose();
}
}
return sb;
}
private bool PopulateParameters(
SQLDatabaseAccessRootSqlDataServiceCommand serviceCommand,
ref SqlCommand Command)
{
bool output = false;
string direction = string.Empty;
try
{
Command.Parameters.Clear();
foreach (SQLParameterRoot parameterRoot in serviceCommand.SQLParameterRoot)
{
direction =
parameterRoot.Parameter.Direction.ToString().Trim().ToUpper();
output |= direction == "OUT" || direction == "INOUT" ? true : false;
if (parameterRoot.Parameter.Type.ToUpper().StartsWith("NCHAR"))
{
int length = 0;
length = Convert.ToInt32(parameterRoot.Parameter.Size);
Command.Parameters.Add(
string.Format("@{0}", parameterRoot.Parameter.Name),
SqlDbType.NChar,
length).Value = parameterRoot.Parameter.Value;
}
else if (parameterRoot.Parameter.Type.ToUpper().StartsWith("VARCHAR"))
{
int length = Convert.ToInt32(parameterRoot.Parameter.Size);
Command.Parameters.Add(
string.Format("@{0}", parameterRoot.Parameter.Name),
SqlDbType.VarChar, length).Value = parameterRoot.Parameter.Value;
}
else if (parameterRoot.Parameter.Type.ToUpper().StartsWith("NVARCHAR"))
{
int length = Convert.ToInt32(parameterRoot.Parameter.Size);
Command.Parameters.Add(
string.Format("@{0}", parameterRoot.Parameter.Name),
SqlDbType.NVarChar,
length).Value = parameterRoot.Parameter.Value;
}
else if (parameterRoot.Parameter.Type.ToUpper().StartsWith("INT"))
Trang 3280
{
Command.Parameters.Add(
string.Format("@{0}", parameterRoot.Parameter.Name),
SqlDbType.Int, 4).Value =
Convert.ToInt32(parameterRoot.Parameter.Value);
}
else if (parameterRoot.Parameter.Type.ToUpper().StartsWith("UNIQUE")) {
Command.Parameters.Add(
string.Format("@{0}", parameterRoot.Parameter.Name),
SqlDbType.UniqueIdentifier, 32).Value =
new Guid(parameterRoot.Parameter.Value);
}
else if (parameterRoot.Parameter.Type.ToUpper().StartsWith("XML")) {
Command.Parameters.Add(
string.Format("@{0}", parameterRoot.Parameter.Name),
SqlDbType.Xml, 0).Value = parameterRoot.Parameter.Value;
}
else if (parameterRoot.Parameter.Type.ToUpper().StartsWith("TIMSTAMP")) {
Command.Parameters.Add(
string.Format("@{0}", parameterRoot.Parameter.Name),
SqlDbType.Timestamp, 0).Value = parameterRoot.Parameter.Value; }
else if (parameterRoot.Parameter.Type.ToUpper().StartsWith("DATETIME")) {
DateTime dt = DateTime.Now;
if (parameterRoot.Parameter.Value != string.Empty)
{
dt = DateTime.Parse(parameterRoot.Parameter.Value);
}
Command.Parameters.Add(
string.Format("@{0}", parameterRoot.Parameter.Name),
SqlDbType.DateTime,
0).Value = dt;
}
}
}
finally
{
if (null != Command)
{
Command.Dispose();
}
}
return output;
}
private bool PopulateStoredProcedureParameters(ref StoredProcedure storedProcedure,
Trang 4281
SQLDatabaseAccessRootSqlDataServiceCommand serviceCommand)
{
bool success = false;
try
{
foreach (SQLParameterRoot parameterRoot in serviceCommand.SQLParameterRoot)
{
StoredProcedureParameter param = null;
if (parameterRoot.Parameter.Type.ToUpper().StartsWith("NCHAR"))
{
param = new StoredProcedureParameter(storedProcedure,
string.Format("@{0}", parameterRoot.Parameter.Name),
DataType.NVarCharMax);
}
else if (parameterRoot.Parameter.Type.ToUpper().StartsWith("VARCHAR"))
{
param = new StoredProcedureParameter(storedProcedure,
string.Format("@{0}", parameterRoot.Parameter.Name),
DataType.VarCharMax);
}
else if (parameterRoot.Parameter.Type.ToUpper().StartsWith("NVARCHAR"))
{
param = new StoredProcedureParameter(storedProcedure,
string.Format("@{0}", parameterRoot.Parameter.Name),
DataType.NVarCharMax);
}
else if (parameterRoot.Parameter.Type.ToUpper().StartsWith("INT"))
{
param = new StoredProcedureParameter(storedProcedure,
string.Format("@{0}", parameterRoot.Parameter.Name),
DataType.Int);
}
else if (parameterRoot.Parameter.Type.ToUpper().StartsWith("UNIQUE"))
{
param = new StoredProcedureParameter(storedProcedure,
string.Format("@{0}", parameterRoot.Parameter.Name),
DataType.UniqueIdentifier);
}
else if (parameterRoot.Parameter.Type.ToUpper().StartsWith("XML"))
{
param = new StoredProcedureParameter(storedProcedure,
string.Format("@{0}", parameterRoot.Parameter.Name),
DataType.NVarCharMax);
}
else if (parameterRoot.Parameter.Type.ToUpper().StartsWith("TIMSTAMP"))
{
param = new StoredProcedureParameter(storedProcedure,
string.Format("@{0}", parameterRoot.Parameter.Name),
DataType.Timestamp);
}
else if (parameterRoot.Parameter.Type.ToUpper().StartsWith("DATETIME"))
Trang 5282
{
DateTime dt = DateTime.Now;
if (parameterRoot.Parameter.Value != string.Empty)
{
dt = DateTime.Parse(parameterRoot.Parameter.Value);
}
param = new StoredProcedureParameter(storedProcedure,
string.Format("@{0}", parameterRoot.Parameter.Name),
DataType.DateTime);
}
if (null != parameterRoot.Parameter)
{
storedProcedure.Parameters.Add(param);
}
}
success = true;
}
catch (Exception ex)
{
string msg = string.Empty;
if (null != ex.InnerException)
{
msg = ex.InnerException.Message;
}
else
{
msg = ex.Message;
}
}
return success;
}
#endregion
}
}
Component Design and Implementation
The concept of using XML data as a driving force can also be applied to user interface design and
implementation Usually user interface implementation is a tedious job Using XML data to design and implement the user interface will tremendously reduce the lines of code and make the UI design more componentized and more object-oriented The user interface for SQLAzureConnect is composed of two user controls and a Windows form The core concept to identify the UI user control is very straightforward A user control should naturally reflect the XML schema When the XML schema is defined, all C# data objects and all UI user controls are actually defined Since we use the UI user controls and Windows form as the front end for presenting the XML data, the instance of the XML data object will be bound to the underlying data source object