C# 检查播放器是否存在错误

C# 检查播放器是否存在错误,c#,sql-server,database,winforms,C#,Sql Server,Database,Winforms,我有这个windows窗体代码 private void StartGame_Click(object sender, EventArgs e) { if (player.Text == "") { MessageBox.Show("Enter A player to proceed."); } else { //SQL Connection String using (SqlConnection conn = n

我有这个windows窗体代码

private void StartGame_Click(object sender, EventArgs e)
{

    if (player.Text == "")
    {
        MessageBox.Show("Enter A player to proceed.");
    }
    else
    {
    //SQL Connection String
        using (SqlConnection conn = new SqlConnection("Data Source=Keith;Initial Catalog=SoftEngg;Integrated Security=True"))
        {
            conn.Open();

            bool exists = false;

            // create a command to check if the username exists
            using (SqlCommand cmd = new SqlCommand("select * from PlayerData where PlayerName = @player", conn))
            {
                cmd.Parameters.AddWithValue("player", player.Text);
                exists = (int)cmd.ExecuteScalar() > 0;
            }

            // if exists, show a message error
            if (exists)
                MessageBox.Show(player.Text, "is used by another user.");
            else
            {
                // does not exists, so, persist the user
                using (SqlCommand cmd = new SqlCommand("INSERT INTO PlayerData(PlayerName) values (@Playername)", conn))
                {
                    cmd.Parameters.AddWithValue("Playername", player.Text);

                    cmd.ExecuteNonQuery();
                }
            }

            conn.Close();
        }
    }
}
我的目标是提醒玩家并在系统中显示消息框“玩家已经存在”。但我的代码似乎不起作用。当我运行程序时,我在下面的代码中得到一个错误:

exists = (int)cmd.ExecuteScalar() > 0;
错误是:(附加信息:对象引用未设置为对象的实例。)


如何解决此问题,请提供帮助。

您应该使用PlayerData中的
选择计数(*),其中PlayerName=@player
如果您想使用
执行Calar
您应该使用
选择计数(*)从PlayerData,其中PlayerName=@player
如果要使用
ExecuteScalar
您的问题不在查询中。 我的意思是不在这个
select*from PlayerData中,其中PlayerName=@player

由于
exists=(int)cmd.ExecuteScalar()>0,您收到错误

原因: 您正在尝试将输出转换为整数。 因此,当
cmd.ExecuteScalar()
获取
null
值时,您就得到了错误

必须记住

执行查询,并返回查询中第一行的第一列 查询返回的结果集。添加了其他列或行 忽略

您可以使用
select*from PlayerData,其中PlayerName=@player
,但必须确认此表的第一列是
不可为空的

你的支票应该是

 exists = (cmd.ExecuteScalar()!=null)?true:false;
或者,您可以通过选择主键列进行尝试

select your_Primary_Key_Name from PlayerData where PlayerName = @player
检查

 exists = (cmd.ExecuteScalar()!=null)?true:false;


您的问题不在查询中。 我的意思是不在这个
select*from PlayerData中,其中PlayerName=@player

由于
exists=(int)cmd.ExecuteScalar()>0,您收到错误

原因: 您正在尝试将输出转换为整数。 因此,当
cmd.ExecuteScalar()
获取
null
值时,您就得到了错误

必须记住

执行查询,并返回查询中第一行的第一列 查询返回的结果集。添加了其他列或行 忽略

您可以使用
select*from PlayerData,其中PlayerName=@player
,但必须确认此表的第一列是
不可为空的

你的支票应该是

 exists = (cmd.ExecuteScalar()!=null)?true:false;
或者,您可以通过选择主键列进行尝试

select your_Primary_Key_Name from PlayerData where PlayerName = @player
检查

 exists = (cmd.ExecuteScalar()!=null)?true:false;


cmd.Parameters.AddWithValue(“@player”,player.Text)
此外,如果查询未返回任何内容,则无法将其转换为
int
,因此您需要对此进行检查。感谢提醒,但我仍然收到一个错误,即代码exists=(int)cmd.ExecuteScalar()>0<代码>cmd.Parameters.AddWithValue(“@player”,player.Text)
此外,如果查询未返回任何内容,则无法将其转换为
int
,因此您需要对此进行检查。感谢提醒,但我仍然收到一个错误,即代码exists=(int)cmd.ExecuteScalar()>0;我想我们可以只做exists=cmd.ExecuteScalar()=无效@Bryknaval是的,很好,不需要使用条件运算符。我错过了这个。:)谢谢你的建议。我想我们可以只做exists=cmd.ExecuteScalar()=无效@Bryknaval是的,很好,不需要使用条件运算符。我错过了这个。:)谢谢你的建议。