从vb.net中的存储过程中检索字段 [英] Retrieve fields from stored procedures in vb.net
问题描述
我有一个存储过程Get_Emp_No,它从数据库中检索所有记录。我有一个代码调用上面提到的存储过程。我想在datagrid中只显示几个字段。但我没有得到它。下面是调用过程的存储过程和代码的代码。
另外我得错误 - 对象引用没有设置为对象的实例
------------------------- -----------------------------------------
存储过程
------------------------------------------------- ----------------
< pre lang = SQL > < pre lang = sql > CREATE PROCEDURE Get_Emp_No
AS
SELECT * from emp_master
RETURN 0 < / pre > 跨度>
----------------------------------- -------------------------------
vb code
- -------------------------------------------------- ---------------
尝试
con.Open()
com.Connection = con
com = 新 SqlCommand( Get_Emp_No,con)
' com.CommandText =Get_Emp_No
com.CommandType = CommandType.StoredProcedure
dr = com.ExecuteReader
while dr.Read
i = DGV_empno.Rows.Add(+1)
DGV_empno.Rows(i).Cells( 0 )。Value = dr ( empid)
DGV_empno .Rows(i).Cells( 1 )。值= dr( empname)
DGV_empno.Rows(i).Cells( 2 )。值= dr( dob)
DGV_empno.Rows(i).Cells( 3 )。值= dr( doj)
DGV_empno.Rows (i).Cells( 4 )。值= dr( dept)
DGV_empno.Rows(i).Cells( 5 )。值= dr( desig)
DGV_empno.Rows(i).Cells( 6 ).Value = dr( exp)
DGV_empno.Rows(i )。单元格( 7 )。值= dr( salary )
结束
Catch ex As 异常
MsgBox(ex.Message)
最后
con.Close()
结束 尝试
结束 Sub
尝试
con.Open()
com = 新 SqlCommand( Get_Emp_No,con )
com.CommandType = CommandType.StoredProcedure
dr = com.ExecuteReader
while dr.Read
i = DGV_empno.Rows.Add(+1)
DGV_empno.Rows(i )。细胞( 0 )。值= dr( empid)
DGV_empno.Rows(i).Cells( 1 )。值= dr( empname)
DGV_empno.Rows(i).Cells( 2 )。Value = dr( dob)
DGV_empno.Rows(i)。单元格( 3 )。值= dr( doj )
DGV_empno.Rows(i).Cells( 4 )。值= dr( dept)
DGV_empno.Rows(i).Cells ( 5 )。值= dr( desig)
DGV_empno.Rows(i).Cells( 6 )。值= dr( exp)
DGV_empno.Rows(i).Cells( 7 )。值= dr( salary)
结束 虽然
Catch ex As 异常
MsgBox(ex.Message)
最后
con.Close()
结束 尝试
结束 Sub
在创建之前使用com它:
com.Connection = con
com = 新 SqlCommand( Get_Emp_No,con)
如果你不愿意将SqlConnection作为参数传递给SqlCommand-Constructor,您只需要反转这两行。但是既然你这样做了,你可以删除这两行中的第一行。
它应该工作 - 如果没有其他错误;)
我建议为SqlCommand使用不同的标识符:cmd
- 这样可以更容易区分con
I have a stored procedure "Get_Emp_No" which retrieves all records from database. I have a code that call the stored procedure mentioned above. I want to display only few fields in datagrid. But I am not getting it. Below is the code of stored procedure and code in which the procedure is called.
Also I am getting Error-"Object Reference not set to an instance of object"
------------------------------------------------------------------
stored procedure
-----------------------------------------------------------------
<pre lang="SQL"><pre lang="sql">CREATE PROCEDURE Get_Emp_No
AS
SELECT * from emp_master
RETURN 0</pre>
------------------------------------------------------------------
vb code
------------------------------------------------------------------
Try
con.Open()
com.Connection = con
com = New SqlCommand("Get_Emp_No", con)
' com.CommandText = "Get_Emp_No"
com.CommandType = CommandType.StoredProcedure
dr = com.ExecuteReader
While dr.Read
i = DGV_empno.Rows.Add(+1)
DGV_empno.Rows(i).Cells(0).Value = dr("empid")
DGV_empno.Rows(i).Cells(1).Value = dr("empname")
DGV_empno.Rows(i).Cells(2).Value = dr("dob")
DGV_empno.Rows(i).Cells(3).Value = dr("doj")
DGV_empno.Rows(i).Cells(4).Value = dr("dept")
DGV_empno.Rows(i).Cells(5).Value = dr("desig")
DGV_empno.Rows(i).Cells(6).Value = dr("exp")
DGV_empno.Rows(i).Cells(7).Value = dr("salary")
End While
Catch ex As Exception
MsgBox(ex.Message)
Finally
con.Close()
End Try
End Sub
Try con.Open() com = New SqlCommand("Get_Emp_No", con) com.CommandType = CommandType.StoredProcedure dr = com.ExecuteReader While dr.Read i = DGV_empno.Rows.Add(+1) DGV_empno.Rows(i).Cells(0).Value = dr("empid") DGV_empno.Rows(i).Cells(1).Value = dr("empname") DGV_empno.Rows(i).Cells(2).Value = dr("dob") DGV_empno.Rows(i).Cells(3).Value = dr("doj") DGV_empno.Rows(i).Cells(4).Value = dr("dept") DGV_empno.Rows(i).Cells(5).Value = dr("desig") DGV_empno.Rows(i).Cells(6).Value = dr("exp") DGV_empno.Rows(i).Cells(7).Value = dr("salary") End While Catch ex As Exception MsgBox(ex.Message) Finally con.Close() End Try End Sub
You use "com" before you have created it:
com.Connection = con com = New SqlCommand("Get_Emp_No", con)
If you wouldn't pass the SqlConnection as an argument to the SqlCommand-Constructor, you would simply have to reverse these two lines. But since you do, you can just remove the first of these two lines.
It should work then - if there are no other bugs ;)
I would suggest to use a different identifier for the SqlCommand:cmd
- that way it's easier to distinguish fromcon
.
这篇关于从vb.net中的存储过程中检索字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!