【发布时间】:2009-08-20 16:07:45
【问题描述】:
我在 Select 语句中加入两个表 - Contact 和 RetailTrainingUserLevelMap。
(两者的共同列是 RetailTrainingUserLevelID int)
选择 Contact.IntranetUserName、Contact.CompanyName、RetailTrainingUserLevelMap.RetailTrainingUserLevel
来自联系人
INNER JOIN RetailTrainingUserLevelMap ON Contact.RetailTrainingUserLevelID = RetailTrainingUserLevelMap.RetailTrainingUserLevelID
AND Contact.RetailTrainingUserLevelID = RetailTrainingUserLevelMap.RetailTrainingUserLevelID
WHERE (Contact.IntranetUserName = @IntranetUserName)
如果我通过 Visual Studio 查询生成器(测试查询窗口)运行此语句,并为“IntranetUserName”输入一个值,我会得到:
内网用户名:
约翰·乔
公司名称:
Acme Inc.
RetailTrainingUserLevel:
经理
这是我想要的输出,到目前为止一切顺利。
如果我在我的 .cs 代码隐藏中使用相同的选择语句,使用 SqlDataReader 将标签绑定到其中一些列,如下所示:
SqlCommand comm;
SqlConnection conn;
string intranetConnectionString = ConfigurationManager.ConnectionStrings["DataConnect"].ConnectionString;
conn = new SqlConnection(intranetConnectionString);
comm = new SqlCommand("SELECT Contact.IntranetUserName, Contact.CompanyName, RetailTrainingUserLevelMap.RetailTrainingUserLevel FROM Contact INNER JOIN RetailTrainingUserLevelMap ON Contact.RetailTrainingUserLevelID = RetailTrainingUserLevelMap.RetailTrainingUserLevelID AND Contact.RetailTrainingUserLevelID = RetailTrainingUserLevelMap.RetailTrainingUserLevelID WHERE (Contact.IntranetUserName = @IntranetUserName)", conn);
comm.Parameters.Add("@IntranetUserName", System.Data.SqlDbType.VarChar, 50);
comm.Parameters["@IntranetUserName"].Value = memberLoginName;
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
memberCompanyNameLabel.Text += reader["CompanyName"];
userLevelLabel.Text += reader["RetailTrainingUserLevel"];
}
reader.Close();
conn.Close();
我收到错误“输入字符串的格式不正确。”这里:
userLevelLabel.Text += reader["RetailTrainingUserLevel"];
这里需要更改什么 C# 语法,以便我可以将该值正确绑定到我的 userLevelLabel?
注意:
RetailTrainingUserLevelID 整数
RetailTrainingUserLevel varchar (50)
感谢您的时间和知识。
【问题讨论】:
-
道格,12 年后你有没有想过这个问题?...
标签: c# label inner-join string-formatting