asp.net(C#) cmd.ExecuteNonQuery(); '(' 附近有语法错误。

login.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;**

public class db
{
public static string connstring = "data source=localhost; Initial Catalog=user_login; integrated security=SSPI";
public static void Dosql(string sql)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = connstring;
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
conn.Close();
}
}

public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
    string user_name, user_pw, real_name, title, school, department, phone_number, email;
    user_name = UserText.Text.Replace("'", "''");
    user_pw = PassText.Text.Replace("'", "''");
    real_name = NameText.Text.Replace("'", "''");
    title = TitleText.Text.Replace("'", "''");
    school = SchoolText.Text.Replace("'", "''");
    department = DepartmentText.Text.Replace("'", "''");
    phone_number = PhoneText.Text.Replace("'", "''");
    email = EmailText.Text.Replace("'", "''");

    string sql = "insert into user_info (user_name,user_pw,real_name,title,school,department,phone_number,email)values]('" + user_name + "','" + user_pw + "','" + real_name + "','" + title + "','" + school + "','" + department + "','" + phone_number + "','" + email + "')";
    db.Dosql(sql);

    Response.Write("<script language=javascript>alert('注册成功,请稍后登陆!');</script>");
}

private void CheckInput()
{
    throw new NotImplementedException();
}
protected void UserText_TextChanged(object sender, EventArgs e)
{

}

}


<?xml version="1.0"?>






providerName="System.Data.SqlClient" />

求大神指导

4个回答

图片说明

fyb1261369646
fyb1261369646 回复caozhy: 您好,我将“]”符号删去了,但还是不行
2 年多之前 回复
fyb1261369646
fyb1261369646 您好,我将“]”符号删去了,但还是不行
2 年多之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 正解
2 年多之前 回复

您好,我将“]”符号删去了,但还是不行

如果存在中文的话在每个属性值的单引号前加一个N

fyb1261369646
fyb1261369646 第一个user_name有效,从第二个开始无效了
2 年多之前 回复
fyb1261369646
fyb1261369646 string sql = "insert into user_info (user_name,user_pw,real_name,title,school,department,phone_number,email)values('"+user_name+"N','"+user_pw+"N','"+real_name+"N','"+title+"N','"+school+"N','"+department+"N','"+phone_number+"N','"+email+"N')";是这样吗?还是不可以,我感觉有可能是我这句sql语句有问题,我把这句sql语句放在sqlserver里执行,显示列名无效
2 年多之前 回复

已解决,sql语句中user_name是保留字,把它用中括号括起来即可

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐