不明确的列名?
Ambigous column name?
好的,所以..我真的找不到问题所以我在这里发帖。 P.S。我是 MSSQL 的新手,所以请保持温柔 xD
我得到的列名不明确 "Name"
我有两个表:Cardiology 和 Cardio_Schedule。我想 select 两者中的一些列并将它们作为结果显示在 gridview 中。我已经查询了..是这样的:
string str = @"
SELECT Cardiology.CID,
Cardiology.Name,
Cardiology.Designation,
Cardiology.Qualification,
Cardiology.Shift,
Cardiology.Appointment_Timings,
Cardio_schedule.Ward_rounds,
Cardio_schedule.Slot1,
Cardio_schedule.Slot2,
Cardio_schedule.Slot3,
Cardio_schedule.Slot4,
Cardio_schedule.BreakTime,
Cardio_schedule.Slot5,
Cardio_schedule.Slot6,
Cardio_schedule.Slot7,
Cardio_schedule.Slot8
FROM Cardiology
CROSS JOIN Cardio_schedule
WHERE Cardiology.Name = Cardio_schedule.Name
AND Name = '" + TextBox1.Text + "'";
cmd = new SqlCommand(str, con);
con.Open();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adp.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
不知道是什么问题。
我确实用 ON 尝试过,但它在 'ON' 附近说语法不正确。任何形式的帮助将不胜感激。
谢谢!
最后你错过了。
SELECT Cardiology.CID,
Cardiology.NAME,
Cardiology.Designation,
Cardiology.Qualification,
Cardiology.Shift,
Cardiology.Appointment_Timings,
Cardio_schedule.Ward_rounds,
Cardio_schedule.Slot1,
Cardio_schedule.Slot2,
Cardio_schedule.Slot3,
Cardio_schedule.Slot4,
Cardio_schedule.BreakTime,
Cardio_schedule.Slot5,
Cardio_schedule.Slot6,
Cardio_schedule.Slot7,
Cardio_schedule.Slot8
FROM Cardiology
CROSS JOIN Cardio_schedule
WHERE Cardiology.NAME = Cardio_schedule.NAME
AND Cardiology.NAME = 'as' -- `Cardiology` alias Missed
您的问题出在过滤器中:-
AND Name = '" + TextBox1.Text + "'";
cmd = new SqlCommand(str, con);
根据您要参考的 table 更改它:-
AND Cardiology.Name = '" + TextBox1.Text + "'";
cmd = new SqlCommand(str, con);
请注意,您的查询是针对 SQL Injection attack 开放的,因此您应该使用参数化查询,而不是像这样:-
string str = "Cardiology.CID,Cardiology.Name, .....WHERE
Cardiology.Name = Cardio_schedule.Name
AND Cardiology.Name = @Name";
cmd = new SqlCommand(str, con);
cmd.Parameters.Add("@Name",DbType.NVarChar,20).Value = TextBox1.Text;
.....and so on
这里请注意,我认为您的数据类型是 NVarchar(20),您需要相应地更改它。
在您的查询结尾应该有:
WHERE Cardiology.Name = Cardio_schedule.Name AND Cardio_schedule.Name = '"
或
WHERE Cardiology.Name = Cardio_schedule.Name AND Cardiology.Name = '"
只写名字属性会产生歧义。
好的,所以..我真的找不到问题所以我在这里发帖。 P.S。我是 MSSQL 的新手,所以请保持温柔 xD
我得到的列名不明确 "Name"
我有两个表:Cardiology 和 Cardio_Schedule。我想 select 两者中的一些列并将它们作为结果显示在 gridview 中。我已经查询了..是这样的:
string str = @"
SELECT Cardiology.CID,
Cardiology.Name,
Cardiology.Designation,
Cardiology.Qualification,
Cardiology.Shift,
Cardiology.Appointment_Timings,
Cardio_schedule.Ward_rounds,
Cardio_schedule.Slot1,
Cardio_schedule.Slot2,
Cardio_schedule.Slot3,
Cardio_schedule.Slot4,
Cardio_schedule.BreakTime,
Cardio_schedule.Slot5,
Cardio_schedule.Slot6,
Cardio_schedule.Slot7,
Cardio_schedule.Slot8
FROM Cardiology
CROSS JOIN Cardio_schedule
WHERE Cardiology.Name = Cardio_schedule.Name
AND Name = '" + TextBox1.Text + "'";
cmd = new SqlCommand(str, con);
con.Open();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adp.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
不知道是什么问题。 我确实用 ON 尝试过,但它在 'ON' 附近说语法不正确。任何形式的帮助将不胜感激。 谢谢!
最后你错过了。
SELECT Cardiology.CID,
Cardiology.NAME,
Cardiology.Designation,
Cardiology.Qualification,
Cardiology.Shift,
Cardiology.Appointment_Timings,
Cardio_schedule.Ward_rounds,
Cardio_schedule.Slot1,
Cardio_schedule.Slot2,
Cardio_schedule.Slot3,
Cardio_schedule.Slot4,
Cardio_schedule.BreakTime,
Cardio_schedule.Slot5,
Cardio_schedule.Slot6,
Cardio_schedule.Slot7,
Cardio_schedule.Slot8
FROM Cardiology
CROSS JOIN Cardio_schedule
WHERE Cardiology.NAME = Cardio_schedule.NAME
AND Cardiology.NAME = 'as' -- `Cardiology` alias Missed
您的问题出在过滤器中:-
AND Name = '" + TextBox1.Text + "'";
cmd = new SqlCommand(str, con);
根据您要参考的 table 更改它:-
AND Cardiology.Name = '" + TextBox1.Text + "'";
cmd = new SqlCommand(str, con);
请注意,您的查询是针对 SQL Injection attack 开放的,因此您应该使用参数化查询,而不是像这样:-
string str = "Cardiology.CID,Cardiology.Name, .....WHERE
Cardiology.Name = Cardio_schedule.Name
AND Cardiology.Name = @Name";
cmd = new SqlCommand(str, con);
cmd.Parameters.Add("@Name",DbType.NVarChar,20).Value = TextBox1.Text;
.....and so on
这里请注意,我认为您的数据类型是 NVarchar(20),您需要相应地更改它。
在您的查询结尾应该有:
WHERE Cardiology.Name = Cardio_schedule.Name AND Cardio_schedule.Name = '"
或
WHERE Cardiology.Name = Cardio_schedule.Name AND Cardiology.Name = '"
只写名字属性会产生歧义。