表包含字段,但datareader返回null

编程入门 行业动态 更新时间:2024-10-24 00:17:16
本文介绍了表包含字段,但datareader返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想调用一个程序,但它什么都不返回,当我调试它时显示为null但我的表包含一条记录 以下是我的代码:

受保护 void Button1_ServerClick( object sender,EventArgs e) { using (SqlConnection con = new SqlConnection(conString)) { 使用(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 ); } } } }

这是我的程序:

CREATE PROCEDURE [dbo]。[LoginProcedure] @ student_id as varchar ( 10 ) AS 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 [ ^

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 need Text1.Text instead of Text1.value. Here is a good article on CodeProject: Using SqlParameter Direction in C#.Net with with Stored Procedure[^]

更多推荐

表包含字段,但datareader返回null

本文发布于:2023-05-31 10:06:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/389563.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   datareader   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!