{"id":4799,"date":"2020-09-10T17:16:12","date_gmt":"2020-09-10T11:46:12","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=4799"},"modified":"2024-09-24T18:40:03","modified_gmt":"2024-09-24T13:10:03","slug":"asp-net-database-connection","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/asp-net-database-connection\/","title":{"rendered":"ASP.NET Database Connection"},"content":{"rendered":"\n<p>ASP.NET Database Connection can connect with most databases, including Oracle, Microsoft SQL Server, MongoDB, and MySQL.&nbsp;<\/p>\n\n\n\n<p>Here we are going to use <a href=\"https:\/\/www.microsoft.com\/en-in\/sql-server\/sql-server-2019\" rel=\"nofollow noopener\" target=\"_blank\">Microsoft SQL Server<\/a> as our database. Microsoft SQL Server is free database software, and one can easily download and install Microsoft SQL Server Express Edition.<\/p>\n\n\n\n<p><strong><em>The below concepts are common in all databases:<\/em><\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Connection:<\/strong> While working with a database, the first step is to establish a connection. To establish a connection, the following parameters are required:<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Database name or data source: <\/strong>The name of the database to which the connection can be made. You can work with only one database at a time.<\/li>\n\n\n\n<li><strong>Credentials:<\/strong> It includes the username and password that are required to establish an <a href=\"https:\/\/www.h2kinfosys.com\/blog\/introduction-to-asp-net\/\">ASP.NET<\/a> Database Connection to the database. Credential is required for SQL Server Authentication and it is optional for Windows Authentication.<\/li>\n\n\n\n<li><strong>Optional Parameters:<\/strong> You can also specify optional parameters to provide more information on how .NET should be connected to the database to handle the data.<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Selecting data from the database:<\/strong> Once the connection is established, data can be fetched from the database by executing <a href=\"https:\/\/www.h2kinfosys.com\/courses\/sql-online-training-course\/\">SQL<\/a> select command against the database. The SQL statement is used to fetch data from a specific table in the database.<\/li>\n\n\n\n<li><strong>Inserting data into the database:<\/strong> You can insert new records into the database and the values for each row that are to be inserted in the database should be specified.<\/li>\n\n\n\n<li><strong>Updating data into the database:<\/strong> Existing records can also be updated into the database. New values should be specified for each row that needs to be updated into the database.<\/li>\n\n\n\n<li><strong>Deleting data from the database:<\/strong> Records can also be deleted from the database.\u00a0<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"DotNet Interview Questions on SQL Server and asp.Net | H2kinfosys\" width=\"800\" height=\"450\" src=\"https:\/\/www.youtube.com\/embed\/hJTGadEq1vk?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>ASP.NET Database Connection<\/strong><\/h2>\n\n\n\n<p>We will connect to a database that has the name of Sampledb. The credentials that are needed to connect to the database are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Username- dbtest<\/li>\n\n\n\n<li>Password- sample123<\/li>\n<\/ul>\n\n\n\n<p>To establish a connection, open your web application project and write the below <a href=\"https:\/\/www.h2kinfosys.com\/blog\/database-setup-mysql-oraclexe\/\">SQL Server Connection<\/a> String in Web.config file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;connectionStrings>\n    &lt;add name=\"ConString\" connectionString=\" Data Source=WIN-50GP30FGO75;Initial Catalog=Demodb ;User ID=dbtest;Password=samplel23\"\/>\n&lt;\/connectionStrings><\/code><\/pre>\n\n\n\n<p>Then, add System.Configuration Reference by right clicking on the project and click Add Reference option from the Context Menu.<\/p>\n\n\n\n<p>From the dialog box Add Reference, click on .Net Tab and look for System.Configuration assembly. Select the component and click OK.<\/p>\n\n\n\n<p>Once you have added the reference, it will appear in the References folder of the Solution Explorer.<\/p>\n\n\n\n<p>Now, write the behind code to get the connection string from web.config file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Configuration;\nnamespace DemoApplication\n{\u00a0\u00a0\npublic partial class Demo\u00a0 System.Web.UI.Page\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0{\u00a0\u00a0\n\u00a0 protected void PageLoad(object sender, EventArgs e)\u00a0\u00a0\n\u00a0 {\u00a0\u00a0\nstring connetionString;\nSqlConnection cnn;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\nconnetionString = ConfigurationManager.ConnectionStrings&#91;\"ConString\"].ConnectionString;\ncnn = new SqlConnection(connetionString);\ncnn.Open();\u00a0\u00a0\nResponse.Write(\"Connection Made\");\u00a0\u00a0\u00a0\u00a0\nconn.Close();\u00a0\u00a0\n}\n}\n}<\/code><\/pre>\n\n\n\n<p>Here, the Data Source is the name of the server on which the database resides. Initial Catalog denotes the name of the database. User ID and Password are the credentials that are required.<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/UxCD3j-l-wgSXGPwBeDw0Hn_F2IZMMinavjSrXjM0R5hAeX7N44IlB53zoAizeUryPJ6mvKq-ctk9wQyPDFAcmXZPodOiTRoUyZRpXNrgQA8HikK5xUqvsQf-VrnfX3DNaFdNbWXtucrWi4SuQ\" alt=\"Database Connection\" title=\"\"><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>Read Database using SqlDataReader:&nbsp;<\/strong><\/h3>\n\n\n\n<p>Suppose you have a table named demotable used to store the ID and names of various Tutorials. The table contains two columns, one is TutorialID, and the other is TutorialName.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>TutorialID<\/td><td>TutorialName<\/td><\/tr><tr><td>1<\/td><td>C#<\/td><\/tr><tr><td>2<\/td><td>ASP.NET<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>For this, write the below code to read the data using a select statement.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace DemoApplication\n{\u00a0\u00a0\npublic partial class Demo System.Web.UI.Page\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0{\u00a0\u00a0\n\u00a0 protected void PageLoad(object sender, EventArgs e)\u00a0\u00a0\n\u00a0 {\u00a0\u00a0\nSqlCommand command = new SqlCommand(\"Select TutorialID,TutorialName from demotable\",cnn);\nSqlDataReader dataReader;\nString Output =\" \";\ndataReader = command.ExecuteReader();\nwhile (dataReader.Read())\n{\n\u00a0 Output = Output + dataReader.GetValue(0) + \"-\" + dataReader.GetValue(1) + \"&lt;\/br>\";\n}\nResponse.Write(Output);\u00a0\ndataReader.Close();\ncommand.dispose();\nconn.Close();\n\u00a0 }\n}\n}<\/code><\/pre>\n\n\n\n<p>Here SQLCommand is a predefined class used to perform operations of reading and writing into the database. The DataReader object is used to retrieve all the data specified by the SQL query and we can read all the table rows one by one with the help of the data reader.<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/fH7XZYz_0XaXKrMDE8iZkrkifulny5_FacUL_Pv4bBDSf5YmRn8pYg_aCcgsIF9Y8q1-9OsuPrtVCr_AToVxijbD5F6Dx4vMoN4ZDv42d1A015UWw6NieOrqiZhdFsVvOsnNQXzzBK-FrJACvw\" alt=\"Database Connection\" title=\"\"><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>Insert Database Record using InsertCommand:<\/strong><\/h3>\n\n\n\n<p>Suppose in the existing demotable; you want to insert a new record.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>TutorialID<\/td><td>TutorialName<\/td><\/tr><tr><td>1<\/td><td>C#<\/td><\/tr><tr><td>2<\/td><td>ASP.NET<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The new record to be added is:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>TutorialID<\/td><td>TutorialName<\/td><\/tr><tr><td>3<\/td><td>VB.NET<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Write the below code to insert a new record in the existing table:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace DemoApplication\n{  \npublic partial class Demo System.Web.UI.Page  \n    {  \n  protected void PageLoad(object sender, EventArgs e)  \n  {  \nSqlCommand command = new SqlCommand(\"Insert into demotable(TutorialID,TutorialName) value(3, '\" + \"VB.Net +\"')\",cnn);\nSqlDataAdapter adapter = new SqlDataAdapter(); \nadapter.InsertCommand = command; \nadapter.InsertCommand.ExecuteNonQuery();\ncommand.Dispose(): \ncnn.Close();\n  }\n}\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Initial Steps in Configuring a DataAdapter<\/h2>\n\n\n\n<p>Here, SQLCommand is used to define objects that perform SQL operations against a database. This object will hold the SQL command that will run against our SQL Server database.<\/p>\n\n\n\n<p>The first step in configuring a DataAdapter is to select the appropriate command objects that the DataAdapter will use to interact with the data source. This selection process involves specifying the SQL statements or stored procedures that define how data is retrieved, inserted, updated, and deleted within the database.<\/p>\n\n\n\n<p>To begin, you must create and configure the necessary command objects. Typically, these are the <code>SelectCommand<\/code>, <code>InsertCommand<\/code>, <code>UpdateCommand<\/code>, and <code>DeleteCommand<\/code>. The <code>SelectCommand<\/code> is the most crucial, as it defines the query or stored procedure used to fetch data from the database. This command object must be set with a valid SQL query or stored procedure call that retrieves the desired data set.<\/p>\n\n\n\n<p>Once the <code>SelectCommand<\/code> is configured, the DataAdapter can use it to fill a <code>DataSet<\/code> or <code>DataTable<\/code> with data from the database. Configuring the other command objects (<code>InsertCommand<\/code>, <code>UpdateCommand<\/code>, and <code>DeleteCommand<\/code>) ensures that changes made to the <code>DataSet<\/code> or <code>DataTable<\/code> can be accurately reflected back in the database, maintaining data integrity and consistency.<\/p>\n\n\n\n<p>By carefully selecting and configuring these command objects, you ensure that the DataAdapter operates efficiently and accurately within your application&#8217;s data management processes.<\/p>\n\n\n\n<p>Now we will write the below code to display the new record in the browser:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace DemoApplication\n{  \npublic partial class Demo System.Web.UI.Page  \n    {  \n  protected void PageLoad(object sender, EventArgs e)  \n  {  \nSqlCommand command = new SqlCommand(\"Select TutorialID,TutorialName from demotable\",cnn);\nSqlDataReader dataReader;\nString Output =\" \";\ndataReader = command.ExecuteReader();\nwhile (dataReader.Read())\n{\n  Output = Output + dataReader.GetValue(0) + \"-\" + dataReader.GetValue(1) + \"&lt;\/br>\";\n}\nResponse.Write(Output); \ndataReader.Close();\ncommand.dispose();\nconn.Close();\n  }\n}\n}<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/_nibpnhyKQGks-NSOQhzPIHvm5KJuQIc8SbE0VsDjgJ2fkOlMc6qqN0R1Jp7BZ8N3EzB_WdQkby3mooLBTOavChn49hCPl0vLmXP0fG8joOkgRXm2HoBcpd3KSe-fMsM3QXG54VRDHvFiqL15g\" alt=\"Database Connection\" title=\"\"><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>Update Database Record using UpdateCommand:<\/strong><\/h3>\n\n\n\n<p>You can also update the existing records in the database.&nbsp;<\/p>\n\n\n\n<p>Let us take the same table.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>TutorialID<\/td><td>TutorialName<\/td><\/tr><tr><td>1<\/td><td>C#<\/td><\/tr><tr><td>2<\/td><td>ASP.NET<\/td><\/tr><tr><td>3<\/td><td>VB.NET<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Suppose we want to update the TutorialName VB.NET to VB.NET complete.<\/p>\n\n\n\n<p>For this, we need to add the below code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace DemoApplication\n{\u00a0\u00a0\npublic partial class Demo System.Web.UI.Page\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0{\u00a0\u00a0\n\u00a0 protected void PageLoad(object sender, EventArgs e)\u00a0\u00a0\n\u00a0 {\u00a0\u00a0\nSqlCommand command = new SqlCommand(\"Update demotable set TutorialName='\"VB.Net Complete\"+\"' where TutorialID=3\",cnn);\nSqlDataAdapter adapter = new SqlDataAdapter();\u00a0\nadapter.InsertCommand = command;\u00a0\nadapter.InsertCommand.ExecuteNonQuery;\ncommand.Dispose():\u00a0\ncnn.Close();\n\u00a0 }\n}\n}<\/code><\/pre>\n\n\n\n<p>Now, to see the above changes in the browser, you need to add the below code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace DemoApplication\n{\u00a0\u00a0\npublic partial class Demo System.Web.UI.Page\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0{\u00a0\u00a0\n\u00a0 protected void PageLoad(object sender, EventArgs e)\u00a0\u00a0\n\u00a0 {\u00a0\u00a0\nSqlCommand command = new SqlCommand(\"Select TutorialID,TutorialName from demotable\",cnn);\nSqlDataReader dataReader;\nString Output =\" \";\ndataReader = command.ExecuteReader();\nwhile (dataReader.Read())\n{\n\u00a0 Output = Output + dataReader.GetValue(0) + \"-\" + dataReader.GetValue(1) + \"&lt;\/br>\";\n}\nResponse.Write(Output);\u00a0\ndataReader.Close();\ncommand.dispose();\nconn.Close();\n\u00a0 }\n}\n}<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/Z3occ3iE09g0VXz-wBZRmtgh0DWnMCit--5G9znmX8VUMoXk5JK2U7w3dtoWFlyAVD-Dtjib3R07-8gtLGb2St2uP4dobqO2H2E9CIeFDSK6iMlEzhYNDeYt7u5FsImQ_50dioeCa09KBXAJaA\" alt=\"Asp.Net Database Connection\" title=\"\"><\/figure>\n<\/div>\n\n\n<p><strong>Delete Database Record using DeleteCommand:<\/strong><\/p>\n\n\n\n<p>Suppose you want to delete the below record from the demotable:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>TutorialID<\/td><td>TutorialName<\/td><\/tr><tr><td>3<\/td><td>VB.NET complete<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>For this, you need to add the below code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace DemoApplication\n{\u00a0\u00a0\npublic partial class Demo System.Web.UI.Page\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0{\u00a0\u00a0\n\u00a0 protected void PageLoad(object sender, EventArgs e)\u00a0\u00a0\n\u00a0 {\u00a0\u00a0\nSqlCommand command = new SqlCommand(\"Delete demotable where TutorialID=3\",cnn);\nSqlDataAdapter adapter = new SqlDataAdapter();\nadapter.DeleteCommand = command;\u00a0\nadapter.DeleteCommand.ExecuteNonQuery;\ncommand.Dispose():\u00a0\ncnn.Close();\n\u00a0 }\n}\n}<\/code><\/pre>\n\n\n\n<p>Now to see the recent changes in the browser write the below code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace DemoApplication\n{\u00a0\u00a0\npublic partial class Demo System.Web.UI.Page\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0{\u00a0\u00a0\n\u00a0 protected void PageLoad(object sender, EventArgs e)\u00a0\u00a0\n\u00a0 {\u00a0\u00a0\nSqlCommand command = new SqlCommand(\"Select TutorialID,TutorialName from demotable\",cnn);\nSqlDataReader dataReader;\nString Output =\"\";\ndataReader = command.ExecuteReader();\nwhile(dataReader.Read())\n{\n\u00a0 Output = Output + dataReader.GetValue(0) + \"-\" + dataReader.GetValue(1) + \"&lt;\/br>\";\n}\nResponse.Write(Output);\u00a0\ndataReader.Close();\ncommand.dispose();\nconn.Close();\n\u00a0 }\n}\n}<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/GsaR6tOI8AGbfoSxUeOYMxHEPFZEFv9zVgRABUc75P743yjtu5r9g26Qjci1tOQ3arDSnGhpmQjnh7E2VR7g23NiNUws5tPgRmIbAUEo1ytIjTOdH9SUE6YCdTY0j8_ZfNm12OBwd_i29TZgvA\" alt=\"Database Connection\" title=\"\"><\/figure>\n<\/div>\n\n\n<p><strong>Connecting Asp.net Controls to Data:<\/strong><\/p>\n\n\n\n<p>There are some methods available in ASP.NET to link controls directly to different fields in the table.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>CheckboxList<\/li>\n\n\n\n<li>RadioButtonList<\/li>\n\n\n\n<li>DropDownlist<\/li>\n\n\n\n<li>Listbox<\/li>\n<\/ul>\n\n\n\n<p>Let us see how we can use control binding in ASP.Net. We are taking an example of a listbox.<\/p>\n\n\n\n<p>Suppose we have the following table:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>TutorialID<\/td><td>TutorialName<\/td><\/tr><tr><td>1<\/td><td>C#<\/td><\/tr><tr><td>2<\/td><td>ASP.NET<\/td><\/tr><tr><td>3<\/td><td>VB.NET complete<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Step 1:<\/strong> Create a basic web form. Using the toolbox in Visual Studio, drag and drop two components: labels and Listboxes. Name the text value of the first label as TutorialID and second label as TutorialName.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/Ju8Ojj3MPN8mSexRJSr37WfTAqQtsHacS5O3FDp2N4FeDDLKfd4sGUIfCsgusgRQCwHXXFq3r5g627oS6CLQDo_ArZRNWjYfnm2JQEDY6xzZ6w3WpGuagWSG06UP9S63sajwqWCK79uCvmX3OQ\" alt=\"Asp.Net Database Connection\" title=\"\"><\/figure>\n<\/div>\n\n\n<p><strong>Step 2: <\/strong>Click on the Listbox for Tutorial ID and then click on Choose Data source from the dialog box.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/wkNy9QkrThOnGWfz7k63gpt7RYFGoTsDoo1BMB7t4-jgKivchyaAkdGCKX6uVuQF-gHwTr_d2B4m-aux876_emPtvvfD18E2wlsbsBuKSvsMV5xf49ViLzV7AGOB33DJRVR6VdMb9UdvpNz_vQ\" alt=\"Database Connection\" title=\"\"><\/figure>\n<\/div>\n\n\n<p><strong>Step 3: <\/strong>Create a new data source by choosing the option of New data source.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/2eeoQjjRcKY4wZ3sIp9CnmPgo17bpjbh2Rp6wptBx0FGpPfMfm387BBPWXZ9n9DAFpslnWmugTdf8fLP1-TSzgRR63X7yRO_Ake3QJ367bQlceZWQYp_wC-JpoSbT_B1R_PxqtuQS8rxyuzy0A\" alt=\"Database Connection\" title=\"\"><\/figure>\n<\/div>\n\n\n<p><strong>Step 4: <\/strong>Choose the database option and give a name to our data source such as DemoDataSource. Click the OK button.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/AHe1KXWr1bsq6G0Pqn7_Z1lLqze5Wm8Iuu_W3ji4YTKbpneDwltYHPqvT2CHrNRFHVyu7L-dcskyj00A-3-84AiksB9xG2ZGGHgj53LJhPTn_BNhJ0vGbxZ-3L5Bhd9XATC8t0xR7XDx7qW7ig\" alt=\"Database Connection\" title=\"\"><\/figure>\n<\/div>\n\n\n<p><strong>Step 5: <\/strong>Create a connection to our database by clicking on the New Connection button.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/tNcnAkk4DG2uJfwz7kjY7_hlCKSafX4Kf-TPR5gTLF3Zt8DzdtZwQsptjSSzRF-lCYHJpJEaPnDKIIMRG8mcCKkR_OA3dfxRt6fADMwKtYbfWZJ7VF-lXY3cTr9TeXwj-Vb5fXJMBEtwX2oWog\" alt=\"Database Connection\" title=\"\"><\/figure>\n<\/div>\n\n\n<p><strong>Step 6:<\/strong> Choose the server name on which the SQL Server resides. Enter the credentials that are needed to connect to the database. Choose the database as demotable and click on OK.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/lJ368zXsnDiTd4MrjM8fRu1L7wgANEYJATQt_BWD6h6xFWkacOlgP54fMn7W6xUQ-ufi1PQToqJYnISHv4CCrjJ3ebJzhesUGu5eIDuSiqk0aAA3PCVjYdfgN8EYhnElvKmHdqi52UaGfMp50Q\" alt=\"Database Connection\" title=\"\"><\/figure>\n<\/div>\n\n\n<p><strong>Step 7:<\/strong> Now, you will see the Demotable table. You only need to click on the Next button to accept the default setting.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/KkcfvK4tfMFexavSTJSq23WIz__GJrXmeAGbBugvm56kJxgp-m61ce0RPTkwETy6K2FbM0TddKlfG2PbtFomwMPGwD98nxbvXTZ2wZ0sfGr7AdLXePaRoe4ScahpQ3Qi2tQB9ydjkoTfIsiemw\" alt=\"Asp.Net Database Connection\" title=\"\"><\/figure>\n<\/div>\n\n\n<p><strong>Step 8: <\/strong>Click on the Test Query button to check if can get the values from the table. Click the Finish button.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/6x4wNVE5Pg-3XZ3EvA3AwDTqs02QBtAmU513FzBRMa8RwmHNP1wA6F_MVVpx6W9br-YQkeH4LATpJ3EAAgNkxU_WCoHgSdBWdSj6SWCnIGC3cUVcYa4K7HK2E2SBVClwpn7YEDhodDGwhFMnNw\" alt=\"Asp.Net Database Connection\" title=\"\"><\/figure>\n<\/div>\n\n\n<p><strong>Step 9:<\/strong> Click the OK button to bind the TutorialID listbox to the TutorialID field name in the demotable table.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/SxT3IoImel4F5kj20MGY9jMieoXHugL2i5rjr-qPLY5zN0QrX6Tzi4yXp_wODsazL0ViIA10UmLlUsXcs6e87IDI-FD0OVXLd_jkn-unHbYNqtfX0-OhbyUBcHx_UwFOQmJ11Sok1Rj-NopP8w\" alt=\"Database Connection\" title=\"\"><\/figure>\n<\/div>\n\n\n<p><strong>Step 10:<\/strong> Click on the Tutorial Name Listbox and choose Data Source in the dialog box.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/LC9V3Oquz0y5L01MU_183lIoTeBaqN81uO3i8RqNGFZijc10f_7BJYj9dwfOeZ958ry8jXEfuesTBumLOHz4fJVNCk4ib2TqnzpKS6SsOMC-VcJSXI9HXlNYLDie6tDWjTsqOWAg3hjMBAiW0w\" alt=\"\" title=\"\"><\/figure>\n<\/div>\n\n\n<p><strong>Step 11:<\/strong> Choose the DemoData Source and click on the OK button.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/BOReq-x8EYRBORmIRGVujTWrTGGc8dR4O8Z36Um1MABxodlLk5q7Fos7GaYQMMldWoPTnOtaZjqKlGOnlsCOdGuGve79ICY8EvKIaz0pmX_jYtzgNwxXXIIMJa-5CsD89o4d2KEfExVpNdYchQ\" alt=\"Database Connection\" title=\"\"><\/figure>\n<\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/QZ9sGFNoTw8WLFswH4Y0arCTNOiQdpI5XI1nzZ20Y6rCb_P7BKl8iZ4jzuat0E8ZbUe2JmSszmBDHnCoIZZxmhUx6f8eFsnO22d3atoabRNc2kCBMTkqEDE9rhkvennPn3aWaTP36ogUE75CBw\" alt=\"Asp.Net Database Connection\" title=\"\"><\/figure>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>ASP.NET Database Connection can connect with most databases, including Oracle, Microsoft SQL Server, MongoDB, and MySQL.&nbsp; Here we are going to use Microsoft SQL Server as our database. Microsoft SQL Server is free database software, and one can easily download and install Microsoft SQL Server Express Edition. The below concepts are common in all databases: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7256,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[499],"tags":[1346,1825,1826,1823,1347,1348,1824],"class_list":["post-4799","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet-tutorials","tag-asp-net-database-connection","tag-deleting-data","tag-from-the-databas","tag-inserting-data","tag-read-database","tag-record-using-insertcommand","tag-updating-data"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=4799"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4799\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/7256"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=4799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=4799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=4799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}