我的数据库中有个tb_storage表,表中有仓库号,设备名称,库存3个列名
我想要查找到仓库号跟设备名称与我textbox1跟textbox2中输入的内容一样后的那一行的库存的值,该如何获得?
用C#语句,我的代码如下:
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
                if (textBox1.Text == "")
                {
                    MessageBox.Show("请输入查询仓库号!");
                    return;
                }
                if (textBox2.Text == "")
                {
                    MessageBox.Show("请输入查询设备名称!");
                    return;
                }
                if (textBox3.Text != "")
                {
                    MessageBox.Show("请不要填写库存!");
                    return;
                }
                //实例SqlConnection对象打开数据库连接
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = "Server=.;uid=sa;pwd=sa;database=Manager";
                conn.Open();
                string strselect = "select COUNT(*) from tb_storage where 仓库号='" + textBox1.Text + "' and 设备名称='"+textBox2.Text+"' ";
                SqlCommand cmd = new SqlCommand(strselect, conn);
                int intConut = Convert.ToInt32(cmd.ExecuteScalar().ToString());//查询输入编号是否有效
                if (intConut != 0)//编号有效
                {
                    string efg;
                    efg = "select 库存 from tb_storage where 仓库号='" + textBox1.Text + "' and 设备名称='" + textBox2.Text + "' ";
                    string efgh = DataTable.Rows[0][0].ToString();
                    MessageBox.Show("库存为:" + efgh, "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("输入的仓库号跟设备名称不在表中,无法查询!");
                }
        }
怎么样把查询到的值赋值给textbox3,或者直接新创建一个string 赋值;求高手帮忙,在线等……谢谢了……

解决方案 »

  1.   

    cmd.CommandText=efg;
    SqlDataReader rde = cmd.ExecuteReader();
    if(rde.Reade())
    {
       textbox3.Text=Convert.ToString(rde["库存"]);
    }
    rde.Close();
    你是这个意思不?
      

  2.   

    根据你提供的应该是如下:
    string s1 = DataTable.Rows[0][0].ToString();//仓库号
    string s2 = DataTable.Rows[0][1].ToString();//设备名称
    string s3 = DataTable.Rows[0][2].ToString();//库存
      

  3.   

    都已经把efgh读出来了 直接赋值不就行了吗
      

  4.   

    lz写的太繁琐了就按照一楼的格式写吧。。
    还有LZ数据库中的字段能不能用英文啊
    以上纯属个人建议
      

  5.   

    //你的语句有问题//实例SqlConnection对象打开数据库连接
      SqlConnection conn = new SqlConnection();
      conn.ConnectionString = "Server=.;uid=sa;pwd=sa;database=Manager";
      conn.Open();
      string strselect = "select 库存 from tb_storage where 仓库号='" + textBox1.Text + "' and 设备名称='"+textBox2.Text+"' ";//直接查询库存来
      SqlCommand cmd = new SqlCommand(strselect, conn);
    SqlDataReader rde = cmd.ExecuteReader();
    if(rde.hasRow() && rde.Reade())
    {
      textbox3.Text=Convert.ToString(rde["库存"]);
     MessageBox.Show("库存为:" + rde["库存"].ToString(), "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
    } else
      {
      MessageBox.Show("输入的仓库号跟设备名称不在表中,无法查询!");
      }
    rde.Close();
     /** int intConut = Convert.ToInt32(cmd.ExecuteScalar().ToString());//查询输入编号是否有效
      if (intConut != 0)//编号有效
      {
      string efg;
      efg = "select 库存 from tb_storage where 仓库号='" + textBox1.Text + "' and 设备名称='" + textBox2.Text + "' ";
      string efgh = DataTable.Rows[0][0].ToString();
      MessageBox.Show("库存为:" + efgh, "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
      }
    **/
     
      }