detail code for sql data connections?

Answers were Sorted based on User's Feedback



detail code for sql data connections? ..

Answer / nitin kumar

Steps for 1. Using a SqlConnection
// Fully Commented Code
using System;
using System.Data;
using System.Data.SqlClient;

class SqlConnectionDemo
{
static void Main()
{
// 1. Instantiate the connection
SqlConnection conn = new SqlConnection(
"Data Source=(local);Initial
Catalog=Northwind;Integrated Security=SSPI");

SqlDataReader rdr = null;

try
{
// 2. Open the connection
conn.Open();

// 3. Pass the connection to a command object
SqlCommand cmd = new SqlCommand("select * from
Customers", conn);

//
// 4. Use the connection
//

// get query results
rdr = cmd.ExecuteReader();

// print the CustomerID of each record
while (rdr.Read())
{
Console.WriteLine(rdr[0]);
}
}
finally
{
// close the reader
if (rdr != null)
{
rdr.Close();
}

// 5. Close the connection
if (conn != null)
{
conn.Close();
}
}
}
}

Is This Answer Correct ?    7 Yes 0 No

detail code for sql data connections? ..

Answer / bhagwat prasad sharma

Steps: SqlConnection with vb.net code
'' Code

imports system.data.sqlclients
Public Class Form1 Inherits System.Windows.Forms.Form
'' createconnection with database

dim con as new sqlconnection("server=bps; database=company; user id=sa; password=admin")

private sub button1_click()

dim str as string="insert into employee (roll_no,name,class) values(@roll_no,@name,@class)"

dim cmd as new sqlcommand =(str,con)

if con.state=connectionstate.closed than
con.open()
'' parameter passing

cmd.parameters.add("@roll_no",type.int)
cmd.parameters.add("@name",type.varchar)
cmd.parameters.add("@class",type.varchar)

cmd.parameters("@roll_no").value= txtroll.text
cmd.parameters("@name").value=txtname.text
cmd.parameters("@class").value= txtclass.text

dim i as integer
i=cmd.executeNonquery

if i>o
msgbox("addition successful")
else
msgbox("sum mistakes into add")
end if
' end of connection
con.close()
end sub
end class

Is This Answer Correct ?    2 Yes 1 No

Post New Answer

More ASP.NET Interview Questions

can i use two web.config files of ConnectionString in One Default.aspx page

6 Answers   Satyam, Verinon Technology Solutions, Wipro,


What are Session states available and its Uses?

1 Answers   CTS, iLogic,


Difference between overriding and overloading?

1 Answers  


In What Order Do The Events Of An Aspx Page Execute. As A Developer Is It Important To Undertsand These Events?

1 Answers   Siebel Systems,


What r the asp.net list controls and difference between them?

1 Answers  


In C#.net application is run on linux michine

4 Answers  


Caching techniques in .NET ?

1 Answers   Microsoft,


What is the maximum length of textbox

1 Answers  


what is client-server architecture in .net? and what is 3-tier architecture?

1 Answers  


What is difference between Server.Transfer and Response.Redirect in ASP.NET?

1 Answers   Sans Pareil IT Services,


How will you maintain versioning in asp.net 2.0?

1 Answers   MCN Solutions,


How to merge 2 tables fields in DataTable in asp.net

6 Answers   Wipro,


Categories