表包含字段,但datareader返回null [英] Table contain field but datareader returns null
问题描述
我想调用一个程序,但它什么都不返回,当我调试它时显示为null但我的表包含一条记录
以下是我的代码:
受保护 void Button1_ServerClick( object sender,EventArgs e)
{
using (SqlConnection con = new SqlConnection(conString))
{
使用(SqlCommand cmd = < span class =code-keyword> new SqlCommand( LoginProcedure,con) )
{
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter( @ student_id,Text1.Value);
cmd.Parameters.Add(p1);
con.Open();
cmd.ExecuteNonQuery();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Label2.Text = dr.GetString( 0 跨度>);
}
}
}
}
这是我的程序:
CREATE PROCEDURE [dbo]。[LoginProcedure]
@ student_id as varchar ( 10 )
AS
< span class =code-keyword> SELECT student_id,dob 来自个人其中 student_id = @student_id
RETURN 0
我尝试了什么:
我尝试了很多不同的东西,我使用了文本框和按钮作为HTML使用runat = server属性控制它们是HTML控件,我也尝试使用查询更改我的过程,但仍返回no事情
试试这个:
SqlParameter p1 = 新 SqlParameter( @ student_id,SqlDbType.VarChar,Text1。 value , student_id);
p1.IsNullable = true ;
// p1.Direction = ParameterDirection.Input; //不需要,默认。
也许你还需要Text1.Text
而不是Text1.value
。
这是一篇关于CodeProject的好文章:
在C#.Net中使用SqlParameter Direction with with Stored Procedure [< a href =http://www.codeproject.com/Tips/828514/Using-SqlParameter-Direction-in-Csharp-Net-with-witarget =_ blanktitle =New Window> ^
I want to call a procedure but it returns nothing and when I debug it displays null but my table contain a record
below is my code :
protected void Button1_ServerClick(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("LoginProcedure", con))
{
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("@student_id", Text1.Value);
cmd.Parameters.Add(p1);
con.Open();
cmd.ExecuteNonQuery();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Label2.Text = dr.GetString(0);
}
}
}
}
this is my procedure :
CREATE PROCEDURE [dbo].[LoginProcedure]
@student_id as varchar(10)
AS
SELECT student_id,dob from Personal where student_id=@student_id
RETURN 0
What I have tried:
I tried many different things and I used textbox and button as HTML controls they are HTML controls with runat=server property true and I also try to change my procedure with query but still it returns nothing
Try this:
SqlParameter p1 = new SqlParameter("@student_id", SqlDbType.VarChar, Text1.value, "student_id"); p1.IsNullable = true; // p1.Direction = ParameterDirection.Input; // Not needed, default.
Maybe you also needText1.Text
instead ofText1.value
.
Here is a good article on CodeProject:
Using SqlParameter Direction in C#.Net with with Stored Procedure[^]
这篇关于表包含字段,但datareader返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!