Google Brother Up

2008/08/05

How to write a Database Connection in Dot Net

---------------------------------------------------------------------------

source = "server=(local); integrated security=SSPI; database=Northwind";

select = "select * from Customers";
Sqlconnection conn = new SqlConnection(source);

conn.Open();

SqlCommand cmd = new SqlCommand(select,conn);

int rowsReturned = cmd.ExecuteNonQuery(); //returns no of rows affected

---------------------------
SqlDataReader reader = cmd.ExecuteReader();

while(reader.Read())

{

ConsoleWriteline(reader[0], reader[1]); //returns a set of values

} // by iterating through records

-------------------------


object o = cmd.ExecuteScalar();

Console.WriteLine(o); // returns a column

-----------------

XmlReader xr = cmd.ExecuteXmlReader();

xr.Read(); // returns a XmlReaderObject

--------------

SqlDataAdapter da = new SqlDataAdapter(select, conn);

DataSet ds = new DataSet();

da.Fill(ds,"Customers"); //runtime schema generation
---------------------------------------------------------------------------

68 unique visitors

No comments:

Drop Down List

1. http://www.janetsystems.co.uk/Articles/NetArticles/tabid/74/itemid/161/modid/449/Default.aspx