コードの本体は SUSER_NAME() ストアドプロシージャです。
<%@ PAGE LANGUAGE="C#" %> <%@ Import Namespace="System.Data.SqlClient" %> <html> <head> <title>connect db</title> <script runat="server"> void get_db_user(Object sender, CommandEventArgs e) { String connection_string = "Data Source=.\\SQLExpress;Integrated Security=sspi;"; String query_string = "SELECT SUSER_NAME() AS db_user_name"; using (SqlConnection con = new SqlConnection(connection_string)) { try { con.Open(); SqlCommand command = new SqlCommand(query_string, con); SqlDataReader reader = command.ExecuteReader(); lbl_result.Text = "no user"; try { while(reader.Read()) { lbl_result.Text = String.Format("{0}", reader[0]); // reader[0] は Object型のため直接代入できない } } finally { reader.Close(); } } catch(SqlException sqle) { lbl_result.Text = sqle.Message; } } } </script> </head> <body> <form runat="server"> <asp:Button id="btn_connectdb1" Text="Connect DB" CommandName="Get DB UserName" OnCommand="get_db_user" runat="server" /> <br> <asp:Label id="lbl_result" Text="" runat="server" /> </form> </body> </html>