SqlCeDataReader参数ordinal = 1

时间:2011-05-28 03:06:20

标签: c# .net database sql-server-ce

此代码上的参数ordial = 1错误。 任何人都可以在这种情况下解释它? dbCon是正确的,因为我可以将数据插入到数据库中,只是尝试如何将其返回no。

              if (dbCon.State == ConnectionState.Closed)
            {
                dbCon.Open(); ;
            }

            SqlCeParameter vetidfromdropbox = new SqlCeParameter("@vetidfromdropbox", SqlDbType.Int);
            vetidfromdropbox.Value = 2;

            SqlCeCommand mySQLCommand = new SqlCeCommand("SELECT * FROM vets WHERE vetID = @vetidfromdropbox", dbCon);

            SqlCeDataReader rdata = mySQLCommand.ExecuteReader();


            if(rdata.Read()){

                    editNameTextbox.Text = (String)rdata["vetName"];
                    editSurnameTextbox.Text = (String)rdata["vetSurname"];
                    editCompanyNameTextbox.Text = (String)rdata["vetCompanyName"];
                    editPractiseAddTextBox.Text = (String)rdata["vetPractiseAddress"];
                    editMobileTextbox.Text = (String)rdata["vetMobile"];
                    editOtherTextbox.Text = (String)rdata["vetOther"];
                    editNotesTextbox.Text = (String)rdata["vetNotes"];

                }else{
                        MessageBox.Show(" There has been an error with Read() ");                               
                }



            if (dbCon.State == ConnectionState.Open)
            {
                dbCon.Close();
            }

1 个答案:

答案 0 :(得分:1)

您没有在SQL命令中添加参数:

var vetidfromdropbox = new SqlCeParameter("@vetidfromdropbox", SqlDbType.Int);
vetidfromdropbox.Value = 2;

var mySQLCommand = new SqlCeCommand(
    "SELECT * FROM vets WHERE vetID = @vetidfromdropbox", dbCon);
mySQLCommand.Parameters.Add(vetidfromdropbox);

您可以使用AddWithValue来简化语法:

var mySQLCommand = new SqlCommand(
    "SELECT * FROM vets WHERE vetID = @vetidfromdropbox", dbCon);
mySQLCommand.Parameters.AddWithValue("vetidfromdropbox", 2);

附注:在sql连接和命令上使用using以保证处理并简化代码。