Sunday, June 19, 2016
So you have following connection string to the Northwind database in your Web.config and you want to connect to the database in the code behind instead using query builder.
Here is how you would do it
1. Get the connection from the Web.config file programmatically, you can look at this blog to find out how perform this step by step. But here is the code to get the connection string from the Web.config file
2. On the top of the file specify that you want to use the Sql.Data.SqlClient namespace by typing
3. Now type the following
Then you create a new SqlCommand using the connection you've just opened
The while loop loops through the reader until there is no more rows to read and print out the ProductID, and ProductName of each database row. Make sure you always use the SqlDataReader in a try block and always remember to close it since, it's a data stream.
This is a quick and easy way to query the database.
<connectionStrings> <add name="NorthwindConnectionString" connectionString="Data Source=(local); Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data. SqlClient"/></connectionStrings>
Here is how you would do it
1. Get the connection from the Web.config file programmatically, you can look at this blog to find out how perform this step by step. But here is the code to get the connection string from the Web.config file
string connectString = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"]. ConnectionString;
2. On the top of the file specify that you want to use the Sql.Data.SqlClient namespace by typing
using System.Data.SqlClient;
3. Now type the following
using (SqlConnection conn = new SqlConnection(connectString)) { conn.Open(); SqlCommand cmd = new SqlCommand("SELECT ProductID,ProductName FROM Products", conn); SqlDataReader dataReader = cmd.ExecuteReader(); try { while (dataReader.Read()) { Response.Write("Product ID: " + dataReader["ProductID"] + dataReader["ProductName"] + "<br>"); } } finally { dataReader.Close(); } } }By using the "using" keyword you assure that the connection is automatically closed after the code is executed inside the using code block. So you don't have remember to close the connection string.
using (SqlConnection conn = new SqlConnection(connectString))Then you open the SqlConnection with
conn.Open();
Then you create a new SqlCommand using the connection you've just opened
SqlCommand cmd = new SqlCommand("SELECT ProductID,ProductName FROM Products", conn);Then to execute command to retrieve the data object SqlDataReader which will store your result in a read-only forward only stream of database rows.
try { while (dataReader.Read()) { Response.Write("Product ID: " + dataReader["ProductID"] + dataReader["ProductName"] + "<br>"); } } finally { dataReader.Close(); }
The while loop loops through the reader until there is no more rows to read and print out the ProductID, and ProductName of each database row. Make sure you always use the SqlDataReader in a try block and always remember to close it since, it's a data stream.
This is a quick and easy way to query the database.
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
Mua vé máy bay tại Aivivu, tham khảo
ReplyDeletevé máy bay đi Mỹ tháng nào rẻ nhất
có chuyến bay từ mỹ về việt nam
vé máy bay nhật về việt nam
chuyến bay từ frankfurt đến hà nội
giá vé máy bay từ Vancouver về việt nam
giá vé máy bay hàn quốc về việt nam
chuyen bay chuyen gia trung quoc
Thanks for the blog article.Much thanks again. Fantastic.
ReplyDeletedot net training
dot net online training