这个代码是将数据检索到文本框的核心 [英] is this code is corerct to retrive the data into text box
本文介绍了这个代码是将数据检索到文本框的核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public string user(AddInfoModel info)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Resource_sp", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
FirstName.Text = reader["FirstName"].ToString();
LastName.Text = reader["LastName"].ToString();
reader.Close();
con.Close();
}
}
}
}
推荐答案
如果你得到多行,那么你需要注意你正在重写文本框
。这不正确,因为只显示最后一行中的值。使用Datagrid
或列表框
而不是文本框。
If you are getting multiple rows, then you need to note that you are rewriting into thetextbox
. This is not correct as only the values in the last row would be displayed. Use aDatagrid
or aListbox
instead of a textbox.
使用阅读器.close()和con.close()在if条件之外
公共字符串用户(AddInfoModel info)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [MyConsString]。ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand( Resource_sp,con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader reader = cmd.ExecuteReader();
if( reader.Read())
{
FirstName.Text = reader [FirstName]。ToString();
LastName.Text = reader [LastName]。ToString();
}
reader.Close();
con.Close();
}
}
}
use reader.close() and con.close() outside the if condition
public string user(AddInfoModel info)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Resource_sp", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
FirstName.Text = reader["FirstName"].ToString();
LastName.Text = reader["LastName"].ToString();
}
reader.Close();
con.Close();
}
}
}
这篇关于这个代码是将数据检索到文本框的核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文