使用SqldataReader时出错 [英] Error using SqldataReader

查看:163
本文介绍了使用SqldataReader时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我试图使用SqldataReader从我的表中读取所有列并在表单的文本框中显示它们,但它给出了一个错误,说明没有对象引用

dr(SqldataReader)低于公共类frmRegCoop



Hi im trying to use SqldataReader to read all the columns from my table and show them in the textbox of the form, but it gives me an error saying that there no object reference
the dr (SqldataReader) is below Public Class frmRegCoop

selectCommand.Connection = conn
  selectCommand.CommandText = "SELECT * FROM [tblEjemplo] WHERE COOP_ID= @COOP_ID"

  selectCommand.CommandType = CommandType.Text
  selectCommand.Parameters.Add("@COOP_ID", SqlDbType.Int).Value = txtIdCoop.Text

  While dr.Read ()
      Try
          conn.Open()

          selectCommand.ExecuteReader()
          txtNombreCoop.Text = dr("@COOP_NOMBRE").ToString()
          EstatusRecordRCoop.Text = "Record Encontrado"

      Catch ex As SqlException
          EstatusRecordRCoop.Text = "Error"
          MessageBox.Show(ex.Message)
      Finally
          conn.Close()
      End Try

  End While



谢谢


Thanks

推荐答案

你还没有执行命令,因为读者没有被初始化。尝试:

You haven''t executed the command, os the chancesa re that the reader is not initialized. Try:
selectCommand.Parameters.Add("@COOP_ID", SqlDbType.Int).Value = txtIdCoop.Text
dr = selectCommand.ExecuteReader()
While dr.Read ()





我修复了它,但现在没有@我易受sqlInjection攻击?还有新错误我get是在我第二次执行搜索,插入或更新之后,它告诉我变量名''@ COOP_ID''已经被声明。变量名在查询批处理或存储过程中必须是唯一的。



将您的代码更改为:



"I fix it but they are now without the @ am I vulnerable to sqlInjection? also the new error I get is after searchin,inserting or updating one time after I do it for second time it tells me The variable name ''@COOP_ID'' has already been declared. Variable names must be unique within a query batch or stored procedure."

Change your code to like this:

selectCommand.CommandType = CommandType.Text
selectCommand.Parameters.Add("@COOP_ID", SqlDbType.Int).Value = txtIdCoop.Text
conn.Open()

selectCommand.ExecuteReader()

    Try
        While dr.Read ()
            txtNombreCoop.Text = dr("@COOP_NOMBRE").ToString()
            EstatusRecordRCoop.Text = "Record Encontrado"
        End While
    Catch ex As SqlException
        EstatusRecordRCoop.Text = "Error"
        MessageBox.Show(ex.Message)
    Finally
        conn.Close()
    End Try



您可能还想让textbox.Text设置添加文本而不是每次都覆盖它。

你你发现你不想在dr访问中使用@ - 除非你有一个名为@ COOP_NOMBRE的列你不应该需要它。 (这也是一种很好的做法,在SELECT语句中明确地找到你希望返回的列,而不是使用SELECT * FROM ...,因为后者会浪费带宽和时间)。


You may also want to make the the textbox.Text set add the text rather than overwrite it each time.
You may find you don''t want the "@" on the dr access - unless you have a column named @COOP_NOMBRE you shouldn''t need it. (It''s also good practice to explicitly nam eteh columns you want returned in teh SELECT statement rather than using "SELECT * FROM..." as the latter wastes bandwidth and time).


这篇关于使用SqldataReader时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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