所以基本上,我有2张桌子,租户和业主。我的存储过程是内部连接,以在文本框中显示选择结果。但不幸的是,查询只是从所有者表中提取而不是两者都是。尝试从租户表中显示该列不存在时出现错误。
@ID varchar(4)
SELECT owner_table.ownerID AS ownID, tenant_table.tenantID, owner_table.apt_num AS aptNum, owner_table.first_name AS own_first, owner_table.last_name AS own_last,
owner_table.address AS own_address, owner_table.city AS own_city, owner_table.state AS own_state, owner_table.zip AS own_zip,
owner_table.phone AS own_phone, owner_table.phone_2 AS own_phone2, owner_table.notes AS own_notes, owner_table.last_update AS own_lastUpdate,
tenant_table.ownerID AS ten_ownID, tenant_table.last_name, tenant_table.keyID, tenant_table.storage, tenant_table.phone, tenant_table.phone_2, tenant_table.notes,
tenant_table.complaints, tenant_table.lease_end, tenant_table.last_update, tenant_table.apt_status
FROM owner_table INNER JOIN
tenant_table ON owner_table.ownerID = @ID AND tenant_table.tenantID = @ID
RETURN
代码试图从程序中获得结果。
private void LoadInfo(string ID)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand Command = new SqlCommand();
Command.Connection = connection;
Command.CommandText = "MoreInfoData";
Command.CommandType = CommandType.StoredProcedure;
Command.Parameters.Add(new SqlParameter("@ID", SqlDbType.VarChar, 4)).Value = ID;
table = new DataTable();
SqlDataAdapter MyAdapter = new SqlDataAdapter();
MyAdapter.SelectCommand = Command;
MyAdapter.Fill(table);
if (table.Rows.Count > 0)
{
ten_name.Text = table.Rows[0]["last_name"].ToString();
}
}
答案 0 :(得分:1)
修改以使用列别名:
ten_name.Text = table.Rows[0]["own_last"].ToString();
答案 1 :(得分:1)
您将字段名称更改为own_last,因此它当然不存在。 owner_table.last_name AS own_last
。所以你应该把你的代码改成
ten_name.Text = table.Rows[0]["own_last"].ToString();
答案 2 :(得分:0)
您是否尝试为tenant_table.last_name添加别名?:
SELECT .... tenant_table.last_name AS tenant_last_name
然后:
ten_name.Text = table.Rows[0]["tenant_last_name"].ToString();
答案 3 :(得分:0)
您是否可以运行此命令并查看输出中的内容,应列出要检索的数据表的所有列名称。
if (table.Rows.Count > 0)
{
foreach (System.Data.DataColumn col in table.Columns)
{
System.Diagnostics.Debug.WriteLine(col.ColumnName);
}
}
答案 4 :(得分:0)
总而言之,无论如何我当时保存到我的查询中都不会因为某些原因而保存。毕竟这最终成了问题。感谢您的帮助。