ado.net XO游戏

时间:2014-01-22 07:21:54

标签: c# ado.net

我写了一个插入玩家名字的代码,现在在游戏结束时我想将得分更新为+1给赢家(在奖金栏下)和+1给失败者(在损失列下)

我每次都会收到错误:'('。

附近的语法不正确

首先插入代码:             int readtable = 0;

        SqlConnection con = new SqlConnection
        (@"Data Source=.\SQLExpress;Initial Catalog=ExampleDB;Integrated Security=True");
        con.Open();

        SqlCommand tryfindname = new SqlCommand();
        tryfindname.Connection = con;
        tryfindname.CommandType = CommandType.Text;

        tryfindname.CommandText = "select * from XOhalloffame where PlayerName='@input1'";
        tryfindname.Parameters.AddWithValue("@input1", player1.getname());

        SqlDataReader readingtime = tryfindname.ExecuteReader();
        while (readingtime.Read())
        {
            readtable = 1;
        }
        con.Close();

        if (readtable == 0)
        {
            con.Open();
            SqlCommand pl1db = new SqlCommand();
            pl1db.Connection = con;
            pl1db.CommandType = CommandType.Text;

            pl1db.CommandText = "insert into XOhalloffame (PlayerName ,Winnings, Losses) values (@name1, @win1, @lose1)";
            pl1db.Parameters.AddWithValue("@name1",player1.getname());
            pl1db.Parameters.AddWithValue("@win1", 0);
            pl1db.Parameters.AddWithValue("@lose1", 0);
            pl1db.ExecuteNonQuery();

            SqlCommand pl2db = new SqlCommand();
            pl2db.Connection = con;
            pl1db.CommandType = CommandType.Text;

            pl2db.CommandText = "insert into XOhalloffame (PlayerName ,Winnings, Losses) values (@name2, @win2, @lose2)";
            pl2db.Parameters.AddWithValue("@name2",player2.getname());
            pl2db.Parameters.AddWithValue("@win2", 0);
            pl2db.Parameters.AddWithValue("@lose2", 0);
            pl2db.ExecuteNonQuery();
            con.Close();

有问题的代码:

    public void whowon()
    {
        if (turn == 1)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
            "success", "setInterval(2000);alert('Player X wins !!!');", true);



            SqlConnection con = new SqlConnection
            (@"Data Source=.\SQLExpress;Initial Catalog=ExampleDB;Integrated Security=True");
            con.Open();

            SqlCommand wincmd = new SqlCommand();
            wincmd.Connection = con;
            wincmd.CommandType = CommandType.Text;

            wincmd.CommandText = "update XOhalloffame set (Winnings='@win1'+1,) where (PlayerName='"+player1.getname()+"')";
            wincmd.Parameters.AddWithValue("@win1",+1);
            wincmd.ExecuteNonQuery();

1 个答案:

答案 0 :(得分:0)

根据我的说法,您应该为玩家详细信息和游戏玩法提供不同的表格。 假设一个表是Player_Master,其中包含player_id(PK),player_name和第二个表Say Game_Master包含player1_id(FK),player2_id(FK),winner_player_id,其中每次玩新游戏时都会插入数据。 您可以根据用途修改表模式。