这种方法将无休止地帮助你。这是我的宝贝,是一个真正的美女。
它接受您的 SQLQuery 作为参数并返回一个 DataTable 对象:
public DataTable DBGetDataTable(string SQLQuery)
{
string MethodName = "public DataTable DBGetDataTable(string SQLQuery)";
DataTable Result = null;
SqlConnection SqlConnection = null;
SqlCommand SqlCommand = null;
try
{
string DatabaseName = "";
string ServerNameOrIP = "";
string DatabaseUserID = "";
string Password = "";
string ConnectionString = "database=" + DatabaseName + ";server=" + ServerNameOrIP + ";user ID=" + DatabaseUserID + ";PWD=" + Password + ";Connection Timeout=5000";
SqlConnection = new SqlConnection(ConnectionString);
SqlCommand = new SqlCommand(SQLQuery, SqlConnection);
SqlConnection.Open();
SqlDataReader SqlDataReader = SqlCommand.ExecuteReader();
if (SqlDataReader.HasRows)
{
DataTable Dt = new DataTable();
Dt.Load(SqlDataReader);
Result = Dt;
}
}
catch (Exception ex)
{
//Common.Exception(ClassName, MethodName, ex);
}
finally
{
SqlConnection.Close();
SqlConnection.Dispose();
SqlConnection = null;
SqlCommand.Dispose();
SqlCommand = null;
}
return Result;
}
由于您的数据库凭据与我的不同,您需要在上述代码中为以下变量提供值:
- 字符串数据库名称 = "";
- 字符串 ServerNameOrIP = "";
- 字符串数据库用户 ID = "";
- 字符串密码 = "";
它非常有弹性,并且可以非常有效地自行清理,因此专为大量数据库使用而设计。
这是一个使用它的例子:
StringBuilder sbSQL = new StringBuilder();
sbSQL.AppendLine(" SET DATEFORMAT DMY");
sbSQL.AppendLine(" SELECT");
sbSQL.AppendLine(" TillPayment.TillPaymentId As Id");
sbSQL.AppendLine(" , TillPayment.TransactionGuid As TransactionCode");
sbSQL.AppendLine(" , TillPayment.Amount As Price");
sbSQL.AppendLine(" , TillPayment.DiscountPercentage");
sbSQL.AppendLine(" , TillPayment.Processed As ProcessedDate");
sbSQL.AppendLine(" , PaymentType.Name As PaymentType");
sbSQL.AppendLine(" , TillPayment.ItemType As Item");
sbSQL.AppendLine(" , TillUser.LoginCode As Login");
sbSQL.AppendLine(" , TillUser.Name As UserName");
sbSQL.AppendLine(" , TillPayment.TillName As Till");
sbSQL.AppendLine(" FROM");
sbSQL.AppendLine(" [tblTillPayment] TillPayment");
sbSQL.AppendLine(" INNER JOIN [tblPaymentType] PaymentType ON TillPayment.PaymentTypeId = PaymentType.PaymentTypeId");
sbSQL.AppendLine(" INNER JOIN [tblTillUser] TillUser ON TillPayment.TillLoginCode = TillUser.LoginCode");
int SelectedMonthFrom = GetMonthAsNumber(CboMonthReportMonth.SelectedItem.Text);
int SelectedYearFrom = int.Parse(CboMonthReportYear.SelectedItem.Text);
int SelectedMonthTo = SelectedMonthFrom == 12 ? 1 : SelectedMonthFrom + 1;
int SelectedYearTo = SelectedMonthTo == 1 ? SelectedYearFrom + 1: SelectedYearFrom;
sbSQL.AppendLine(" WHERE");
sbSQL.AppendLine(" Processed >= '01-" + SelectedMonthFrom + "-" + SelectedYearFrom + " 00:00'");
sbSQL.AppendLine(" AND Processed < '01-" + SelectedMonthTo + "-" + SelectedYearTo + " 00:00'");
sbSQL.AppendLine(" AND NOT(TillUser.LoginCode = '2503' or TillUser.LoginCode = '1453')");
sbSQL.AppendLine(" ORDER BY");
sbSQL.AppendLine(" Till");
sbSQL.AppendLine(" , Processed");
DataTable DtReport = DBGetDataTable(sbSQL.ToString());
当然,您的 SQL 查询将与我的不同。
请注意,我使用了 StringBuilder 对象,但如果您更愿意直接写入字符串,那么您可以这样做。
你写了
我仍然得到相同的错误错误 3 'OrderTransaction_Master.get_OrderTransaction_Master_ByOrderID(long)': 并非所有代码路径都返回值 C:\IPC\App_Code\OrderTransaction_Master.cs 33 22 http://localhost/ipc/ 从之前
这表明名为 OrderTransaction get_OrderTransaction_Master_ByOrderID(long OrderId) 的方法没有默认返回值
我想有问题的方法应该如下所示:
public OrderTransaction get_OrderTransaction_Master_ByOrderID(long OrderId)
{
string MethodName = "public OrderTransaction get_OrderTransaction_Master_ByOrderID(long OrderId)";
OrderTransaction Result = null;
try{
StringBuilder sbSQL = new StringBuilder();
sbSQL.AppendLine(" SELECT");
sbSQL.AppendLine(" *");
sbSQL.AppendLine(" FROM");
sbSQL.AppendLine(" dbo.OrderTransaction_Master");
sbSQL.AppendLine(" WHERE");
sbSQL.AppendLine(" OrderID = " + OrderID);
DataTable Dt = DBGetDataTable(sbSQL.ToString())
OrderTransaction OrderTransaction = new OrderTransaction(Dt);
Result = OrderTransaction;
}
catch(Exception ex)
{
//Common.Exception(ClassName,MethodName,ex);
}
return Result;
}
请询问您是否有任何问题。