justsomeguy Posted January 14, 2009 Share Posted January 14, 2009 It's been a while since I've worked with C#, what's the minimum code necessary to retrieve a single value from get or post and insert it into a database table? Do I need to set up a whole .NET application and do any install on the server, or can I just write the code and copy a single file over like with ASP classic? Do I need more than one file? Link to comment Share on other sites More sharing options...
jesh Posted January 15, 2009 Share Posted January 15, 2009 Something along these lines is the minimum amount of code you'll need. // Hard code your connection string...string connectionString = "";// Or read it from a config file...//string connectionString = ConfigurationManager.ConnectionStrings["DatabaseName"].ConnectionString;string commandText = "SELECT * FROM Table1 WHERE ID=@ID";using(SqlConnection connection = new SqlConnection(connectionString)){ SqlCommand command = new SqlCommand(commandText, connection); command.CommandType = CommandType.Text; command.Parameters.AddWithValue("@ID", Request.QueryString["MyGetParameter"]); try { connection.Open(); SqlDataReader reader = command.ExecuteReader(); if(reader.Read()) { // get at all your data here using reader["ColumnName"]. } } finally { command.Dispose(); connection.Close(); }} If you set up a .NET web application, all you'd have to do is copy over the compiled DLL(s) when you make updates to the application. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 15, 2009 Author Share Posted January 15, 2009 Cool, thanks for taking the time. Link to comment Share on other sites More sharing options...
jesh Posted January 15, 2009 Share Posted January 15, 2009 Cool, thanks for taking the time.Pah, any time. It might also be a good idea to close and dispose the reader, I forgot to add that in there. Anything that's inside the "using" block will automatically be disposed, but it doesn't hurt to call it. try { connection.Open(); SqlDataReader reader = command.ExecuteReader(); if(reader.Read()) { // get at all your data here using reader["ColumnName"]. } reader.Close(); reader.Dispose(); } Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.