2013-04-10 47 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(); 
} 

     } 
    }