PDA

View Full Version : ASP.NET / C# beginner question - sorry!


udjamaflip
04-21-2008, 05:35 PM
Hi everyone,

I have been tasked to learn ASP.NET/C# to a level where I can write an website which enables you to enter an RSS feed url and it will then display the feed.

The problem I have is that I cannot figure out how to make a function which will let me do anything to a database by typing functionname(sql, connection) and returns a datatable or something when its a SELECT.

Don't give me RTFM, just some commands I should be googling for or even tutorial urls :) I have spent a good hour googling for function snippets or tutorials to help me get on my way with no joy.

Thanks everyone!

I am using db.mdf as my database within the App_Data folder

mjlorbet
04-21-2008, 11:04 PM
you'll have to write a function that instantiates SqlConnection and fires off the specified request, it's not very difficult at all

This is just on the fly... not entirely sure that the datatable bit is right in this example, i usually work with formatted text, or just value extraction myself

public DataTable your_function(string Sql_statement, string conn_string){
SqlConnection temp = new SqlConnection(conn_string);
SqlCommand command = new SqlCommand(Sql_statement);
command.Connection = temp;
temp.open();
SqlDataReader reader = command.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
return dt;
}


http://msdn2.microsoft.com/en-us/library/system.data.datatable.aspx
http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx
http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection(VS.71).aspx