INSERT语句中的列少于VALUES子句中指定的值...但是没有! [英] Fewer columns in INSERT statement than values specified in VALUES clause ... but there isn't !
问题描述
我在这里感到困惑,我一生都无法明白为什么收到此错误消息.
我有2张桌子:
联盟(带有2列),联盟ID (Int,PK和IsIdentity是)和联赛名称(varchar16)
Teams (具有3列), Team ID (Int,PK和IsIdentity是), TeamName (varchar16)和 LeagueID (Int,FK链接到联赛表的PK)
插入到联赛表中效果很好...
I''m baffled here, I can''t for the life of me see why I am getting this error message.
I have 2 Tables:
Leagues, with 2 Columns, League ID (Int, PK & IsIdentity Yes) & LeagueName (varchar16)
Teams, with 3 Columns, Team ID (Int, PK & IsIdentity Yes), TeamName (varchar16) & LeagueID (Int, FK linked to PK of Leagues Table)
Inserting into the Leagues Table works perfectly ...
void myDB_Insert_League(string myString)
{
using (SqlConnection myDB = new SqlConnection(myDBlocation))
using (SqlCommand mySqlCmd = myDB.CreateCommand())
{
try
{
mySqlCmd.CommandText = "INSERT INTO Leagues(LeagueName) VALUES(@myLeagueName); SELECT SCOPE_IDENTITY();";
mySqlCmd.Parameters.AddWithValue("@myLeagueName", myString);
myDB.Open();
myLeagueID = int.Parse(mySqlCmd.ExecuteScalar().ToString());
return;
}
catch (SqlException myExc)
{
if (myExc.Message.Contains("Violation of UNIQUE KEY constraint"))
{
MessageBox.Show("ERROR - League Name Is Already In Use");
return;
}
}
finally
{
myDB.Close();
}
}
}
...但是插入到Teams表失败,并出现错误,但是我做的与联赛插入代码相同,并且包括了除PK之外的所有字段...
[代码]
... but Inserting into the Teams Table fails with the error, yet I have done the same as in the Leagues Insert code and have included all fields except the PK ...
[code]
void myDB_Insert_Team(string myTeam, int myLeague)
{
using (SqlConnection myDB = new SqlConnection(myDBlocation))
using (SqlCommand mySqlCmd = myDB.CreateCommand())
{
try
{
mySqlCmd.CommandText = "INSERT INTO Teams(TeamName) VALUES(@myTeamName, @myLeagueID)";
mySqlCmd.Parameters.AddWithValue("@myTeamName", myTeam);
mySqlCmd.Parameters.AddWithValue("@myLeagueID", myLeague);
myDB.Open();
mySqlCmd.ExecuteNonQuery();
return;
}
catch (SqlException myExc)
{
MessageBox.Show("ERROR - SQL Error Occurred During Insert - " + myExc);
return;
}
finally
{
myDB.Close();
}
}
}
[/code]
...为什么我会收到此错误?!?
[/code]
... why am I getting this error ?!?
推荐答案
mySqlCmd.CommandText ="INSERT INTO Teams(TeamName ,LeagueID )值(@myTeamName,@myLeagueID);
mySqlCmd.CommandText = "INSERT INTO Teams(TeamName, LeagueID) VALUES(@myTeamName, @myLeagueID)";
这篇关于INSERT语句中的列少于VALUES子句中指定的值...但是没有!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!