The example in this article describes how C # queries a SQL Server database and returns a single value. Share to everyone for your reference. The implementation methods are as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
static public string getsqlasstring (String sqltext, sqlparameter[] sqlparameters, string databaseconnectionstring) { string result = ""; SqlDataReader reader; SqlConnection connection = new SqlConnection (databaseconnectionstring); using (connection) {SqlCommand SqlCommand = connection. CreateCommand (); Sqlcommand.commandtext = SQLText; if (sqlparameters!= null) {SqlCommand. Parameters.addrange (SqlParameters); } connection. Open (); Reader = SqlCommand. ExecuteReader (); if (reader!= null) if (reader). Read ()) {result = reader. GetString (0); } return result; } |
I hope this article will help you with the C # program.