2013-10-19 56 views
2

我的代碼有錯誤。錯誤的語法靠近');

代碼執行直接流向catch塊,並說:incorrect 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){ 

    } 
} 
+0

這是編譯器錯誤,或者SqlCommand的錯誤? – OldProgrammer

+0

打印出調試命令:'SqlCommand cmd = new SqlCommand(「INSERT INTO OrderNum(SrviceType,Msg,[File])VALUES(''+ DropDownList1.SelectedItem.Text +'','」+ TextBox1.Text + 「 ''」 + FileUpload1.PostedFile.FileName + 「」));「,CON);' 兩個打開的支架和三對貼近那些 對於未來的問題:包括完整的錯誤消息。堆棧包含導致錯誤的代碼行。 – Daniel

回答

1

它看起來像你的SQL語句的末尾一個額外的); ...

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

                    ^^ 
                    remove these 
0

SQLCommand無效。它應該是:

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

E.G.在聲明結尾刪除多餘的);

1

我想你應該擺脫的);在:

  • 「」));「

而且,考慮使用placeholders爲更好的安全性。

0

請勿將);置於查詢的末尾。而對於一個簡單的compresion使用此語法:

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的是記得你要這個做什麼名字。

例如,如果你有需要的用戶名調用文本框的文本框txtUserNametxtNickName

相關問題