To day i will share with you , ADO.net Important Questions which is normally Ask by the interviewer at the Time of Interview.
ADO.NET is the new database technology of the .NET (Dot Net) platform, and it builds on Microsoft ActiveX® Data Objects (ADO).
ADO is a language-neutral object model that is the keystone of Microsoft's Universal Data Access strategy.
ADO.NET is an integral part of the .NET Compact Framework, providing access to relational data, XML documents, and application data. ADO.NET supports a variety of development needs. You can create database-client applications and middle-tier business objects used by applications, tools, languages or Internet browsers.
To Day I will Explain you Some Reasons, why a Data base Become slow (Normally When there is more Data).
In Student Life Projects we can not experience this kinds of issues because in student life Normally our Data Base may contain 10-30 tables maximum and Each table may contain 100-300 rows or above .Data base has no over head and we cannot experience Serious Issues.
But What Happen when there is a data base that may have at least 900-1000 table and each table may contain 10,000- 1,000,00 or above rows then we can Faces Serious issue like Data base is Slow....
Data base Performance is not good. Data retrieval time is too high and etc.
Following are some Issue which may Help you situation like above.
Following Step is Involve to take Back up of data base
First, you need to configure the Microsoft SQL Server Management Studio on your local machine. If you don’t have it, you can download it from the following location.
Step 1: Open your Microsoft SQL Server Management Studio, whichever you prefer, standard or express edition.
Step 2: Using your Database Username and Password, simply login to your MS SQL server database.
Step 3: Select the database >> Right-click >> Tasks >> Back Up [as shown in the image below]:
Once you click on the “Backup” the following Backup Database window will appear [as shown in the image below]:
Step 4: Select the following options:
Backup type: Full
Under Destination, Backup to: Disk
Step 5: Now, by clicking on the “Add” button the following window will appear to select the path and file name for the database backup file [as shown in the image below]:
Step 6: Select the destination folder for the backup file and enter the “File name” with .bak extension [as shown in the image below]:
Make sure you place your MS SQL database .bak file under the MSSQL backup folder.
Step 7: Hit the OK button to finish the backup of your MS SQL Server 2008 Database. Upon the successful completion of database backup, the following confirmation window will appear with a message “The backup of database “yourdatabasename” completed successfully. [as shown in the image below]:
Following the above shown steps, you will be able to create a successful backup of your MS SQL Server 2008 Database into the desired folder.
How to Restore MS SQL Server 2008 Database Backup File ?
In order to restore a database from a backup file, follow the steps shown below:
Step 1: Open your Microsoft SQL Server Management Studio Express and connect to your database.
Step 2: Select the database >> Right-click >> Tasks >> Restore >> Database [as shown in the image below]:
Step 3: The following “Restore Database“ windows will appear. Select “From device” mentioned under the “Source for restore” and click the button infront of that to specify the file location [as shown in the image below]:
Step 4: Select the option “Backup media as File” and click on the Add button to add the backup file location [as shown in the image below]:
Step 5: Select the backup file you wish to restore and Hit the OK button [as shown in the image below]:
That’s it! You will get the confirmation windows with a message “The restoration of database “yourdatabasename” completed successfully.” Now you know the procedure of backing up and restoring MS SQL Server 2008 Database.
Recently we changed our DAC layer from using inline SQL to stored procedures in the database. On some of these SQL, we did record deletion (usually only 1 record), and we just execute it via IDBCommand.ExecuteNonQuery() and then check the return value to see how many records were affected (which should be 1) for verification that the query actually does something. With the change to stored procedure, we just return 1 in the stored procedure if the delete is successful. However, the calling code then started to show these deletions as errors. Apparently ExecuteNonQuery only returns the number of affected rows on SELECT, INSERT and DELETE statements; for everything else it returns -1. So I tried to figure out how to get a return value from a stored procedure.Let's assume a simplistic stored procedure as follows:
ALTERPROC ReturnOnly
AS
BEGIN
RETURN 5
END
You can't use ExecuteScalar to get the returned value, and ExecuteNonQuery will always return -1. To get the value back, you need to add a return value parameter to the command. The name of the parameter is not important. The code to get the value returned by that procedure will be as follows:
Here’s a way to reset the value of the identity column in SQL Server . The scenario is explained below .
For example , when the table “Customers” has a identity column with the initial value of 1 and seed 1 .Each time when you start an App and perform an operation , you might want to delete all the records
inside the table and perform the new inserts . ( Not the best of the methods , but the App had to do it ) .
Now Each time i wanted to have the identity value start from 1 .
The Delete statement alone is not enough to reset the identity value .
After the Deletion of the records we should execute the DBCC command along with the CHECKIDENT switch along with the table name and the seed value .
Like this
DELETEFROM CUSTOMERS
DBCC CHECKIDENT (CUSTOMERS,RESEED, 0)
DELETE FROM CUSTOMERS
DBCC CHECKIDENT (CUSTOMERS,RESEED, 0)
Here comes a better approach , instead of using the DELETE and DBCC commands , the Truncate will do the job for you .
TRUNCATETABLE CUSTOMERS
TRUNCATE TABLE CUSTOMERS
This will delete the records as well as reset the identity value
In this example i am going to describe how to Insert record or edit or delete record in GridView using SqlDataSource.
For inserting record, i've put textboxes in footer row of GridView using ItemTemplate and FooterTemaplete.
Go to design view of aspx page and drag a GridView control from toolbox, click on smart tag of GridView and choose new datasource
Select Database and click Ok
In next screen, Enter your SqlServer name , username and password and pick Database name from the dropdown , Test the connection
In next screen, select the table name and fields , Click on Advance tab and check Generate Insert,Edit and Delete statements checkbox , alternatively you can specify your custom sql statements
Click on ok to finish
Check Enable Editing , enable deleting checkbox in gridView smart tag
Now go to html source of page and define DatakeyNames field in gridview source
Remove the boundFields and put ItemTemplate and EditItemTemplate and labels and textboxs respectively, complete html source of page should look like this
<formid="form1"runat="server"><div><asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"DataKeyNames="ID"DataSourceID="SqlDataSource1"OnRowDeleted="GridView1_RowDeleted"OnRowUpdated="GridView1_RowUpdated"ShowFooter="true"OnRowCommand="GridView1_RowCommand"><Columns><asp:CommandFieldShowDeleteButton="True"ShowEditButton="True"/><asp:TemplateFieldHeaderText="ID"SortExpression="ID"><ItemTemplate><asp:LabelID="lblID"runat="server"Text='<%#Eval("ID") %>'></asp:Label></ItemTemplate><FooterTemplate><asp:ButtonID="btnInsert"runat="server"Text="Insert"CommandName="Add"/></FooterTemplate></asp:TemplateField><asp:TemplateFieldHeaderText="FirstName"SortExpression="FirstName"><ItemTemplate><asp:LabelID="lblFirstName"runat="server"Text='<%#Eval("FirstName") %>'></asp:Label></ItemTemplate><EditItemTemplate><asp:TextBoxID="txtFirstName"runat="server"Text='<%#Bind("FirstName") %>'></asp:TextBox></EditItemTemplate><FooterTemplate><asp:TextBoxID="txtFname"runat="server"></asp:TextBox></FooterTemplate></asp:TemplateField><asp:TemplateFieldHeaderText="LastName"SortExpression="LastName"><ItemTemplate><asp:LabelID="lblLastName"runat="server"Text='<%#Eval("LastName") %>'></asp:Label></ItemTemplate><EditItemTemplate><asp:TextBoxID="txtLastName"runat="server"Text='<%#Bind("LastName") %>'></asp:TextBox></EditItemTemplate><FooterTemplate><asp:TextBoxID="txtLname"runat="server"></asp:TextBox></FooterTemplate></asp:TemplateField><asp:TemplateFieldHeaderText="Department"SortExpression="Department"><ItemTemplate><asp:LabelID="lblDepartment"runat="server"Text='<%#Eval("Department") %>'></asp:Label></ItemTemplate><EditItemTemplate><asp:TextBoxID="txtDepartmentName"runat="server"Text='<%#Bind("Department") %>'></asp:TextBox></EditItemTemplate><FooterTemplate><asp:TextBoxID="txtDept"runat="server"></asp:TextBox></FooterTemplate></asp:TemplateField><asp:TemplateFieldHeaderText="Location"SortExpression="Location"><ItemTemplate><asp:LabelID="lblLocation"runat="server"Text='<%#Eval("Location") %>'></asp:Label></ItemTemplate><EditItemTemplate><asp:TextBoxID="txtLocation"runat="server"Text='<%#Bind("Location") %>'></asp:TextBox></EditItemTemplate><FooterTemplate><asp:TextBoxID="txtLoc"runat="server"></asp:TextBox></FooterTemplate></asp:TemplateField></Columns></asp:GridView><asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:DBConString%>"DeleteCommand="DELETE FROM [Employees] WHERE [ID] = @ID"InsertCommand="INSERT INTO [Employees] ([FirstName],
[LastName],[Department], [Location])
VALUES (@FirstName, @LastName, @Department, @Location)"SelectCommand="SELECT [ID], [FirstName], [LastName],
[Department], [Location] FROM [Employees]"UpdateCommand="UPDATE [Employees] SET
[FirstName] = @FirstName, [LastName] = @LastName,
[Department] = @Department, [Location] = @Location
WHERE [ID] = @ID"OnInserted="SqlDataSource1_Inserted"><DeleteParameters><asp:ParameterName="ID"Type="Int32"/></DeleteParameters><UpdateParameters><asp:ParameterName="FirstName"Type="String"/><asp:ParameterName="LastName"Type="String"/><asp:ParameterName="Department"Type="String"/><asp:ParameterName="Location"Type="String"/><asp:ParameterName="ID"Type="Int32"/></UpdateParameters><InsertParameters><asp:ParameterName="FirstName"Type="String"/><asp:ParameterName="LastName"Type="String"/><asp:ParameterName="Department"Type="String"/><asp:ParameterName="Location"Type="String"/></InsertParameters></asp:SqlDataSource><asp:LabelID="lblMessage"runat="server"Font-Bold="True"></asp:Label><br/></div></form>
Write this code in RowCommand Event of GridView in codebehind C# code Behind
In this example i am Uploading Images using FileUpload Control and saving or storing them in SQL Server database in ASP.NET with C# and VB.NET.
Database is having a table named Images with three columns.
1. ID Numeric Primary key with Identity Increment.
2. ImageName Varchar to store Name of Image.
3. Image Image to store image in binary format.
After uploading and saving images in database, images are displayed in GridView.