Showing posts with label Database. Show all posts
Showing posts with label Database. Show all posts

Wednesday, January 6, 2010

Retrieve Images from fatabase

This is a sample code to retrieve images from data base in a gridview as to upload images to database.

use fileupload control to upload images and on upload button click retrieve image from fileupload control(named as fulimage)by writing

StreamimgStream = fuImage.PostedFile.InputStream;

and insert it into database along with other parameters.

to retrieve image from database in a gridview use generic handler. place a template in gridview and place itemtemplate in it.now add a image here like this.



and in the ImageHandler.ashx get image id from query string and retrive image from database in a datareader then return that image by this code

context.Response.BinaryWrite((Byte[])dr[0]);

here dr[0] means that image is in first field of the database.

Download the complete code here

Upload Image to database in Binary formate

This is a sample code for uploading images in database.


protected void Button1_Click(object sender, EventArgs e)
{
byte[] image = FileUpload1.FileBytes;

String name = TextBox1.Text;

String qryString = "INSERT INTO [Table] VALUES ( '" + name + "' , '" + image + "' )";
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
using (SqlCommand command = new SqlCommand(qryString, connection))
{
connection.Open();
command.ExecuteNonQuery();
}
}
}


for more details download code from here

Saturday, August 29, 2009

All Connection Strings Sample

You have the .NET Framework on your local computer and you want to connect to a Microsoft Access database called database1.mdb located in the following folder on your hard disk: c:\database1.mdb. Here are the parameters to create this connection string:

Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\database1.mdb;

OLE DB Provider for DB2 (from Microsoft)

For TCP/IP connections
oConn.Open = "Provider=DB2OLEDB;" & _
"Network Transport Library=TCPIP;" & _
"Network Address=xxx.xxx.xxx.xxx;" & _
"Initial Catalog=MyCatalog;" & _
"Package Collection=MyPackageCollection;" & _
"Default Schema=MySchema;" & _
"User ID=MyUsername;" & _
"Password=MyPassword"
For APPC connections

oConn.Open = "Provider=DB2OLEDB;" & _
"APPC Local LU Alias=MyLocalLUAlias;" & _
"APPC Remote LU Alias=MyRemoteLUAlias;" & _
"Initial Catalog=MyCatalog;" & _
"Package Collection=MyPackageCollection;" & _
"Default Schema=MySchema;" & _
"User ID=MyUsername;" & _
"Password=MyPassword"

ODBC Driver for Excel

oConn.Open "Driver={Microsoft Excel Driver (*.xls)};" & _
"DriverId=790;" & _
"Dbq=c:\somepath\mySpreadsheet.xls;" & _
"DefaultDir=c:\somepath"

ODBC Driver for MySQL (via MyODBC)
To connect to a local database (using MyODBC Driver)


oConn.Open "Driver={mySQL};" & _
"Server=MyServerName;" & _
"Option=16834;" & _
"Database=mydb"

To connect to a remote database


oConn.Open "Driver={mySQL};" & _
"Server=db1.database.com;" & _
"Port=3306;" & _
"Option=131072;" & _
"Stmt=;" & _
"Database=mydb;" & _
"Uid=myUsername;" & _
"Pwd=myPassword"

To connect to a local database (using MySQL ODBC 3.51 Driver)

oConn.Open "DRIVER={MySQL ODBC 3.51 Driver};" & _
"Server=myServerName;" & _
"Port=3306;" & _
"Option=16384;" & _
"Stmt=;" & _
"Database=mydatabaseName;" & _
"Uid=myUsername;" & _
"Pwd=myPassword"
Or
oConn.Open "DRIVER={MySQL ODBC 3.51 Driver};" & _
"SERVER=myServerName;" & _
"DATABASE=myDatabaseName;" & _
"USER=myUsername;" & _
"PASSWORD=myPassword;"

Tuesday, June 30, 2009

Update database using DataSet and SqlDataAdapter

Here we are going to see how to update database using DataSet and SqlDataAdapter.

using System;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace UpdateDatabase
{
public class TestDataSet : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button btnLoad;
private System.Windows.Forms.Button btnUpdate;
private DataSet ds;
private SqlDataAdapter adap;
private SqlConnection con;
private System.Windows.Forms.Label lblTitle;
private System.Windows.Forms.TextBox txtTitle;
private System.ComponentModel.Container components = null;
public TestDataSet()
{
InitializeComponent();
con = new SqlConnection("server=.;uid=sa;pwd=test;database=test");}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.btnUpdate = new System.Windows.Forms.Button();
this.btnLoad = new System.Windows.Forms.Button();
this.txtTitle = new System.Windows.Forms.TextBox();
this.lblTitle = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(16, 80);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(568, 280);
this.dataGrid1.TabIndex = 0;
//
// btnUpdate
//
this.btnUpdate.Location = new System.Drawing.Point(176, 369);
this.btnUpdate.Name = "btnUpdate";
this.btnUpdate.Size = new System.Drawing.Size(112, 23);
this.btnUpdate.TabIndex = 1;
this.btnUpdate.Text = "Update Title";
this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
//
// btnLoad
//
this.btnLoad.Location = new System.Drawing.Point(504, 56);
this.btnLoad.Name = "btnLoad";
this.btnLoad.TabIndex = 2;
this.btnLoad.Text = "Load";
this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
//
// txtTitle
//
this.txtTitle.Location = new System.Drawing.Point(64, 372);
this.txtTitle.Name = "txtTitle";
this.txtTitle.TabIndex = 3;
this.txtTitle.Text = "";
//
// lblTitle
//
this.lblTitle.AutoSize = true;
this.lblTitle.Location = new System.Drawing.Point(16, 376);
this.lblTitle.Name = "lblTitle";
this.lblTitle.Size = new System.Drawing.Size(26, 16);
this.lblTitle.TabIndex = 4;
this.lblTitle.Text = "Title";
//
// TestDataSet
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(616, 438);
this.Controls.Add(this.lblTitle);
this.Controls.Add(this.txtTitle);
this.Controls.Add(this.btnLoad);
this.Controls.Add(this.btnUpdate);
this.Controls.Add(this.dataGrid1);
this.Name = "TestDataSet";
this.Text = "Data";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new TestDataSet());
}
private void btnLoad_Click(object sender, System.EventArgs e)
{
LoadData();
}
//Load data from database make sure we are fetching primarykey too so we could use adapter
update method
//on dataset because commandbuilder will create command on that primary key.
//Commands will be created as it see rowstate of dataset table's row's rowstate.
private void LoadData()
{
if(ds != null)
ds.Clear();
adap = new SqlDataAdapter("select id,title, description from testtable", con);
ds = new DataSet();
adap.Fill(ds);
dataGrid1.DataSource = ds.Tables[0];
}
//This click will update one of the field in the database using adapter update() method on
dataset.
private void btnUpdate_Click(object sender, System.EventArgs e)
{
SqlCommandBuilder com = new SqlCommandBuilder(adap);
foreach(DataRow dr in ds.Tables[0].Rows)
dr["title"] = txtTitle.Text;
adap.Update(ds);
}
}
}