我想调用一个程序,但它什么都不返回,当我调试它时显示为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 0What 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
发布评论