コード例 #1
0
ファイル: ADO.cs プロジェクト: TiagoGraf89/DapperSample
        public void RunCommands()
        {
            // Initialize Connection
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString))
            {
                conn.Open();

                // Initialize Model
                ShowroomModel model = new ShowroomModel("Linq to SQL Test");
                string        query;

                // Insert Record
                using (SqlCommand command = conn.CreateCommand())
                {
                    query = DapperSample.Properties.Resources.InsertCommand;
                    command.CommandText = query;
                    command.Parameters.Add("@ShowroomId", SqlDbType.UniqueIdentifier).Value = model.ShowroomId;
                    command.Parameters.Add("@ShowroomName", SqlDbType.VarChar).Value        = model.ShowroomName;
                    command.Parameters.Add("@IsActive", SqlDbType.Bit).Value = model.IsActive;
                    command.ExecuteNonQuery();
                }

                // Select Record
                using (SqlCommand command = conn.CreateCommand())
                {
                    query = DapperSample.Properties.Resources.SelectCommand;
                    command.CommandText = query;
                    command.Parameters.Add("@ShowroomId", SqlDbType.UniqueIdentifier).Value = model.ShowroomId;
                    var reader = command.ExecuteReader();
                    if (reader.Read())
                    {
                        model.ShowroomId   = Guid.Parse(reader["ShowroomId"].ToString());
                        model.ShowroomName = reader["ShowroomName"].ToString();
                        model.IsActive     = reader["IsActive"].ToString() == "1" ? true : false;
                    }
                    reader.Close();
                }

                // Update Record
                using (SqlCommand command = conn.CreateCommand())
                {
                    model.IsActive      = true;
                    query               = Properties.Resources.UpdateCommand;
                    command.CommandText = query;
                    command.Parameters.Add("@ShowroomId", SqlDbType.UniqueIdentifier).Value = model.ShowroomId;
                    command.Parameters.Add("@ShowroomName", SqlDbType.VarChar).Value        = model.ShowroomName;
                    command.Parameters.Add("@IsActive", SqlDbType.Bit).Value = model.IsActive;
                    command.ExecuteNonQuery();
                }
            }
        }
コード例 #2
0
ファイル: Dapper.cs プロジェクト: TiagoGraf89/DapperSample
        public void RunCommands()
        {
            string query = "insert into crm.Showrooms (ShowroomId, ShowroomName, IsActive) values (@ShowroomId, @ShowroomName, @IsActive);";
            List <ShowroomModel> modelList = new List <ShowroomModel>();

            modelList.Add(new ShowroomModel()
            {
                ShowroomId = Guid.NewGuid(), ShowroomName = "Showroom 1", IsActive = true
            });
            modelList.Add(new ShowroomModel()
            {
                ShowroomId = Guid.NewGuid(), ShowroomName = "Showroom 2", IsActive = true
            });
            modelList.Add(new ShowroomModel()
            {
                ShowroomId = Guid.NewGuid(), ShowroomName = "Showroom 3", IsActive = true
            });
            Dapper.SqlMapper.Execute(sqlConnection, query, modelList);
            // Initialize Connection
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString))
            {
                conn.Open();

                // Initialize Model
                ShowroomModel model = new ShowroomModel("Linq to SQL Test");
                string        sqlCommand;

                // Insert Record
                sqlCommand = DapperSample.Properties.Resources.InsertCommand;
                Dapper.SqlMapper.Execute(conn, sqlCommand, model);

                // Select Record
                sqlCommand = DapperSample.Properties.Resources.SelectCommand;
                IEnumerable <ShowroomModel> modelList = Dapper.SqlMapper.Query <ShowroomModel>(conn, sqlCommand, new { ShowroomId = model.ShowroomId });
                model = modelList.ElementAt(0);

                // Update Record
                model.IsActive = true;
                sqlCommand     = Properties.Resources.UpdateCommand;
                Dapper.SqlMapper.Execute(conn, sqlCommand, model);
            }
        }