Wednesday, March 3, 2010

C# code for upload image file into database

Database Table

Fields                          Type
img_name                - varchar(50)
img_data                  - image
img_contenttype       -varchar(50)

In form


1.    Add fileupload control
2.    Add button control
3.    Add label control for message display
4.    Write the below code in button onclick event

code
Stream imgstream = FileUpload1.PostedFile.InputStream;
int imglen = FileUpload1.PostedFile.ContentLength;
string imgname = FileUpload1.FileName;
string imgtype = FileUpload1.PostedFile.ContentType;
//create a byte array
byte[] imgdata = new byte[imglen];
//convert Image Stream to a Byte array
int n = imgstream.Read(imgdata, 0, imglen);

SqlConnection scon = new SqlConnection(ConfigurationSettings.AppSettings["DSN"]);

(Or)
SqlConnection scon = new SqlConnection("ConnectionString");
string Sql = "INSERT INTO Image (img_name,img_data,img_contenttype) VALUES ( '" + imgname + "', '" + imgdata + "','" + imgtype + "' )";
SqlCommand cmd = new SqlCommand(Sql, scon);
scon.Open();
int val = cmd.ExecuteNonQuery();
scon.Close();
if (val > 0)
{
Label1.Text = "The Image was saved";
}
else
{
Label1.Text = "An error occurred uploading the image";       
}

Friday, February 12, 2010

How to retrive data from database by C#

//First we should add the following Namespace

using System.Data;
using System.Data.OleDb;


{
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\ERDATA.mdb;Persist Security Info=True");
        connection.Open();
        String query = "Select sum(amount) FROM Tran";
        OleDbDataAdapter dadaptor = new OleDbDataAdapter(query, connection);
        DataSet ds = new DataSet();
        dadaptor.Fill(ds);

        if (ds.Tables[0].Rows.Count > 0)
        {
            String sum = ds.Tables[0].Rows[0][0].ToString();
            lblTotal.Text = "Rs. " + sum;
        }
        connection.Close();
        connection.Dispose();
}

Tuesday, February 9, 2010

C# code for Insert Data into Database

//First we should add the following Namespace

using System.Data;
using System.Data.OleDb;

//Database connection code

{
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\SamDB.mdb;Persist Security Info=True");
connection.Open();
String query;
query = "insert into sample(NAME,ADDRESS,PHONENO)" +
             "VALUES ('" + custName + "'," + txtAddress.Text.Trim() + ",'" + txtMobileNumber.Text.Trim() + "')";
       
OleDbCommand command = new OleDbCommand(query, connection);
int status = command.ExecuteNonQuery();
if (status > 0)
lblTranStatus.Text = status + " Data inserted Successfully";
else
      lblTranStatus.Text = "FAILED!!!";

connection.Close();
connection.Dispose();
}

Monday, February 8, 2010

PHP code for Database connection

//Database connection code


//mysql_connect(servername,username,password);

$con = mysql_connect("localhost","","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "
";
}
mysql_close($con);
?>

Thursday, February 4, 2010

What is a database?

A database is a collection of information that is organized so that it can easily be accessed, managed, and updated. A database is an integrated collection of logically-related records or files consolidated into a common pool that provides data for one or more multiple uses. One way of classifying databases involves the type of content, for example: bibliographic, full-text, numeric, image. Other classification methods start from examining database models or database architectures: see below. Software organizes the data in a database according to a database model. As of 2010[update] the relational model occurs most commonly. Other models such as the hierarchical model and the network model use a more explicit representation of relationships.