1

我有这个我试图在其中运行 sql 查询的公共 DataTable,但是该死的东西不会工作......

public DataTable get_OrderTransaction_Master_ByOrderID(Int64 orderID)
    {
        cn = new SqlConnection(objCommon.IpcConnectionString);
        cmd = new SqlCommand("select * from dbo.OrderTransaction_Master where orderID = " + orderID, cn);
        cmd.CommandType = CommandType.Text;
        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
    }

get_OrderTransaction_Master_ByOrderID我在说下面有一条红线

Error   3   'OrderTransaction_Master.get_OrderTransaction_Master_ByOrderID(long)': not all code paths return a value    C:\IPC\App_Code\OrderTransaction_Master.cs  32  22  http://localhost/ipc/

我究竟做错了什么?

4

3 回答 3

6

您的方法的返回类型是 a DataTable,但您的方法中没有 return 语句。您也在执行cmd.ExecuteNonQuery(),您应该在哪里执行cmd.ExecuteReader()并加载一个DataTable

public DataTable get_OrderTransaction_Master_ByOrderID(Int64 orderID)
{
    DataTable dt = new DataTable();
    using(var cn = new SqlConnection(objCommon.IpcConnectionString))
    {
        using(var cmd = new SqlCommand(
            "select * from dbo.OrderTransaction_Master where orderID = " + orderID, cn))
        {
            cmd.CommandType = CommandType.Text;
            cn.Open();
            using(SqlDataReader reader = cmd.ExecuteReader())
            {
                dt.Load(reader);            
                return dt;
            }
        }
    }
}

我把SqlConnection,SqlCommandSqlDataReader放在using语句中。一旦退出 using 语句,这将正确处理对象(关闭连接等)。

于 2012-10-12T20:03:03.067 回答
0

您的返回类型是 DataTable 但您的函数不返回任何内容。

2件事,把函数改成void,或者返回一个dataTable

于 2012-10-12T23:33:53.690 回答
-1

这种方法将无休止地帮助你。这是我的宝贝,是一个真正的美女。

它接受您的 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;
    }

请询问您是否有任何问题。

于 2014-05-20T15:07:32.900 回答