无法从数据库正确获取数据

无法从数据库正确获取数据

问题描述:


我正在尝试从数据库中获取数据.

为此,我建立了一个存储过程,如

Hi,
I''m trying to get the data from the database.

For that i build one stored procedure as

ALTER Procedure [dbo].[Select_Students](@Sno int = null)
As
Begin
If @Sno Is Null
Select Sno,Sname,Class,Fees From Stud 
Else
Select Sname,Class,Fees From Stud Where Sno=@Sno
End



而获取"按钮下的代码为



And the code under get button as

private void button1_Click(object sender, EventArgs e)
        {
            cmd.Parameters.Clear();
            ds = new DataSet();
            da = new SqlDataAdapter();
            cmd.CommandText = "Select_Students";
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "Stu");
            string value_of_sno = textBox1.Text;
            cmd.Parameters.Add("@sno", value_of_sno);
            
              if (ds.Tables[0].Rows.Count > 0)
            {
                textBox2.Text = ds.Tables[0].Rows[0][1].ToString();
                textBox3.Text = ds.Tables[0].Rows[0][2].ToString();
                textBox4.Text = ds.Tables[0].Rows[0][3].ToString();
            }
            else
                MessageBox.Show("Record Not Existed");
        }


但总是我只获得第一个记录值.
我认为textBox2.Text = ds.Tables [0] .Rows [0] [1] .ToString();
textBox3.Text = ds.Tables [0] .Rows [0] [2] .ToString();
textBox4.Text = ds.Tables [0] .Rows [0] [3] .ToString();
由于此代码,我只能获取第一个记录值.
可以帮助我根据我在第一个文本框中输入的值来获取记录值.


But always i''m getting the first record values only.
I think textBox2.Text = ds.Tables[0].Rows[0][1].ToString();
textBox3.Text = ds.Tables[0].Rows[0][2].ToString();
textBox4.Text = ds.Tables[0].Rows[0][3].ToString();
due to this code only i''m getting the first record values.
can one help me to get the record values depending on the value entered by me in the first textbox.

您已经弄乱了代码.在Fill之后添加参数无效.
试试这个
You have messed up your code. Adding parameter after Fill serves nothing.
Try this
cmd.Parameters.Clear();
cmd.CommandText = "Select_Students";
string value_of_sno = textBox1.Text;
cmd.Parameters.Add("@sno", value_of_sno);
ds = new DataSet();
da = new SqlDataAdapter(cmd);
da.Fill(ds, "Stu");


更改
if (ds.Tables[0].Rows.Count > 0)
            {
                textBox2.Text = ds.Tables[0].Rows[0][1].ToString();
                textBox3.Text = ds.Tables[0].Rows[0][2].ToString();
                textBox4.Text = ds.Tables[0].Rows[0][3].ToString();
            }



至:



to:

if (ds.Tables[0].Rows.Count > 0)
{
    foreach (DataRow row in ds.Tables[0].Rows)
    {
        if (row["sno"].ToString() == textbox1.Text)
        {
            textBox2.Text = row[1].ToString();
            textBox3.Text = row[2].ToString();
            textBox4.Text = row[3].ToString();
            break; // get out of the foreach
        }
    }
}


如果textBox1.Text的值为空白,则不要设置参数.
if the value of textBox1.Text is blank, then do not set the parameter.
string value_of_sno = textBox1.Text;
            cmd.Parameters.Add("@sno", value_of_sno);


并由digimanu建议,将参数设置在适当的位置.


and suggested by digimanu, set the parameters at the proper places.