White Whale Studio

mssql - c# sql문 호출시 parameters를 기반으로 호출할때! 본문

IT Engineering/.Net (WPF)

mssql - c# sql문 호출시 parameters를 기반으로 호출할때!

glorymind 2014. 1. 9. 18:56
반응형
  1.     protected void Page_Load(object sender, System.EventArgs e) {  
  2.         if (!Page.IsPostBack) {  
  3.             SqlConnection MyConnection;  
  4.             SqlCommand MyCommand;  
  5.             SqlDataReader MyReader;  
  6.             SqlParameter ProductNameParam;  
  7.   
  8.             MyConnection = new SqlConnection();  
  9.             MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings["AppConnectionString1"].ConnectionString;  
  10.   
  11.             MyCommand = new SqlCommand();  
  12.             MyCommand.CommandText = "SELECT * FROM PRODUCTS WHERE PRODUCTNAME = @PRODUCTNAME";  
  13.             MyCommand.CommandType = CommandType.Text;  
  14.             MyCommand.Connection = MyConnection;  
  15.   
  16.             ProductNameParam = new SqlParameter();  
  17.             ProductNameParam.ParameterName = "@PRODUCTNAME";  
  18.             ProductNameParam.SqlDbType = SqlDbType.VarChar;  
  19.             ProductNameParam.Size = 25;  
  20.             ProductNameParam.Direction = ParameterDirection.Input;  
  21.             ProductNameParam.Value = "CHAI";  
  22.   
  23.             MyCommand.Parameters.Add(ProductNameParam);  
  24.   
  25.             MyCommand.Connection.Open();  
  26.             MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection);  
  27.   
  28.             GridView1.DataSource = MyReader;  
  29.             GridView1.DataBind();  
  30.   
  31.             MyCommand.Dispose();  
  32.             MyConnection.Dispose();  
  33.         }  
  34.     }  


반응형
Comments