关键字附近的语法不正确'选择'

时间:2014-07-29 19:35:20

标签: c#

当我尝试在visual studio中使用此代码时出现错误 有人可以帮帮我

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace mywebsite
{
    public partial class myregistration : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(IsPostBack)
            {
                SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Registration1ConnectionString"].ConnectionString);
                Conn.Open();
                string checkuser = " Select count(*) from [userdata] where UserName='" + TextBoxuname.Text + "' ";
                SqlCommand com = new SqlCommand(checkuser, Conn);
                int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
                if (temp == 1)
                {
                    Response.Write(" User Already Exists ");
                }

                Conn.Close();



            }
        }



        protected void Button1_Click1(object sender, EventArgs e)
        {
            try
            {
                SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Registration1ConnectionString"].ConnectionString);
                Conn.Open();
                string insertQuery = "insert into userdata (UserName,Password,Email,select country) values (@Uname ,@Password ,@email ,@Country)";
                SqlCommand com = new SqlCommand(insertQuery, Conn);
                com.Parameters.AddWithValue("@Uname", TextBoxuname.Text);
                com.Parameters.AddWithValue("@Password", TextBoxpass.Text);
                com.Parameters.AddWithValue("@email", TextBoxemail.Text);
                com.Parameters.AddWithValue("@Country", DropDownListselectcountry.SelectedItem.ToString());

                com.ExecuteNonQuery();
                Response.Redirect("manager.aspx");
                Response.Write("Resgistration is Successfull");

                Conn.Close();
            }
            catch(Exception ex)
            {
                Response.Write("Error:" + ex.ToString());
            }

                    }
    }
}

错误:

  

错误:System.Data.SqlClient.SqlException:关键字“select”附近的语法不正确。 System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection)处于系统的System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)的System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection) System.Data.SqlClient上的System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString)中的.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj) System.Data.SqlClient上的System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String方法,DbAsyncResult结果)中的.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async)。 SqlCommand.InternalExecuteNonQuery(DbAsyncResult结果,Str位于enter code here C:\ Users \ Rahul \ Documents \ Visual Studio 2008中的mywebsite.myregistration.Button1_Click1(Object sender,EventArgs e)的System.Data.SqlClient.SqlCommand.ExecuteNonQuery()中的methodName,Boolean sendToPipe) Projects \ Project1 \ mywebsite \ mywebsite \ myregistration.aspx.cs:第50行

它在第50行显示错误,即com.ExecuteNonQuery(); 如果我从代码中删除它,记录没有插入数据库,所以请建议我必须对代码做什么。

4 个答案:

答案 0 :(得分:4)

string insertQuery = "insert into userdata (UserName,Password,Email,select country) values (@Uname ,@Password ,@email ,@Country)";

我认为您需要删除“选择”一词

答案 1 :(得分:2)

试试这个:

string checkuser = "Select count(*) from [userdata] where UserName='" + TextBoxuname.Text + "' ";

即删除select

之前的空格

也试试这个:

string insertQuery = "insert into userdata (UserName,Password,Email,select country) values (@Uname ,@Password ,@email ,@Country)";

旁注:

不要使用字符串连接,而是尝试使用paramerterize query

答案 2 :(得分:1)

这是因为select是关键字,您的列名称中有空格。改为编写此查询:

string insertQuery = "insert into userdata (UserName,Password,Email, [select country]) values (@Uname ,@Password ,@email ,@Country)";

答案 3 :(得分:0)

我不会像这样使用内联SQL。糟糕的做法,并开放SQL注入。 见SQL Injection

你想用 Parameterized Query

请使用类似的东西

using (SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Registration1ConnectionString"].ConnectionString))
{
    Conn.Open();
    using (SqlCommand cmd = Conn.CreateCommand())
    {
        cmd.CommandText = "select count(*) from [userdata] where UserName=@username";
        cmd.Parameters.AddWithValue(TextBoxuname.Text);
        int temp = (int)cmd.ExecuteScalar();
        if (temp.Equals(1))
        {
            Response.Write(" User Already Exists ");
        }
    }
}