0% found this document useful (0 votes)
30 views

Database Connectivity

The document provides code examples for connecting to databases and performing common database operations like retrieving, updating, deleting, and searching data using ADO.NET and SQL Server. It includes examples of using connection strings, opening and closing connections, executing queries with SqlCommand, and retrieving data with SqlDataReader and ExecuteScalar.

Uploaded by

Shital Pandya
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Database Connectivity

The document provides code examples for connecting to databases and performing common database operations like retrieving, updating, deleting, and searching data using ADO.NET and SQL Server. It includes examples of using connection strings, opening and closing connections, executing queries with SqlCommand, and retrieving data with SqlDataReader and ExecuteScalar.

Uploaded by

Shital Pandya
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Include

using System.Data.SqlClient; using System.Data;

Connection string for access :Provider=Microsoft.Jet.OLEDB.4.0;Data Source="C:\Documents and Settings\mayur\Desktop\studdb.mdb"


<configuration> <connectionStrings> <add name="Products" connectionString=" </connectionStrings> </configuration>

Provider=Microsoft.Jet.OLEDB.4.0;Data Source="C:\Documents and Settings\mayur\Desktop\studdb.mdb" />

<add name="pubsConnectionString" connectionString="Data Source=mayurdec;Initial Catalog=pubs;Integrated Security=True" providerName="System.Data.SqlClient" /> <add name="pubsConnectionString2" connectionString="Data Source=MAYURDEC;Initial Catalog=pubs;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> string _connectionString = WebConfigurationManager.ConnectionStrings["pubsConnectionString"].Conne ctionString; SqlConnection con = new SqlConnection(_connectionString); con.Open(); con.Close();

Display data using datareader


string connectionString = WebConfigurationManager.ConnectionStrings["No rthwind"].ConnectionString; SqlConnection con = new SqlConnection(connectionString); string sql = "SELECT * FROM Employees"; SqlCommand cmd = new SqlCommand(sql, con); con.Open(); SqlDataReader reader = cmd.ExecuteReader(); StringBuilder htmlStr = new StringBuilder(""); while (reader.Read()) { htmlStr.Append("<li>"); htmlStr.Append(reader["TitleOfCourtesy"]); htmlStr.Append(" <b>"); htmlStr.Append(reader.GetString(1)); htmlStr.Append("</b>, "); htmlStr.Append(reader.GetString(2)); htmlStr.Append(" - employee from "); htmlStr.Append(reader.GetDateTime(6).ToString("d")); htmlStr.Append("</li>"); } reader.Close(); con.Close(); HtmlContent.Text = htmlStr.ToString(); }

Update using sqlcommnad


public void Update(int id, string title, string director) { SqlConnection con = new SqlConnection(_connectionString); SqlCommand cmd = new SqlCommand("UPDATE ProductS SET Title=@Tit le,Director=@Director WHERE Id=@Id", con); cmd.Parameters.AddWithValue("@Title", title); cmd.Parameters.AddWithValue("@Director", director); cmd.Parameters.AddWithValue("@Id", id); using (con) { con.Open(); cmd.ExecuteNonQuery(); } }

Delete using sqlcommand


public void Delete(int id) { SqlConnection con = new SqlConnection(_connectionString); SqlCommand cmd = new SqlCommand("DELETE ProductS WHERE Id=@Id", con); cmd.Parameters.AddWithValue("@Id", id); using (con) { con.Open(); cmd.ExecuteNonQuery(); } }

Get single value from database


protected void btnSearch_Click(object sender, EventArgs e) { string connectionString = WebConfigurationManager.ConnectionStr ings["Products"].ConnectionString; SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand("SELECT sum(price) FROM Produc); using (con) { con.Open(); Object result = cmd.ExecuteScalar(); if (result != null) lblResult.Text = result.ToString(); else lblResult.Text = "No match!"; }

You might also like