如何将图像放入sql server 2005
我编写了以下代码来将图像路径放入 sql server 2005,但它不起作用是他们从客户端应用程序将图像放入 sql server 的任何替代方法。
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>
example.js
$("#Addresslisting").click(function() {
var zipcode = ($("#addzipcode").val());
var streetno = ($("#addstreetno").val());
var streetname = ($("#addstreetname").val());
var image = ($("#addpicture").val());
var submitaddress = "{\"zipcode\":\"" + zipcode + "\",\"streetnumber\":\"" + streetno + "\",\"streetname\":\"" + streetname + "\",\"streetname\":\"" + streetname + "\",\"Imagelocation\":\"" + image + "\"}";
$.ajax({
type: "POST",
url: "/exampleproject/Afterlogin.asmx/addresslisting",
data: submitaddress,
contentType: "application/json; charset=utf-8",
success: ajaxSucceed,
dataType: "json",
failure: ajaxFailed
});
});
asmx webservices 文件
[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;
}
}
I have written the following code to place the image path into sql server 2005 but its not working is their any alternate way to place images into sql server from clientside application.
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>
example.js
$("#Addresslisting").click(function() {
var zipcode = ($("#addzipcode").val());
var streetno = ($("#addstreetno").val());
var streetname = ($("#addstreetname").val());
var image = ($("#addpicture").val());
var submitaddress = "{\"zipcode\":\"" + zipcode + "\",\"streetnumber\":\"" + streetno + "\",\"streetname\":\"" + streetname + "\",\"streetname\":\"" + streetname + "\",\"Imagelocation\":\"" + image + "\"}";
$.ajax({
type: "POST",
url: "/exampleproject/Afterlogin.asmx/addresslisting",
data: submitaddress,
contentType: "application/json; charset=utf-8",
success: ajaxSucceed,
dataType: "json",
failure: ajaxFailed
});
});
asmx webservices file
[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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅供参考,您的代码存在许多问题。应该更像是这样:
using
块将确保资源被释放,即使抛出异常FYI, there are a number of problems with your code. It should be more like this:
using
blocks will ensure that resources are released, even if an exception is thrown