IT Engineering/.Net (WPF)
mssql - c# sql문 호출시 parameters를 기반으로 호출할때!
glorymind
2014. 1. 9. 18:56
반응형
- protected void Page_Load(object sender, System.EventArgs e) {
- if (!Page.IsPostBack) {
- SqlConnection MyConnection;
- SqlCommand MyCommand;
- SqlDataReader MyReader;
- SqlParameter ProductNameParam;
- MyConnection = new SqlConnection();
- MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings["AppConnectionString1"].ConnectionString;
- MyCommand = new SqlCommand();
- MyCommand.CommandText = "SELECT * FROM PRODUCTS WHERE PRODUCTNAME = @PRODUCTNAME";
- MyCommand.CommandType = CommandType.Text;
- MyCommand.Connection = MyConnection;
- ProductNameParam = new SqlParameter();
- ProductNameParam.ParameterName = "@PRODUCTNAME";
- ProductNameParam.SqlDbType = SqlDbType.VarChar;
- ProductNameParam.Size = 25;
- ProductNameParam.Direction = ParameterDirection.Input;
- ProductNameParam.Value = "CHAI";
- MyCommand.Parameters.Add(ProductNameParam);
- MyCommand.Connection.Open();
- MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection);
- GridView1.DataSource = MyReader;
- GridView1.DataBind();
- MyCommand.Dispose();
- MyConnection.Dispose();
- }
- }
반응형