SQL C#WPF查询已展开
本文关键字:查询 C#WPF SQL | 更新日期: 2023-09-27 18:24:45
我在这里得到了一个查询的答案SQL Select All Without Values in Another Table。
我刚刚被要求集成来自另一个数据库的数据。这就是我现在所拥有的。
string _loanSubcontractor = TableNames.Default.LoansSubcontractors;
string _loanPacific = TableNames.Default.LoansPacific;
string _tools = TableNames.Default.Tools;
string _selectStatement = " SELECT [Tools].[Type], [Tools].[Brand], [Tools].[Serial], [Tools].[Year], [Tools].[Code] ";
string _groupBy = " GROUP BY [Tools].[Type], [Tools].[Brand], [Tools].[Serial], [Tools].[Year], [Tools].[Code], [Tools].[Working] ";
string _searchItems = " ([Tools].Code LIKE @toolSerial OR [Tools].Serial LIKE @toolSerial) AND ([Tools].[Working] = 'True' OR [Tools].[Working] IS NULL) ";
SqlConnection myConnection = new SqlConnection(Connection.Default.ConnectionString);
//Checks the main tool information
myConnection.Open();
SqlCommand getTool = new SqlCommand(
_selectStatement + "FROM [" + _tools + "] LEFT OUTER JOIN [" + _loanSubcontractor + "] ON " +
_tools + ".code = [" + _loanSubcontractor + "].ToolCode FULL JOIN [" + _loanPacific + "] ON " + _tools + ".Code = " +
_loanPacific + ".ToolCode WHERE [" + _loanSubcontractor + "].ToolCode IS NULL AND [" + _loanPacific + "].ToolCode IS NULL AND (" + _searchItems + ")" +
"UNION " +
_selectStatement + " FROM [" + _loanSubcontractor + "] INNER JOIN " + _tools + " ON " + _tools + ".Code = [" + _loanSubcontractor + "].ToolCode " +
"INNER JOIN " + _loanPacific + " ON " + _loanPacific + ".ToolCode = " + _tools + ".Code " + _groupBy +
"HAVING (COUNT(" + _loanSubcontractor + ".ReturnDate) = COUNT(*) OR COUNT(" + _loanPacific + ".ReturnDate) = COUNT(*)) " +
" AND " + _searchItems, myConnection);
getTool.Parameters.Add("@toolSerial", SqlDbType.NVarChar).Value = "%" + toolSerial + "%";
我有两个贷款表(一个用于员工,一个用于分包商),因为属性名称不同,数据类型也不同。从本质上讲,我需要检查该工具是否正常工作,并且该工具在任何一个贷款表中都没有被出租(如返回日期为空所示)。这两张表中可能有也可能没有贷款。
另外,有人能为我提供一个链接,展示C#中SQL的良好格式技术吗?
i建议您将linq用于SQL。这是ADO.NET的一部分,您可以使用100%英亩的长查询和简单的
用于linq到SQL 的msdn.microsoft.com
LINQ to SQL:关系数据的.NET语言集成查询
C#中的简单LINQ to SQL