我只做过 C++ 和一些 C#,而且我是 MySQL 新手,我不明白这里的字符串生成器的目的。
此代码是用于从网页中提取数据并使用书签将其插入现有 Word 文件的程序的一部分。
我的代码
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using MySql.Data.MySqlClient;
namespace ARExtractionLibrary
{
public class DataExtractor
{
#region Attributes
private string _databaseConfig = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
private const int AR_TRACKER_ID = 10;
private const int AR_NUMBER_ID = 3;
#endregion
#region Methods
public Dictionary<string, Field> GetFieldData(int TicketID)
{
MySqlConnection sqlConnection = new MySqlConnection(_databaseConfig);
Dictionary<string, Field> fieldsDictionary = new Dictionary<string, Field>();
StringBuilder queryDefaultFields = new StringBuilder();
queryDefaultFields.Append("SELECT issues.id AS TicketID, issues.subject, issues.description, ");
queryDefaultFields.Append(" CONCAT(users.firstname,' ', users.lastname) as Author");
queryDefaultFields.Append(" FROM issues LEFT JOIN users ON users.id =issues.author_id");
queryDefaultFields.Append(" WHERE issues.id = @TicketID");
StringBuilder queryCustomFields = new StringBuilder();
queryCustomFields.Append("SELECT custom_fields.name, custom_fields.field_format, CONCAT(users.firstname,' ',users.lastname) AS value");
queryCustomFields.Append(" FROM custom_values");
queryCustomFields.Append(" LEFT JOIN custom_fields ON custom_values.custom_field_id = custom_fields.id");
queryCustomFields.Append(" JOIN users ON custom_values.value = users.id");
queryCustomFields.Append(" WHERE custom_values.customized_id = @TicketId");
queryCustomFields.Append(" AND field_format = 'user'");
queryCustomFields.Append(" UNION");
queryCustomFields.Append(" SELECT custom_fields.name, custom_fields.field_format, custom_values.value");
queryCustomFields.Append(" FROM custom_values");
queryCustomFields.Append(" LEFT JOIN custom_fields ON custom_values.custom_field_id = custom_fields.id");
queryCustomFields.Append(" WHERE custom_values.customized_id = @TicketId");
queryCustomFields.Append(" AND field_format <> 'user'");
sqlConnection.Open();
//First query
MySqlCommand sqlCommand = null;
sqlCommand = new MySqlCommand(queryDefaultFields.ToString(), sqlConnection);
sqlCommand.Parameters.AddWithValue("@TicketId", TicketID);
MySqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
while (sqlDataReader.Read())
{
Field subject = new Field("Subject", sqlDataReader["subject"].ToString());
fieldsDictionary.Add(subject.Name, subject);
Field description = new Field("Description", sqlDataReader["description"].ToString());
fieldsDictionary.Add(description.Name, description);
Field ticketID = new Field("TicketID", sqlDataReader["TicketID"].ToString());
fieldsDictionary.Add(ticketID.Name, ticketID);
Field author = new Field("Author", sqlDataReader["Author"].ToString());
fieldsDictionary.Add(author.Name, author);
}
sqlDataReader.Close();
//Second query
sqlCommand = new MySqlCommand(queryCustomFields.ToString(), sqlConnection);
sqlCommand.Parameters.AddWithValue("@TicketId", TicketID);
sqlDataReader = sqlCommand.ExecuteReader();
while (sqlDataReader.Read())
{
string fieldName = sqlDataReader["name"].ToString();
string fieldValue = sqlDataReader["value"].ToString();
string fieldFormat = sqlDataReader["field_format"].ToString();
if (fieldsDictionary.ContainsKey(fieldName))
{
fieldsDictionary[fieldName].Values.Add(fieldValue);
}
else
{
Field localField = new Field(fieldName, fieldValue, fieldFormat);
fieldsDictionary.Add(localField.Name, localField);
}
}
sqlDataReader.Close();
sqlConnection.Close();
return fieldsDictionary;
}
public Dictionary<int, string> GetARs()
{
Dictionary<int, string> ARDictionary = new Dictionary<int, string>();
MySqlConnection sqlConnection = new MySqlConnection(_databaseConfig);
StringBuilder sqlCommandIsAR = new StringBuilder();
sqlCommandIsAR.Append("SELECT issues.id AS TicketID, issues.tracker_id AS Tracker, issues.subject AS Subject, custom_values.custom_field_id, custom_values.value");
sqlCommandIsAR.Append(" FROM issues LEFT JOIN Custom_values ON custom_values.customized_id = issues.id ");
sqlCommandIsAR.Append(" WHERE tracker_id = @IsAR AND custom_field_id = @IsARNumber ");
sqlConnection.Open();
MySqlCommand commandGetARs = new MySqlCommand(sqlCommandIsAR.ToString(), sqlConnection);
commandGetARs.Parameters.AddWithValue("@IsAR", AR_TRACKER_ID);
commandGetARs.Parameters.AddWithValue("@IsARNumber", AR_NUMBER_ID);
MySqlDataReader sqlDataReader = null;
sqlDataReader = commandGetARs.ExecuteReader();
while (sqlDataReader.Read())
{
string DictionaryValue = sqlDataReader["TicketID"].ToString() + " - " + sqlDataReader["Subject"].ToString();
if (sqlDataReader["value"].ToString().Length != 0)
{
DictionaryValue += " [" + sqlDataReader["value"].ToString() + "]";
}
int key = int.Parse(sqlDataReader["TicketID"].ToString());
ARDictionary.Add(key, DictionaryValue);
}
sqlDataReader.Close();
sqlConnection.Close();
return ARDictionary;
}
#endregion
}
}