SQL从两个表中读取项目 [英] SQL read items from two tables

查看:56
本文介绍了SQL从两个表中读取项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试从两个不同的表(SQL)中检索一些值,但是我得到了一般异常错误。



我的意思是我有表销售和表item_sales



使用以下代码我从需要的变量中检索正确的值

Hi guys ,
I'm trying to retrive some values from two different tables(SQL) but I get generall exception error.

What I mean is I have table sales and table item_sales

with the following code I retrive the correct values from variables that i need

protected string InputId { get; set; }
    protected string InputValue { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        //this.InputValue = "100";
        SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        SqlCommand sqlCommand = new SqlCommand("select id_product,price from item_sales where id_sales=(select max(id_sales) from item_sales)", con1);
        con1.Open();

        using (SqlDataReader read = sqlCommand.ExecuteReader())
        {
            while (read.Read())
            {
                string id_product = read["Id_product"].ToString();
                string price = read["price"].ToString();
              //  this.InputValue = price;
                this.InputId = id_product;
            }
            read.Close();
        }
        con1.Close();





但如果可以,我需要在同一页面检索全价项目,所以我添加以下代码



But I need in the same page if that is possible to retrieve the full price item so i add the following code

SqlCommand sqlCommand2 = new SqlCommand("select price_item from sales where id_sales=(select max(id_sales) from sales)", con1);
        con1.Open();
        using (SqlDataReader read2 = sqlCommand.ExecuteReader())
        {
            while (read2.Read())
            {


                string total_cart_price = read2["price_item"].ToString();
                this.InputValue = total_cart_price;

            }
            read2.Close();
        }
        con1.Close();





但我收到错误用户代码未处理IndexOutOfRangeException



有没有办法从两个不同的表中检索值?如何?



Thnx提前为您服务时间!



Jason!



but i receive an error "IndexOutOfRangeException was unhandled by user code"

Is any way to retrieve values from two different tables? and how?

Thnx in advance for yours Time!

Jason!

推荐答案

您的返回查询可能没有任何要读取的记录。我会更改你的代码以检查以确保阅读器有行



Your return query may not have any records to read. I would change your code to check to make sure that the reader has rows

if (read2.HasRows == true)
{
 while(read2.Read())
 {
  //do sutff
 }
}





进一步阅读使用DataReader检索数据 [ ^ ]


它有效!!!谢谢你的帮助我的朋友

除了我发现我的代码中有另一个错误:



here->使用(SqlDataReader read2 = sqlCommand.ExecuteReader())

我应该有: - >使用(SqlDataReader read2 = sqlCommand2.ExecuteReader())

,因为我在错误的桌子上进行查询!



Thnx为您提供帮助和你的时间
it worked!!! thank you for your help my friend
except for that i found another error that i have in my code:

here-> using (SqlDataReader read2 = sqlCommand.ExecuteReader())
i shoud be have :-> using (SqlDataReader read2 = sqlCommand2.ExecuteReader())
because i was making the query at the wrong table!

Thnx for your help and your time


这篇关于SQL从两个表中读取项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆