sql语句中的语法不正确 [英] Incorrect syntax in sql statement

查看:106
本文介绍了sql语句中的语法不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试在asp.net中加载一个页面,其中显示了所选锦标赛的详细信息.我已经调试了加载页面期间抛出的异常,但是在Page_Load的任何一行中都没有错误.
错误:Player_Reg
附近的语法不正确 我仅在按钮单击方法中具有带有Player_Reg的SQL语句.当我在SQL Managenent Studio中运行该SQL语句时,它选择了一个团队的成员进行锦标赛注册,这是正确的,但是当我尝试从Visual Studio中运行页面时,会抛出此错误,有人知道为什么吗?

代码:

Hi,
I am trying to load a page in asp.net which displays the detail of the selected Tournament. The exception is thrown during the loading page, I have debuged it, but in no line of Page_Load was an error.
The Error: Incorrect syntax near Player_Reg
I have SQL Statement with Player_Reg only in button click method. This SQL statement which selects players of a team to register to tournament is correct when I run it in SQL Managenent Studio, but when I try to run page from Visual Studio it throws this error, does anybody know why???

Codes:

public class Client
    {
        private String ConnectionString = "Data Source=PETA3NEC_NTBK;Initial Catalog=Beach_Volleyball;Integrated Security=True";
        public SqlConnection Connection { get; set; }
        public SqlCommand Command { get; set; }
        public SqlDataAdapter Adapter { get; set; }
        public SqlDataReader Reader {get; set;}
        //
        public Client()
        {
            Connection = new SqlConnection(this.ConnectionString);
        }
...
}



页面加载和按钮单击:



Page Load and button click:

protected void Page_Load(object sender, EventArgs e)
    {
        Client client = new Client();
        try
        {
            
            client.Connect();
            client.Command = client.Connection.CreateCommand();
            client.Command.CommandText = "select Nazev, Zacatek_Turnaje from Tournament where ID = @id";
            client.Command.Parameters.Add("@id", System.Data.SqlDbType.Int, 10, "ID").Value = Session["Tournament"];
            client.Reader = client.Command.ExecuteReader();
            DateTime date = new DateTime();
            while (client.Reader.Read())
            {
                lblTournamentName.Text = client.Reader.GetString(0);
                date = client.Reader.GetDateTime(1);
            }
            if (Session["Nickname"] == null || DateTime.Now > date)
            {
                Panel3.Visible = false;
            }
            else
            {
                Panel3.Visible = true;
                lblRegister.Visible = false;
            }
            client.Disconnect();
        }
        catch (Exception)
        {
            
           throw;
        }
        DB_Registration_View tmp = new DB_Registration_View();
        GridView3.DataSource = tmp.Select("A");//in this select is no Player_Reg
        GridView3.DataBind();
        GridView4.DataSource = tmp.Select("B");
        GridView4.DataBind();
        GridView5.DataSource = tmp.Select("C");
        GridView5.DataBind();
        GridView6.DataSource = tmp.Select("D");
        GridView6.DataBind();
        GridView7.DataSource = tmp.Select("E");
        GridView7.DataBind();
        GridView8.DataSource = tmp.Select("F");
        GridView8.DataBind();
        if (GridView5.Rows.Count == 0)
        {
            Panel1.Visible = false;
            Panel2.Visible = false;
        }
        else if (GridView7.Rows.Count == 0)
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
        }
        else
        {
            Panel1.Visible = true;
            Panel2.Visible = true;
        }
        

    }
protected void btnRegister_Click(object sender, EventArgs e)
    {
        Client client1 = new Client();
        Client client2 = new Client();
        List<DB_Player> players = new List<DB_Player>();
        try
        {
            client1.Connect();
            client1.Command = client1.Connection.CreateCommand();
            client1.Command.CommandText = "select pla.Nickname from Player pla, Player_Reg plareg where pla.Nickname = plareg.Player_ID and plareg.Team_ID = @id and (plareg.Pozice = 'C' or plareg.Pozice = 'Player') order by plareg.Pozice";
            client1.Command.Parameters.Add("@id", SqlDbType.Int).Value = GridView9.SelectedRow.Cells[1];
            client1.Reader = client1.Command.ExecuteReader();
            while (client1.Reader.Read())
            {
                DB_Player tmp = new DB_Player();
                tmp.Nickname = client1.Reader.GetString(0);
                players.Add(tmp);

            }
            client1.Disconnect();
            client2.Connect();
            client2.Command = client2.Connection.CreateCommand();
            client2.Command.CommandText = "Tournament_Registration";
            client2.Command.CommandType = System.Data.CommandType.StoredProcedure;
            client2.Command.Parameters.Add("@return", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
            client2.Command.Parameters.Add("@Team_ID", SqlDbType.Int).Value = GridView9.SelectedRow.Cells[1];
            client2.Command.Parameters.Add("@Tournament_ID", SqlDbType.Int).Value = Session["Tournament"];
            client2.Command.Parameters.Add("@Paid", SqlDbType.Int).Value = 0;
            client2.Command.Parameters.Add("@Player1_ID", SqlDbType.VarChar).Value = players[0].Nickname;
            client2.Command.Parameters.Add("@Player2_ID", SqlDbType.VarChar).Value = players[1].Nickname;
            client2.Command.ExecuteNonQuery();
            client2.Disconnect();
            int output = (int)client2.Command.Parameters[0].Value;
            if (output == 0)
            {
                lblRegister.Visible = true;
                lblRegister.Text = "Nepodařilo se registrovat do turnaje! Pravděpodobně jeden z hráčů již hraje v turnaji!";
            }
            else
            {
                lblRegister.Visible = true;
                lblRegister.Text = "Úspěšná registrace!";
            }
        }
        catch (Exception)
        {
            
            throw;
        }
        
    }



感谢您的答复!

-Pepin z Hane



Thank you for replies!

-Pepin z Hane

推荐答案

该错误是其中一个Grid View的一个SQL Source的sql语句中...我已经解决了!缺少逗号.
The error was in the sql statement of one SQL Source for one of the Grid Views... I have solved it! There was missing a comma.


您不能在FROM子句中使用多个表名:
You can''t use multiple table names in a FROM clause:
client1.Command.CommandText = "select pla.Nickname from Player pla, Player_Reg plareg where pla.Nickname = plareg.Player_ID and plareg.Team_ID = @id and (plareg.Pozice = 'C' or plareg.Pozice = 'Player') order by plareg.Pozice";

可能您需要连接两个表,或删除第二个表名.

Probably, you need to join the two tables, or remove the second table name.


在btnRegister_Click内部是以下代码....

"
Inside btnRegister_Click is the following code....

"
select pla.Nickname from Player pla, Player_Reg plareg where pla.Nickname = plareg.Player_ID and plareg.Team_ID = @id and (plareg.Pozice = 'C' or plareg.Pozice = 'Player') order by plareg.Pozice



您的Sql语句是完全错误的.据我所知,您来自错误的地方.以及Player_Reg
是需要连接的另一个表,您需要通过左连接或类似的方式将其连接.这是一个联接表.

http://msdn.microsoft.com/en-us/library/ms191517 (v = sql.105).aspx -联接表



Your Sql statement is completely wrong. As far as I can see you''re from is in the wrong place. As well as if Player_Reg
is another Table you need to join you need to join it with a left join or something along those lines. Here is a joining tables.

http://msdn.microsoft.com/en-us/library/ms191517(v=sql.105).aspx - Joining Tables


这篇关于sql语句中的语法不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆