Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Using Rows.Find in ADO.NET

When using a DataTable in ADO.NET, you may want to search your table given a key. The Find method requires the index of the primary key to find the row that you're looking for. However, before you do this you must set the primary key of the Datatable. Setting the primary key in your database is not enough. To set the primary key, create a new column and then set the primary key field on the Datatable like so:


// Create the DataColumn list for holding the columns of the primary keys. In this case, we are using only one primary key
DataColumn[] primaryKeyColumns = new DataColumn[1];

// Set the first primary key to the column 'id' in the datatable
primaryKeyColumns[0] = myDataTable.Columns["id"];

// Now apply the primary key to the datatable
myDataTable.PrimaryKey = primaryKeyColumns;

RecordSet to tab-separated values

Function TSV(rs)
	Dim field
	For Each field In rs.Fields
		TSV = TSV & field.Name & VBTab
	Next
	TSV = Left(TSV, Len(TSV) - 1) & vbCr & rs.GetString()
End Function


The rows are separated by vbCr ("\r" in most languages).
The first row is the field names.

conectarte a una base de datos con .net

// description of your code here

SqlConnection conn = null;
SqlCommand cmd = null;
SqlDataReader dr = null;

String s = null;
Product prod = null;

try
{
	conn = new SqlConnection("Provider=SQLOLEDB;SERVER=sissql2;UID=sa;PWD=AlfaRoma440;DATABASE=genericatest2");

	s = " ";


	cmd = new SqlCommand(s,conn);
	dr = cmd.ExecuteReader();
	// lleno el objeto producto

	while( dr.Read())
	{

	}


}
catch (Exception ex)
{
	// poner el manejo de errores	
}
finally
{
	dr.Close();
	conn.Close();
}
dr.Close();
conn.Close();
return prod;
}
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS