我的代碼有錯誤。錯誤的語法靠近');
代碼執行直接流向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){
}
}
這是編譯器錯誤,或者SqlCommand的錯誤? – OldProgrammer
打印出調試命令:'SqlCommand cmd = new SqlCommand(「INSERT INTO OrderNum(SrviceType,Msg,[File])VALUES(''+ DropDownList1.SelectedItem.Text +'','」+ TextBox1.Text + 「 ''」 + FileUpload1.PostedFile.FileName + 「」));「,CON);' 兩個打開的支架和三對貼近那些 對於未來的問題:包括完整的錯誤消息。堆棧包含導致錯誤的代碼行。 – Daniel