我想要做的是在数据库中获取特定的列。目前,我有ID,用户名,姓氏,名字,电子邮件,CreditRequest和联系人。我希望信用请求列显示在我的asp文本框中。您可以分享任何技巧吗?
我在想这样的事情:
using (SqlConnection scn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True"))
{
scn.Open();
SqlCommand cmd = new SqlCommand("SELECT CreditRequest FROM CreditRequests WHERE Username=@Username", scn);
您需要通过对执行命令的方法的适当调用来完成代码,并获取结果。我想该查询仅返回一条记录,因此调用ExecuteScalar非常有效。
using (SqlConnection scn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True"))
{
scn.Open();
SqlCommand cmd = new SqlCommand("SELECT CreditRequest FROM CreditRequests WHERE Username=@Username", scn);
// Add the parameter required by the query....
cmd.Parameters.Add("@Username", SqlDbType.NVarChar).Value = variableWithYouUserNameValue;
// Run the query and get the single row/column returned
object value = cmd.ExecuteScalar();
// If it is not null (possible if the where fails) then put on textbox
if(value != null)
txtBoxCredit.Text = value.ToString();
}
当然,我不知道您的文本框的真实名称,因此我选择了一个虚构的名称,将其更改为您的文本框的真实名称。包含要搜索的当前用户名的变量也是如此。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句