Jquery 如何将图像放入SQLServer2005

Jquery 如何将图像放入SQLServer2005,jquery,html,sql-server-2005,asmx,Jquery,Html,Sql Server 2005,Asmx,我已经编写了以下代码来将映像路径放置到SQLServer2005中,但它不起作用,因为它们没有任何其他方法可以从客户端应用程序将映像放置到SQLServer中 example.html <form id="addresslistingform"> <fieldset id="fieldset1"><legend>Address for listing</legend> Zipcod

我已经编写了以下代码来将映像路径放置到SQLServer2005中,但它不起作用,因为它们没有任何其他方法可以从客户端应用程序将映像放置到SQLServer中

example.html

<form id="addresslistingform">
                  <fieldset id="fieldset1"><legend>Address for listing</legend>
                Zipcode:<br/>
                  <input size="30" type="text" id="zipcode"/><br/>
                Street No:<br/>
                  <input size="30" type="text" id="addstreetno" class="number" name="streetno"/><br/>
                Street Name:<br/> 
                  <input size="30" type="text" id="addstreetname" class="required" name="streetname"/><br/>                            
                Upload a couple of pictures:<br>
              <input size="30" type="file" id="addpicture"/> <br>                 
              </fieldset>

             <input id="Addresslisting" type="image" src="images/Submit.png" align="left"  />                   
 </form>
asmx Web服务文件

[WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public bool addresslisting(string zipcode, string streetnumber, string streetname,  string Imagelocation)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "";
        con.Open();

        SqlCommand sqlcom = new SqlCommand();//declaring a new command
        sqlcom.CommandText = "insert into Address_Listing(Zip_Code,Street_Number,Street_Name,Image_Location) values ('" + zipcode + "','" + streetnumber + "','" + streetname + "',  '" + Imagelocation + "')"; //query for inserting data into contact table
        sqlcom.Connection = con;//connecting to database

        try
        {
            int success = sqlcom.ExecuteNonQuery();
            con.Close();

            if (success > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception e)
        {
            con.Close();
            return false;
        }


    }

仅供参考,您的代码存在许多问题。应该是这样的:

public bool addresslisting(string zipcode, string streetnumber, string streetname, string Imagelocation)
{
    using (SqlConnection con = new SqlConnection())
    {
        con.ConnectionString = "";
        con.Open();

        using (SqlCommand sqlcom = new SqlCommand())
        {
            sqlcom.CommandText =
                string.Format(
                    "insert into Address_Listing(Zip_Code,Street_Number,Street_Name,Image_Location) " +
                    "values ('{0}','{1}','{2}',  '{3}')",
                    zipcode, streetnumber, streetname, Imagelocation);
            sqlcom.Connection = con;

            int success = sqlcom.ExecuteNonQuery();

            return success > 0;
        }
    }
}
  • 使用
    块将确保释放资源,即使引发异常也是如此
  • 永远不要隐藏例外。通过返回“false”,可以隐藏引发异常的任何问题
  • 我当然希望您已经清理了zipcode、streetnumber等,否则您的代码可能容易受到SQL注入攻击
  • public bool addresslisting(string zipcode, string streetnumber, string streetname, string Imagelocation)
    {
        using (SqlConnection con = new SqlConnection())
        {
            con.ConnectionString = "";
            con.Open();
    
            using (SqlCommand sqlcom = new SqlCommand())
            {
                sqlcom.CommandText =
                    string.Format(
                        "insert into Address_Listing(Zip_Code,Street_Number,Street_Name,Image_Location) " +
                        "values ('{0}','{1}','{2}',  '{3}')",
                        zipcode, streetnumber, streetname, Imagelocation);
                sqlcom.Connection = con;
    
                int success = sqlcom.ExecuteNonQuery();
    
                return success > 0;
            }
        }
    }