一般说明:多个SQL数据库的单个SQL查询(通过连接)返回除一个数据库以外的所有项目的所有项目。未返回的项目通过一个可以(有时是null)的项目加入。
具体说明: 我正在继续开发内部票务系统以进行工作。我刚刚开始使用C#,SQL和Web开发大约一个月,所以我仍然围绕着Jquery,SQL,C#和MVC的所有互连和语法。
目前,我正在尝试在表格中显示SQL查询,以显示支持会员的故障单的简要信息。我已经显示了所有内容,除了" CircuitDescription"这是一个非常重要的元素,以支持区分电路。它正在桌面上作为" undefined"我收集的是初始化变量的JQuery响应。所有其他值都在网页上显示。
通过Microsoft SQL Server Management Studio运行SQL查询会显示包含电路描述的列。
声明:
在搜索这个时,我看到的帖子阻止了多个数据库查询,但这就是之前编写程序的方式,所以我想让我的代码尽可能地与我们的代码保持一致。
因此,如果我们可以跳过隐含的部分,我就是一个白痴(这是我在stackoverflow上提出问题的唯一体验),这将是可爱的。
如果你帮助我解决这个问题,并帮助我了解你的建议为什么有效,那么你可以随意暗示或直接给我打电话。
代码:
C#/ SQL查询:
- 连接语句将列表与DB" Tickets"中的数值组合在一起。使用其他数据库中的数值。这些数据库包含2列,数值和相应的字符串描述。
- 完全外部联接将列表中的数字circuitID与电路数据库中的数字circuitID组合在一起。
- 电路数据库保存着我正在努力的电路描述。
- 有些CircuitID值为null,我怀疑这可能是为什么这不起作用。我接收数据的其他连接语句都不为空。
public static async Task<List<Ticket>> GetAllTicketsForCustomerAsync(DBAccess db, int customerID)
{
var cmd = "select TicketID, Tickets.DateCreated, Tickets.DateResolved, Tickets.CustomerCircuitID, CircuitDescription, TicketTypeDesc, BillingStatusDescription, TicketStatusDescription " +
"from Tickets " +
"join TicketTypes on Tickets.TicketTypeID = TicketTypes.TicketTypeID " +
"join TicketStatusTypes on Tickets.TicketStatus = TicketStatusTypes.TicketStatus " +
"join TicketBillingStatusTypes on Tickets.BillingStatus = TicketBillingStatusTypes.BillingStatus " +
"full outer join CustomerCircuits on Tickets.CustomerCircuitID = CustomerCircuits.CustomerCircuitID " +
"where Tickets.CustomerID = " + customerID +
"order by Tickets.TicketID DESC";
var table = await db.ReadTableAsync(cmd);
return (from DataRow row in table.Rows select db.AssignFromRow<Ticket>(row)).ToList();
}
JQuery的:
- circDesc的第三名运营商将列出没有circuitID的任何票证作为&#34;非特定&#34;对于他们的电路描述。否则,他们应该显示当前正在进行的电路描述&#34; Undefined&#34;
function buildPartialCustomerTicketsTable(tickets) {
var table = "";
var maxSize = (tickets.length < 5) ? tickets.length : 5;
for (var i = 0; i < maxSize; i++) {
var t = tickets[i];
var circDesc = (t.CustomerCircuitID == null) ? "Nonspecific" : t.CircuitDescription;
var rowClass = ((i % 2) == 0) ? "listRowNormal" : "listRowAlternate";
table += "<tr class='" + rowClass + "'>"
+ "<td class='listElement'><a href='#' onclick='viewTicket(" + t.TicketID + ",true)'>update</a></td>"
+ "<td class='listElement'>" + t.TicketStatusDescription + "</td>"
+ "<td class='listElement'>" + formatDate(t.DateCreated) + "</td>"
+ "<td class='listElement'>" + formatDate(t.DateResolved) + "</td>"
+ "<td class='listElement'>" + circDesc + "</td>"
+ "<td class='listElement'>" + t.TicketTypeDescription + "</td>"
+ "<td class='listElement'>" + t.BillingStatusDescription + "</td>"
+ "<td class='listElement'>" + t.TicketID + "</td>"
+ "</tr>";
}
return table;
}
请求的代码:
public T AssignFromRow<T>(DataRow row) where T : new()
{
var rec = new T();
this.AssignFromRow(row, rec);
return rec;
}
public void AssignFromRow(DataRow row, object rec)
{
if (row == null || rec == null)
{
return;
}
// Look at all of the properties in the record
PropertyInfo[] recprops = rec.GetType().GetProperties();
foreach (PropertyInfo pi in recprops)
{
// default the sql column name to the property name
string columnName = pi.Name;
// skip any read only parameters
if (!pi.CanWrite)
{
continue;
}
// Check for a mapping attribute. This attribute can change the name of the table column name from the default.
var customAttrs = pi.GetCustomAttributes(typeof(MappingAttribute), false);
if (customAttrs.Length > 0)
{
var mapping = (MappingAttribute)customAttrs[0];
if (!string.IsNullOrEmpty(mapping.ColumnName))
{
columnName = mapping.ColumnName;
}
}
// If the row does not have this element name then skip it
if (!row.Table.Columns.Contains(columnName))
{
continue;
}
// If the DataRow has a value with the same name, and it is not null, then assign it
object dbval = row[columnName];
if (dbval != null && !(dbval is DBNull))
{
pi.SetValue(rec, dbval, null);
}
}
}