简体   繁体   中英

Incorrect syntax near some keyword in c# / sql

I have the following code snippet :-

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
    string idd = Session["senderefirstname"].ToString();

    Query = " create table '"+ idd +"' (  senderid varchar(90) , recipientid     varchar(90),senderimage varchar(90), senderfirstname varchar(90), senderlastname varchar(90),    message varchar(max) ) ";
    adap = new SqlDataAdapter(Query, con);
    ds = new DataSet();
    adap.Fill(ds); 
    Response.Redirect("newmessage.aspx");
}

but m getting the error is

"Incorrect syntax near 'noah'"

noah is the string stored in session["senderfirstname"] I'm getting this error. Can anyone guide?

You don't need to use single quotes when you use CREATE TABLE .

Just use create table "+ idd +"

Take a look at syntax from CREATE TABLE (Transact-SQL)

But more important, I don't understand your code at all. I don't think you need to use SqlDataAdapter or DataSet because your query does not return anything.

Just execute your query with SqlCommand.ExecuteNonQuery method like;

Query = " create table "+ idd +"(senderid varchar(90), recipientid varchar(90),senderimage varchar(90), senderfirstname varchar(90), senderlastname varchar(90),message varchar(max)) ";
SqlCommand cmd = new SqlCommand(Query, con);
cmd.ExecuteNonQuery();
Query = " create table "+ idd +" (  senderid varchar(90) , recipientid     varchar(90),senderimage varchar(90), senderfirstname varchar(90), senderlastname varchar(90),    message varchar(max) ) ";

我认为这应该有效。

尝试这个..

Query= "create table "+ idd +"(senderid varchar(90),recipientid varchar(90),senderimage varchar(90), senderfirstname varchar(90), senderlastname varchar(90),message varchar(max) ) ";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM