2017-09-02 38 views
-1
SqlConnection con=new SqlConnection("Data Source=PRAVESH-PC\\SQLEXPRESS;Initial Catalog=shopping;Integrated Security=True"); 

    string a, b; 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    protected void b1_Click(object sender, EventArgs e) 
    { 

     a = Class1.GetRandomPassword(10).ToString(); 
     f1.SaveAs(Request.PhysicalApplicationPath+"./images/" + a + f1.FileName.ToString()); 
     b="images/" + a + f1.FileName.ToString(); 
     con.Open(); 
     SqlCommand cmd = con.CreateCommand(); 
     cmd.CommandType = CommandType.Text; 
     cmd.CommandText = "insert into product values('"+t1.Text+"','"+t2.Text+"','"+t3.Text+"','"+t4.Text+",'"+b.ToString()+"')"; 
     cmd.ExecuteNonQuery(); ///// Error Occured.. 
     con.Close(); 
    } 
} 

,它提供了錯誤:當我在我的項目使用此下面的代碼是當我在我的項目使用此代碼提供了錯誤

Error Details: System.Data.SqlClient.SqlException: Incorrect syntax near 'images'. Unclosed quotation mark after the character string ')'.

任何人都可以請幫我的。

感謝

回答

1

您在這裏缺少一個單引號:'"+t4.Text+"

所有其他參數正確包裹,但是這對缺少它的最後報價。儘管如此,整個字符串連接仍然很難看。我建議做別的東西,也許使用string.Format,沿着線:

string.Format("insert into product values('{0}', '{1}', ...)", t1.Text, t2.Text, etc etc); 

更重要的是,使用SQL PARAMATERS

相關問題