如果Result =多个

时间:2014-02-20 16:04:35

标签: c# sql

我不知道该怎么做,但我想这样做......

           using (SqlCommand sqlCmd2 = new SqlCommand { CommandText = "SELECT ID FROM Products.Products WHERE PartNumber like '" + textBox8.Text + "'", Connection = sqlCon2 })
                using (SqlCommand sqlCmd3 = new SqlCommand { CommandText = "SELECT ID FROM Products.Products WHERE PartNumber like '" + textBox9.Text + "'", Connection = sqlCon2 })
{
                sqlCon2.Open();
                object result1 = sqlCmd2.ExecuteScalar();
                if (result1 != null)
                {
                    textBox1.Text = sqlCmd2.ExecuteScalar().ToString();
                    textBox2.Text = sqlCmd3.ExecuteScalar().ToString();

但我希望result1同时拥有sqlCmd2和sqlCmd3。我该怎么做?

可能是这样的?

这是错误的.....我只是不知道如何正确地写它。

 object result1 = sqlCmd2.ExecuteScalar(), sqlCmd3.ExecuteScalar();

1 个答案:

答案 0 :(得分:4)

您所寻找的可能是ArrayList。这两个都可以包含多个值的序列,这将允许您执行此操作,例如:

var resultArray = new[] { 
    sqlCmd2.ExecuteScalar().ToString(), 
    sqlCmd3.ExecuteScalar().ToString() 
};

// Read results with resultArray[0], resultArray[1]

另一个选择是将每个结果分配给一个对象属性:

var result = new { 
    OneResult = sqlCmd2.ExecuteScalar(), 
    OtherResult = sqlCmd3.ExecuteScalar() 
};

// Read results with result.OneResult or result.OtherResult