Lowyat.NET Forums

Welcome Guest ( Log In | Register )

LYN wins Intel-Lenovo-Tangs Blogathon challenge. Thank you everybody!
 
RSS feedBump TopicReply to this topicStart new topicStart Poll

Outline · [ Standard ] · Linear+

> retrive data from MSSQL, C# (.NET)

elite87
post Jul 2 2009, 04:45 PM
Show posts by this member only |This post's rating (0+, 0-) | Post #1


Casual
***

Group: Junior Member
Posts: 333
Ratings earned: 0+, 0-
Ratings given: 0+, 0-

Joined: Feb 2007





I have uploaded the image to the datbase... bt i duno hw to retrive n show it bk in web based...


this is my code to upload in datbase...


CODE
namespace add_edit_delete
{
   public partial class upload : System.Web.UI.Page
   {
protected System.Web.UI.WebControls.Button UploadBtn;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.HtmlControls.HtmlInputText txtImgName;
protected System.Web.UI.HtmlControls.HtmlInputFile UploadFile;





private void Page_Load(object sender, System.EventArgs e){ }

public void UploadBtn_Click(object sender, System.EventArgs e)
{
if (Page.IsValid) //save the image
{
Stream imgStream = UploadFile.PostedFile.InputStream;
int imgLen = UploadFile.PostedFile.ContentLength;
string imgContentType = UploadFile.PostedFile.ContentType;
string imgName = txtImgName.Value;
byte[] imgBinaryData = new byte[imgLen];
int n = imgStream.Read(imgBinaryData,0,imgLen);

int RowsAffected = SaveToDB( imgName, imgBinaryData,imgContentType);
if ( RowsAffected>0 )
{
Response.Write("<BR>The Image was saved");
Response.Redirect("viewImage.aspx");
}
else
{
Response.Write("<BR>An error occurred uploading the image");
}

}
}


private int SaveToDB(string imgName, byte[] imgbin, string imgcontenttype)
{
//use the web.config to store the connection string
   SqlConnection connection = new SqlConnection("Data Source=xxx;Initial Catalog=xx;User ID=xx;Password=xxx");
SqlCommand command = new SqlCommand( "INSERT INTO Image (img_name,img_data,img_contenttype) VALUES ( @img_name, @img_data,@img_contenttype )", connection );

SqlParameter param0 = new SqlParameter( "@img_name", SqlDbType.VarChar,50 );
param0.Value = imgName;
command.Parameters.Add( param0 );

SqlParameter param1 = new SqlParameter( "@img_data", SqlDbType.Image );
param1.Value = imgbin;
command.Parameters.Add( param1 );

SqlParameter param2 = new SqlParameter( "@img_contenttype", SqlDbType.VarChar,50 );
param2.Value = imgcontenttype;
command.Parameters.Add( param2 );

connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close();

return numRowsAffected;
}
}
}





how to retrive the data saved to database????
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
vinothrao84
post Jul 3 2009, 12:29 PM
Show posts by this member only |This post's rating (0+, 0-) | Post #2


Getting Started
**

Group: Junior Member
Posts: 59
Ratings earned: 0+, 0-
Ratings given: 0+, 0-

Joined: Nov 2006





User is offlineProfile CardPM
Go to the top of the page
+Quote Post
feeyeah
post Jul 7 2009, 04:32 PM
Show posts by this member only |This post's rating (0+, 0-) | Post #3


Newbie
*

Group: Junior Member
Posts: 28
Ratings earned: 0+, 0-
Ratings given: 0+, 0-

Joined: Jul 2009




Since u are using C#, why not give a try on Linq to SQL?
work like a charm... no need to worry on how to add/edit/delete... just need to learn the lambda expression.
Thanks.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Bump TopicReply to this topicTopic OptionsStart new topic
 



----debug section please ignore----
Lo-Fi Version Time is now: 23rd November 2009 - 07:43 PM
All Rights Reserved 2003-2009 Vijandren Ramadass (~living on a prayer~)