无法从数据库正确获取数据 [英] not getting the data properly from database

查看:98
本文介绍了无法从数据库正确获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


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

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

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.


这篇关于无法从数据库正确获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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