2013-04-10 64 views
0

我插入圖片路徑到數據庫中,當我得到的路徑我用img標籤顯示它們。插入命令爲asp.net

我可以做到這一點,但我找不到在sql查詢後寫入的方法。

我在sql查詢後寫什麼?

Page_Load我的選擇命令起作用。

c = new Common(ConfigurationManager.ConnectionStrings["ahapp"]); 
    string sql = "select Query"; 
    string str = ""; 
    DataTable dt = c.GetDataTable(sql); 
    foreach (DataRow item in dt.Rows) 
    { 
     str += "<img src='" + item["path"].ToString() + "' style='width:100px' />"; 
    } 

    dokList.InnerHtml = str; 

該代碼總是說: enter image description here

sql ="INSERT INTO DR_OZLUK VALUES(3," + ddlDoktor.SelectedValue + "," + belgeid + ",3," + str + ",1)"; 
    SqlCommand cmd = new SqlCommand(sql, c); 
    cmd.ExecuteNonQuery(); 

回答

0

謝謝大家。我找到了解決方案。我添加了這個代碼c.GetExecuteNonQuery(sql);並使c全局。

2

c對象似乎比的SqlConnection類型其他別的東西。那個普通班是什麼? SqlCommand需要兩個參數。第一個是字符串,它是sql語句或存儲過程的名稱,另一個參數是SqlConnection類型的對象。

2

插入語句易受sql注入的影響,但問題與sql語句無關。問題是你正在通過Common類而不是Connection對象在SqlCommand異議。

試試這個代碼片段:

string connStr = "connection string here"; 
string sqlStatement = "INSERT INTO DR_OZLUK VALUES (3, @val1, @val2, 3, @val3, 1)"; 
using (SqlConnection conn = new SqlConnection(connStr)) 
{ 
    using(SqlCommand comm = new SqlCommand()) 
    { 
     comm.Connection = conn; 
     comm.CommandText = sqlStatement; 
     comm.CommandType = CommandType.Text; 

     comm.Parameters.AddWithValue("@val1", ddlDoktor.SelectedValue); 
     comm.Parameters.AddWithValue("@val2", belgeid); 
     comm.Parameters.AddWithValue("@val3", str); 

     try 
     { 
      conn.Open(); 
      comm.ExecuteNonQuery(); 
     } 
     catch(SqlException e) 
     { 
      // do something with the exception 
      // do not hide it 
      // e.Message.ToString() 
     } 
    } 
} 

對於正確的編碼

  • 使用using語句propr對象處理
  • 使用try-catch塊妥善處理對象
0

我想c應該是連接對象

c = new Common 

您正在傳遞c連接對象的位置。

簽名的SqlCommand的創建目標是

public SqlCommand(
    string cmdText, 
    SqlConnection connection 
) 

所以要根據您的這第二個參數必須是連接對象。這就是它給你一個錯誤的原因。您正在傳遞Common類對象以創建命令對象。 visi MSDN鏈接以獲取更多信息:http://msdn.microsoft.com/en-us/library/877h0y3a.aspx

0

您的c不是SqlConnection或者您的IDE有另一個與您粘貼的錯誤混淆的錯誤。有時VS2010在顯示錯誤時會中斷。

0
Or try this, this sample code is very secure and 100% works any time, you can copy paste it but u must put your connection string and table 

    string connectionStr = "connection string STRING"; 
    string sqlQuery = "INSERT INTO yourtable VALUES (ID, @pam1, @pam2)"; 
    ///// Go to Base and execute Query but with trycatch because if you have problem with using connection it will tell you 

    try{ 

    using (SqlConnection conn = new SqlConnection(connStr)) 
    { 
     using(SqlCommand komand = new SqlCommand(conn,sqlQuery)) //command to use connection and Sqlquery 

    string fromcontrol = "test"; //this is string just for test 

      komand.Parameters.AddWithValue("@pam1", fromcontrol.ToString()); 
      komand.Parameters.AddWithValue("@val2", fromcontrol.ToString()); 

       conn.Open(); 
       komand.ExecuteNonQuery(); 
      } 
      catch(SqlException e) 
      { 
    //throw your execption to javascript or response; e.Message.ToString() + "SQL connection or query error"; 
      } 

    catch(Exception ex) 
    { 
    //throw your execption to javascript or response; e.Message.ToString()+ "system error"; 
    } 

finally 
{ 
conn.Close(); 
conn.Dispose(); 
} 

     } 
    }