C# 靠近';的语法不正确;

C# 靠近';的语法不正确;,c#,mysql,asp.net,C#,Mysql,Asp.net,我的代码有一个错误 代码执行直接流到catch块并说:error syntax near') 我想在数据库中保存一个文件,然后再次调用它 public partial class newsrv : System.Web.UI.Page{ string dir = "C://fileup//"; protected void Page_Load(object sender, EventArgs e){ if (!Directory.Exists(dir)){

我的代码有一个错误

代码执行直接流到catch块并说:error syntax near')

我想在数据库中保存一个文件,然后再次调用它

public partial class newsrv : System.Web.UI.Page{
    string dir = "C://fileup//";

    protected void Page_Load(object sender, EventArgs e){
        if (!Directory.Exists(dir)){
            Directory.CreateDirectory(dir);
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e){

    }

    protected void Button1_Click(object sender, EventArgs e){
        SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|DB.mdf;Integrated Security=True;User Instance=True");
        string fname = FileUpload1.PostedFile.FileName;
        try{
            SqlCommand cmd = new SqlCommand("INSERT INTO OrderNum (SrviceType, Msg,[File]) VALUES ('" + DropDownList1.SelectedItem.Text + "','" + TextBox1.Text + "' ,'" + FileUpload1.PostedFile.FileName + "') );", con);
            con.Open();

            try {
                int res = cmd.ExecuteNonQuery();
                if (res > 0){
                    System.Windows.Forms.MessageBox.Show("success");
                }
                Label2.Text = TextBox1.Text;
                FileUpload1.SaveAs(dir + fname);
                Label1.Text = " file name uploaded succ ";
                FileUpload1.Visible = true;
            }catch (Exception ex){
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }catch{
            Label1.Text = " file name  not uploaded  ";
            FileUpload1.Visible = false;
            con.Close();
        }finally{
            con.Close();
        }
    }

    protected void TextBox1_TextChanged(object sender, EventArgs e){

    }
}

您的
SQLCommand
无效。应该是:

SqlCommand cmd = new SqlCommand("INSERT INTO OrderNum (SrviceType, Msg,[File]) VALUES ('" + DropDownList1.SelectedItem.Text + "','" + TextBox1.Text + "' ,'" + FileUpload1.PostedFile.FileName + "')", con);

例如,删除额外的

看起来您有一个额外的
在SQL语句的末尾

... + TextBox1.Text + "' ,'" + FileUpload1.PostedFile.FileName + "') );", con);

                                                                     ^^ 
                                                                     remove these

我认为你应该摆脱它;在:

  • “);”

也可以考虑使用更好的安全性。

不要放<代码>;<代码>在查询的末尾。为了便于压缩,请使用以下语法:

SqlCommand cmd=new SqlCommand(String.Format(@"INSERT INTO OrderNum (SrviceType, Msg,[File]) VALUES ('{0},{1},{2}')",DropDownList1.SelectedItem.Text, TextBox1.Text,FileUpload1.PostedFile.FileName),con);
为了更好地理解,请更改obj的名称,使其记住您要对其执行的操作


例如,如果您有一个需要用户名的文本框,请调用文本框
txtUserName
txtNickName

,这是编译器错误或SqlCommand错误?打印命令以调试它:
SqlCommand cmd=new SqlCommand(“插入到OrderNum(SrviceType,Msg,[File])值(”+DropDownList1.SelectedItem.Text+”、“+TextBox1.Text+”、“+FileUpload1.PostedFile.FileName+”);”,con)两个打开的括号和三个结束的括号,用于将来的问题:包括完整的错误消息。堆栈包括导致错误的代码行。