Pages

Free Hosting
Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Monday, November 7, 2011

How to save vedeo files in SQL Database


In order to save a vedeo file into your database you need to convert your vedeo data into binary form by uploading the file into your web browser by file upload control. also you need to create a database of following columns
ID(int),Vedeo(binary(max)), Video_Name(nvarchar(50), Video_Size(bigint)
Then come into your web page where you inserted a file uploader. Add a button control of name=ButtonUpload and in the ButtonUpload_Click event handler write down the following code.
byte
[] buffer;

//this is the array of bytes which will hold the data (file)

SqlConnection connection=new SqlConnection("Server=.;Database=MyDatabase;User ID=sa;Password=password");

protected void ButtonUpload_Click(object sender, EventArgs e)

{

//check the file

if (FileUpload1.HasFile && FileUpload1.PostedFile != null

&& FileUpload1.PostedFile.FileName != "")

{

HttpPostedFile file = FileUpload1.PostedFile;

//retrieve the HttpPostedFile object

buffer = new byte[file.ContentLength];

int bytesReaded = file.InputStream.Read(buffer, 0,

FileUpload1.PostedFile.ContentLength);

//the HttpPostedFile has InputStream porperty (using System.IO;)

//which can read the stream to the buffer object,

//the first parameter is the array of bytes to store in,

//the second parameter is the zero index (of specific byte)

//where to start storing in the buffer,

//the third parameter is the number of bytes

//you want to read (do u care about this?)

if (bytesReaded > 0)

{

try

{



SqlCommand cmd = new SqlCommand

("INSERT INTO Vedeos (Vedeo, Video_Name, Video_Size) VALUES (@video, @videoName, @videoSize)", connection);

                   

cmd.Parameters.Add("@video",

SqlDbType.VarBinary, buffer.Length).Value = buffer;

cmd.Parameters.Add("@videoName",

SqlDbType.NVarChar).Value = FileUpload1.FileName;

cmd.Parameters.Add("@videoSize",

SqlDbType.BigInt).Value = file.ContentLength;

using (connection)

{

connection.Open();

int i = cmd.ExecuteNonQuery();

Label1.Text = "uploaded, " + i.ToString() + " rows affected";

}

}

catch (Exception ex)

{

Label1.Text = ex.Message.ToString();

}

finally

{ connection.Close(); }

}

}

else

{

Label1.Text =
"Choose a valid video file";

}

}

Monday, October 17, 2011

How to save XML data into database

Imports System.Xml
Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim command As SqlCommand
        Dim adpter As New SqlDataAdapter
        Dim ds As New DataSet
        Dim xmlFile As XmlReader
        Dim sql As String
        Dim product_ID As Integer
        Dim Product_Name As String
        Dim product_Price As Double
        connetionString = "Data Source=servername;Initial Catalog=databsename;User ID=username;Password=password"
        connection = New SqlConnection(connetionString)
        xmlFile = XmlReader.Create("Product.xml", New XmlReaderSettings())
        ds.ReadXml(xmlFile)
        Dim i As Integer
        connection.Open()
        For i = 0 To ds.Tables(0).Rows.Count - 1
            product_ID = Convert.ToInt32(ds.Tables(0).Rows(i).Item(0))
            Product_Name = ds.Tables(0).Rows(i).Item(1)
            product_Price = Convert.ToDouble(ds.Tables(0).Rows(i).Item(2))
            sql = "insert into Product values(" & product_ID & ",'" & Product_Name & "'," & product_Price & ")"
            command = New SqlCommand(sql, connection)
            adpter.InsertCommand = command
            adpter.InsertCommand.ExecuteNonQuery()
        Next
        connection.Close()
    End Sub
End Class

Accessing and Displaying Data with JScript .NET in ASP.NET


In this section, you create a simple page that uses the DataGrid control. This page will serve as the basis for the pages in subsequent sections.
To create a simple ASP.NET page with a DataGrid control
  1. In your Web directory, located at C:\Inetpub\wwwroot by default, create a new directory named DataAccess.
  2. Create a new ASP.NET file named DataAccess.aspx in the C:\Inetpub\wwwroot\DataAccess directory.
You can do this with an application such as Notepad.
Note   You cannot use the Visual Studio .NET integrated development environment (IDE) to create a JScript .NET project. However, you can edit ASP.NET files in the IDE.
  1. Open the ASP.NET page for editing in either the Visual Studio .NET IDE or Notepad.
Note   You cannot directly paste code copied from a richly formatted source (such as a Web page) into an ASP.NET page that you are editing in the Visual Studio .NET IDE. The IDE converts richly formatted text into HTML code that reproduces the look of the original text instead of pasting as unformatted text. You can use a two-step process to work around this restriction. First, paste a code example into a text-based editor (such as Notepad) to remove the rich formatting. Second, cut the code from the text-based editor and paste it into the IDE.
  1. Add the following code to the page:
<%@ Page Language=jscript %>
<html>
<body>
<script runat=server>
function Page_Load(sender : Object, e : EventArgs) {
// Put code in this function that you want to have loaded
// each time the page is requested from the server.
if (!IsPostBack) {
// Put code here that you want to run only the first time.
}
// Page_Load
</script>
<h3>Category List</h3>
<form runat=server>
<asp:DataGrid id="ItemsGrid" runat="server">
</asp:DataGrid>
</form>
</body>
</html>
  1. Save the page.
  2. View the page by typing the following address into the address bar of Internet Explorer:
http://localhost/DataAccess/DataAccess.aspx
When you access the page, it only displays the "Category List" header. The DataGrid control does not display data because no data has been bound to it.
Creating and Displaying a DataTable
Before you attempt to load data from a database, ensure that the DataGrid control works properly by using data from a well-controlled data source. In this section, you construct an ArrayList, and then you bind the ArrayList to the DataGrid control.
To bind data to a DataGrid control
  1. Add the following function to the script block.
function CreateTestData() : ICollection {
// Function to create a simple table.
var al : ArrayList = new ArrayList();
al.Add("orange");
al.Add("pear");
al.Add("banana");
return al;
} // CreateTestData
  1. Bind the data in the table to the DataGrid control by adding the following lines to the if (!IsPostBack) block of the Page_Load function:
ItemsGrid.DataSource = CreateTestData();
ItemsGrid.DataBind();
  1. Save the page.
  2. Reload the page in Internet Explorer, or type the following address in the address bar of Internet Explorer:
http://localhost/DataAccess/DataAccess.aspx
The page now displays a simple table of several fruits.